function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ti Saunders 8Ti Saunders 8 

Updating Hidden Fields With OnClick JavaScript Button

I'm trying to get a custom activity button to work that will toggle a checkbox field on the contact. It works fine when the field is visible, but fails when I hide the field (needs to be hidden). 

Error I'm getting is:

"A problem with the OnClick JavaScript for this button or link was encountered:

Unexpected token =="
 
{!REQUIRESCRIPT("/soap/ajax/41.0/connection.js")} 

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

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ti,

Greetings to you!

You need to use if ( result[0].getBoolean( "success" ) ) 
I have checked in my org after hiding checkbox field and it is working fine.

Try this:
{!REQUIRESCRIPT("/soap/ajax/41.0/connection.js")} 

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

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
Ti Saunders 8Ti Saunders 8
Still getting the same error message.

It will work when the fields are exposed, but fail when theya re hidden.

User-added image