+ Start a Discussion
Jyothi NimmakayalaJyothi Nimmakayala 

Javascript button with If condition- erroring out

Below code is to generate new task and generate email alert when 'Is Eligible to Survey" formula field is true . But i am receiving error as TRUE is not defined . Can someone help please.

{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')} 
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")} 
if({!Contact.Is_Eligible_to_Survey__c} == "TRUE") 
try{ 
var newTask = new sforce.SObject('Task'); 
newTask.OwnerId = '{!$User.Id}'; 
newTask.Subject = 'Officevisit: Walk-In'; 
newTask.Status = 'Completed'; 
newTask.WhoId = '{!Contact.Id}'; 
if({!Contact.Language__c}=="English"){ 
var message = new sforce.SingleEmailMessage(); 

message.targetObjectId = "{!Contact.Id}"; 
message.toAddresses = "{!Contact.Email}"; 
message.templateId = "00X4C000000QvzM"; 

var result = sforce.connection.sendEmail([message]); 

else { 
({!Contact.Language__c}=="Spanish") 
var message = new sforce.SingleEmailMessage(); 

message.targetObjectId = "{!Contact.Id}"; 
message.toAddresses = "{!Contact.Email}"; 
message.templateId = "00X4C000000QvzM"; 


var result = sforce.connection.create([newTask]); 

var ContactToUpdate = new sforce.SObject("Contact"); 
ContactToUpdate.Id = "{!Contact.Id}"; 
ContactToUpdate.Survey_Sent_Date__c = new Date(); 
sforce.connection.update([ContactToUpdate]); 

window.location.reload(); 

if(result[0].success=='true'){ 
alert('The Task was created Successfully.'); 


else{ 
alert('An Error has Occurred. Error: Please Enter Language' + result); 


catch(e){ 
alert('An Un-expected Error has Occurred: Error: ' + e); 
}

Thank you
Best Answer chosen by Jyothi Nimmakayala
Jyothi NimmakayalaJyothi Nimmakayala
This Code works now after changing if condition 
{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')} 
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")} 

try{ 
var newTask = new sforce.SObject('Task'); 
newTask.OwnerId = '{!$User.Id}'; 
newTask.Subject = 'Officevisit: Walk-In'; 
newTask.Status = 'Completed'; 
newTask.WhoId = '{!Contact.Id}'; 


var result = sforce.connection.create([newTask]); 

var ContactToUpdate = new sforce.SObject("Contact"); 
ContactToUpdate.Id = "{!Contact.Id}"; 
ContactToUpdate.Survey_Sent_Date__c = new Date(); 

sforce.connection.update([ContactToUpdate]); 

if 
('{!Contact.Language__c}'=='English'&&'{!Contact.Is_Eligible_to_Survey__c}' == 'TRUE' ) 

var message = new sforce.SingleEmailMessage(); 

message.targetObjectId = "{!Contact.Id}"; 
message.toAddresses = "{!Contact.Email}"; 
message.templateId = "00X4C000000QvzM"; 
var result = sforce.connection.sendEmail([message]); 

else { 
('{!Contact.Language__c}'=='Spanish'&&'{!Contact.Is_Eligible_to_Survey__c}' == 'TRUE' ) 
var message = new sforce.SingleEmailMessage(); 

message.targetObjectId = "{!Contact.Id}"; 
message.toAddresses = "{!Contact.Email}"; 
message.templateId = "00X4C000000Qw3E"; 
var result = sforce.connection.sendEmail([message]); 


window.location.reload(); 

if(result[0].success=='true') 

alert('The Task was created Successfully.'); 


else{ 
alert('An Error has Occurred. Error: Please Enter Language' + result); } } 
catch(e){ 
alert('An Un-expected Error has Occurred: Error: ' + e); 
}
 

All Answers

Murali MattaMurali Matta
Hi Joythi,

Try below code.
 
{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')} 
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")} 
if({!Contact.Is_Eligible_to_Survey__c}) 
try{ 
var newTask = new sforce.SObject('Task'); 
newTask.OwnerId = '{!$User.Id}'; 
newTask.Subject = 'Officevisit: Walk-In'; 
newTask.Status = 'Completed'; 
newTask.WhoId = '{!Contact.Id}'; 
if({!Contact.Language__c}=="English"){ 
var message = new sforce.SingleEmailMessage(); 

message.targetObjectId = "{!Contact.Id}"; 
message.toAddresses = "{!Contact.Email}"; 
message.templateId = "00X4C000000QvzM"; 

var result = sforce.connection.sendEmail([message]); 
} 
else { 
({!Contact.Language__c}=="Spanish") 
var message = new sforce.SingleEmailMessage(); 

message.targetObjectId = "{!Contact.Id}"; 
message.toAddresses = "{!Contact.Email}"; 
message.templateId = "00X4C000000QvzM"; 
} 

var result = sforce.connection.create([newTask]); 

var ContactToUpdate = new sforce.SObject("Contact"); 
ContactToUpdate.Id = "{!Contact.Id}"; 
ContactToUpdate.Survey_Sent_Date__c = new Date(); 
sforce.connection.update([ContactToUpdate]); 

window.location.reload(); 

if(result[0].success=='true'){ 
alert('The Task was created Successfully.'); 

} 
else{ 
alert('An Error has Occurred. Error: Please Enter Language' + result); 
} 
} 
catch(e){ 
alert('An Un-expected Error has Occurred: Error: ' + e); 
}

​​​​​​​Let me know if you have any confusion.

Kindly mark this as solved if the reply was helpful.

Thanks,
Murali
Jyothi NimmakayalaJyothi Nimmakayala
Hi Murali,

I noticed that you have removed "True" value from the code. But i want this task to work only when "Is eligible to survey field" is true else, everytime the custom button is pushed would send email and create task and i need to avoid that.

Please advise.
Jyothi NimmakayalaJyothi Nimmakayala
This Code works now after changing if condition 
{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')} 
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")} 

try{ 
var newTask = new sforce.SObject('Task'); 
newTask.OwnerId = '{!$User.Id}'; 
newTask.Subject = 'Officevisit: Walk-In'; 
newTask.Status = 'Completed'; 
newTask.WhoId = '{!Contact.Id}'; 


var result = sforce.connection.create([newTask]); 

var ContactToUpdate = new sforce.SObject("Contact"); 
ContactToUpdate.Id = "{!Contact.Id}"; 
ContactToUpdate.Survey_Sent_Date__c = new Date(); 

sforce.connection.update([ContactToUpdate]); 

if 
('{!Contact.Language__c}'=='English'&&'{!Contact.Is_Eligible_to_Survey__c}' == 'TRUE' ) 

var message = new sforce.SingleEmailMessage(); 

message.targetObjectId = "{!Contact.Id}"; 
message.toAddresses = "{!Contact.Email}"; 
message.templateId = "00X4C000000QvzM"; 
var result = sforce.connection.sendEmail([message]); 

else { 
('{!Contact.Language__c}'=='Spanish'&&'{!Contact.Is_Eligible_to_Survey__c}' == 'TRUE' ) 
var message = new sforce.SingleEmailMessage(); 

message.targetObjectId = "{!Contact.Id}"; 
message.toAddresses = "{!Contact.Email}"; 
message.templateId = "00X4C000000Qw3E"; 
var result = sforce.connection.sendEmail([message]); 


window.location.reload(); 

if(result[0].success=='true') 

alert('The Task was created Successfully.'); 


else{ 
alert('An Error has Occurred. Error: Please Enter Language' + result); } } 
catch(e){ 
alert('An Un-expected Error has Occurred: Error: ' + e); 
}
 
This was selected as the best answer