I am completely new to Salesforce development. Custom button on Contact object needs to create a task and send email( email template) to the contact email when Language field is English or blank, when spanish, spanish email template needs to be sent. But this email needs to go out only one first click and after 180 days, if custom button clicked again, should send email. No action need in the interm. I have asked this question on Salesforce development forum too, but no solution yet. Custom button has been created using below Javascript to create a new task and hidden field Email sent date to populate the date field. Added if condition to verify "Is eligible to survey" formula field and send email only when it's true. But errors out as TRUE is not defined . Can someone help me please

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

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

            var result = sforce.connection.sendEmail([message]); 
        else { 
                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(); 


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

            alert('An Error has Occurred. Error: Please Enter Language' +     result); 
        alert('An Un-expected Error has Occurred: Error: ' + e); 
  • 2
    What is your question about this code? – David Reed Feb 28 '19 at 14:55
  • Hi, 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 if({!Contact.Is_Eligible_to_Survey__c} == "TRUE") // added before try{ – Jyothi Feb 28 '19 at 18:00
  • 1
    That line does not appear in the code you have posted. Please make edits to your question to ensure that it contains both a detailed explanation of your problem and the specific code that illustrates the issue. – David Reed Feb 28 '19 at 18:25
  • David, i did update my post with current code. I have been manipulating code to see if i will any success. Right now error-- An Un-expected Error has Occurred: Error: ReferenceError: English is not defined. – Jyothi Feb 28 '19 at 18:44

This line

if ({!Contact.Is_Eligible_to_Survey__c} == "TRUE") 

is missing quote marks around the merge expression {!Contact.Is_Eligible_to_Survey__c}. As a result, when merged, it looks like this:

if (TRUE == "TRUE") 

That's why you get a message saying that TRUE is not defined. The templating engine is "dumb"; it doesn't understand JavaScript and just places the literal value of the merge field exactly where you tell it you want it.

There are several other locations in your code that have the same problem. Anywhere you wish to populate the value of a merge expression into JavaScript, you need to wrap it appropriately. You should additionally use the JSENCODE() function to insert escapes into merged text anywhere a special character, such as an apostrophe, might be present.

  • i tried with and without quotes as -- if({!Contact.Language__c}==English && {!Contact.Is_Eligible_to_Survey__c}==TRUE) . Can you please help with the format JSENCODE() and merge field – Jyothi Feb 28 '19 at 18:49
  • You need quotes around both the merge field and the literal JavaScript value. – David Reed Feb 28 '19 at 18:50
  • Marked quotes around both. {"!Contact.Language__c"}=="English" && {"!Contact.Is_Eligible_to_Survey__c"}=="TRUE"). But a new error unexpected token }. Verified the number of } and did match. – Jyothi Feb 28 '19 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.