1

I've got a fantastic OnClick JavaScript button set up which will check if two fields are the same and populate the to/cc fields on an email template accordingly. Now, however, I want to somehow prevent the button from sending the email a second time either through disabling the button or throwing an error/alert.

Is there a way for the button to also update an "email sent" status field on the record before bringing the user over to the pre-populated email? And then, of course, use that status field as a conditional for the button...

Or, does anyone have another solution that does not involve APEX?

Current button code:

if("{!Teacher_Request__c.Teacher_Email__c}"=="{!Teacher_Request__c.Requester_Email__c}") 
{location.replace("/email/author/emailauthor.jsp?retURL=/{!Teacher_Request__c.Id}&p3_lkid={!Teacher_Request__c.Id}&rtype=003&p2_lkid={!Teacher_Request__c.TeacherId__c }&p24={!Teacher_Request__c.School_Leader_Email_del__c}&template_id=00X50000001JyZZ&[email protected]&p5=");} 
else 
{location.replace("/email/author/emailauthor.jsp?retURL=/{!Teacher_Request__c.Id}&p3_lkid={!Teacher_Request__c.Id}&rtype=003&p2_lkid={!Teacher_Request__c.TeacherId__c }&p24={!Teacher_Request__c.School_Leader_Email_del__c}&p4={!Teacher_Request__c.Requester_Email__c}&template_id=00X50000001JyZZ&[email protected]&p5=");}

4 Answers 4

2

Following code might help in updating a checkbox for that record.

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
var newRecords = []; 
var obj = new sforce.SObject("ObjectName"); 
obj.id ="{!ObjectName.Id}"; 
obj.Custom_Checkbox_API_Name__c = true;
newRecords.push(c); 
result = sforce.connection.update(newRecords); 

--- Send Email along with this Update of the custom email button.

1
  • Thanks, this was exactly the piece I needed except for one typo: newRecords.push(c); should be newRecords.push(obj);
    – Jenllip
    Jun 5, 2015 at 16:56
1

I believe you are already answering your own question in the second paragraph.

Create the checkbox field "email sent" in your sandbox, and update the code of the button to include an additional if statement to check the state of the checkbox field. The either show an alert window (if the field is checked), or perform a field update (if the field is unchecked), and then proceed with the rest of your code.

1
  • I understand that that is the answer, but I do not know how to actually write that code.
    – Jenllip
    Jun 4, 2015 at 18:02
1

Write a workflow to update the status of the email when the Teacher_Email__c == Requester_Email__c and Create a check box (it can be formula or you can update it with the same workflow)

if("{!Teacher_Request__c.EmailStatusAPIName}" != "your Expected Status"){

if(AND("{!Teacher_Request__c.Teacher_Email__c}"=="{!Teacher_Request__c.Requester_Email__c}") {

{location.replace("/email/author/emailauthor.jsp?retURL=/{!Teacher_Request__c.Id}&p3_lkid={!Teacher_Request__c.Id}&rtype=003&p2_lkid={!Teacher_Request__c.TeacherId__c }&p24={!Teacher_Request__c.School_Leader_Email_del__c}&template_id=00X50000001JyZZ&[email protected]&p5=");} 

}

else

{

location.replace("/email/author/emailauthor.jsp?retURL=/{!Teacher_Request__c.Id}&p3_lkid={!Teacher_Request__c.Id}&rtype=003&p2_lkid={!Teacher_Request__c.TeacherId__c }&p24={!Teacher_Request__c.School_Leader_Email_del__c}&p4={!Teacher_Request__c.Requester_Email__c}&template_id=00X50000001JyZZ&[email protected]&p5=");}
}else
{
alert('Email is already sent');
}
2
  • I appreciate the effort, but the conditional actually has no bearing on whether or not the email is / has been / should be sent. The conditional only tells the email template which fields to populate. I need the button to deactivate or throw a warning if either email has been sent, regardless of whether Teacher = Requester.
    – Jenllip
    Jun 4, 2015 at 18:21
  • On second look, after edits for formatting, it does look like this code will properly reference the "email status" field. The question now is, how do I update said field on send? I am unable to use an APEX trigger based on the Activity created when the email is sent, so I think I am left with updating the "email status" field via additional javascript on this button, which I do not know how to do.
    – Jenllip
    Jun 4, 2015 at 20:45
0

Thanks for everyone's help. I'm posting a scrubbed version of my final code so that others who need a "click to send email once" solution will know where to start.

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
if("{!CustomObject__c.Email_Sent__c}" == false) 
{ 
var newRecords = []; 
var obj = new sforce.SObject("CustomObject__c"); 
obj.id ="{!CustomObject__c.Id}"; 
obj.Email_Sent__c = true; 
newRecords.push(obj); 
result = sforce.connection.update(newRecords); 
if("{!CustomObject__c.Program__c}"=="Program Name 1") 
{location.replace("/email/author/emailauthor.jsp?retURL=/{!CustomObject__c.Id}&p3_lkid={!CustomObject__c.Id}&rtype=003&p2_lkid={!CustomObject__c.TeacherId__c }&template_id=00X50000001jiTU&p26={!User.Email}&p5=");} 
else{ 
if("{!CustomObject__c.Program__c}"=="Program Name 2") 
{location.replace("/email/author/emailauthor.jsp?retURL=/{!CustomObject__c.Id}&p3_lkid={!CustomObject__c.Id}&rtype=003&p2_lkid={!CustomObject__c.TeacherId__c }&template_id=00X50000001jiTF&[email protected]&p5=");} 
else{ 
if("{!CustomObject__c.Program__c}"=="Program Name 3") 
{location.replace("/email/author/emailauthor.jsp?retURL=/{!CustomObject__c.Id}&p3_lkid={!CustomObject__c.Id}&rtype=003&p2_lkid={!CustomObject__c.TeacherId__c }&template_id=00X50000001jiTP&[email protected]&p5=");} 
else{alert('Cannot find template');}}} 
} 
else 
{ 
alert('Email is already sent'); 
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .