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
louisa barrett 7louisa barrett 7 

OnClick javaScript button error

Could someone let me know what's wrong with the below code.
It was all working fine until I introduced the IF statement, which should be a simple thing.
I'm now getting errors ranging from IF is not defined to Missing ) after arguments.
I'm must be missing something obvious.....

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
// single mail request 
(function() { 
var mail = new sforce.SingleEmailMessage(); 
var templatedId = "00Xb0000000ZiYS"; 
var contactId = ""; 

{IF({!Opportunity.Branch__c } == "Birmingham Opportunity", 
contactId = "0037E00000GpjxR", 
contactId = "0037E00000GpjeO")} 

opportunityId = "{!Opportunity.Id}"; 
mail.replyTo = "{!$User.Email}"; 
mail.targetObjectId= (contactId); 
mail.whatId= (opportunityId); 
mail.templateId = (templatedId); 
var result = sforce.connection.sendEmail([mail]); 
if(result[0].success == 'true') { 
alert("Your email has been sent."); 
} else { 
alert("I had a booboo."); 

}());
Best Answer chosen by louisa barrett 7
karthikeyan perumalkarthikeyan perumal
Hello, 

use below code 
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
// single mail request 
(function() { 
var mail = new sforce.SingleEmailMessage(); 
var templatedId = "00Xb0000000ZiYS"; 
var contactId = "";
 
var Branch= "{!Opportunity.Branch__c}";
if(Branch == "Birmingham Opportunity")
{
contactId ='0037E00000GpjxR';
 
}
else
{
contactId ='0037E00000GpjeO';
}
 
 
opportunityId = "{!Opportunity.Id}"; 
mail.replyTo = "{!$User.Email}"; 
mail.targetObjectId= (contactId); 
mail.whatId= (opportunityId); 
mail.templateId = (templatedId); 
var result = sforce.connection.sendEmail([mail]); 
if(result[0].success == 'true') { 
alert("Your email has been sent."); 
} else { 
alert("Error while sending Email"); 
} 
}());

Hopw it will work
Mark Best ANSWER if its work for you. 

Thanks
karthik
 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

use below code 
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
// single mail request 
(function() { 
var mail = new sforce.SingleEmailMessage(); 
var templatedId = "00Xb0000000ZiYS"; 
var contactId = "";
 
var Branch= "{!Opportunity.Branch__c}";
if(Branch == "Birmingham Opportunity")
{
contactId ='0037E00000GpjxR';
 
}
else
{
contactId ='0037E00000GpjeO';
}
 
 
opportunityId = "{!Opportunity.Id}"; 
mail.replyTo = "{!$User.Email}"; 
mail.targetObjectId= (contactId); 
mail.whatId= (opportunityId); 
mail.templateId = (templatedId); 
var result = sforce.connection.sendEmail([mail]); 
if(result[0].success == 'true') { 
alert("Your email has been sent."); 
} else { 
alert("Error while sending Email"); 
} 
}());

Hopw it will work
Mark Best ANSWER if its work for you. 

Thanks
karthik
 
This was selected as the best answer
louisa barrett 7louisa barrett 7
Thank you very much. I guess you can't use that style 'If' statement [ if(logical_test, true,  false) ] in a custom button.
 
Kunal Bhatia 11Kunal Bhatia 11
Hi all,

I am trying to write a simple JS button on opportunity to toggle a picklist based on its existing value. When I run it I get an error.

Check whether Sync Flag not set to 'Sync' and update it to 'To Be Sync'. Not sure why I am getting this error. Please help! Thanks!!

Button:
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 

(function() { 
var record = new sforce.SObject("Opportunity"); 
record.Id = "{!Opportunity.Id}"; 
if(record.Sync_Flag__c == "Sync") 
{ 
alert( "Hi " + '{!$User.FirstName}' + ", " + "This opportunity is already synced successfully to Netsuite. Have a good day!); 
} 
else 
{ 
record.Sync_Flag__c = "To Be Sync"; 
sforce.connection.update([record]); 
parent.location.href = parent.location.href; 
} 
}());
Error:
Error message