+ Start a Discussion
Ti Saunders 8Ti Saunders 8 

Oneclick JavaScript Button to Toggle Custom Checkbox Field on Contact

I'm trying to create a button that will toggle a checkbox (i.e. if checked then uncheck, if unchecked then check). This checkbox will trigger a workflow rule that will send an email to the contact 24 hours later.

I realize that these buttons may become obsolete in Lightning, but my org does not wish to switch in the forseeable future (not my decision).

Any help is much appreaciated!
Best Answer chosen by Ti Saunders 8
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ti,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
{!REQUIRESCRIPT("/soap/ajax/41.0/connection.js")} 

if({!Contact.Checkbox__c} == false) 
{ 
    var c = new sforce.SObject("Contact"); 
    c.id = "{!Contact.Id}"; 
    c.Checkbox__c = true; 
    result = sforce.connection.update([c]); 
    if ( result[0].getBoolean( "success" ) ) 
    { 
        window.location.reload(); 
    } 
    else 
    { 
        alert( result[0].errors.message); 
    } 
} 
else 
{ 
    var c = new sforce.SObject("Contact"); 
    c.id = "{!Contact.Id}"; 
    c.Checkbox__c = false; 
    result = sforce.connection.update([c]); 
    if ( result[0].getBoolean( "success" ) ) 
    { 
        window.location.reload(); 
    } 
    else 
    { 
        alert( result[0].errors.message); 
    } 
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Ti,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
{!REQUIRESCRIPT("/soap/ajax/41.0/connection.js")} 

if({!Contact.Checkbox__c} == false) 
{ 
    var c = new sforce.SObject("Contact"); 
    c.id = "{!Contact.Id}"; 
    c.Checkbox__c = true; 
    result = sforce.connection.update([c]); 
    if ( result[0].getBoolean( "success" ) ) 
    { 
        window.location.reload(); 
    } 
    else 
    { 
        alert( result[0].errors.message); 
    } 
} 
else 
{ 
    var c = new sforce.SObject("Contact"); 
    c.id = "{!Contact.Id}"; 
    c.Checkbox__c = false; 
    result = sforce.connection.update([c]); 
    if ( result[0].getBoolean( "success" ) ) 
    { 
        window.location.reload(); 
    } 
    else 
    { 
        alert( result[0].errors.message); 
    } 
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Ti Saunders 8Ti Saunders 8
Works! Thank you!