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&p26=myname@email.org&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&p26=myname@email.org&p5=");}
| improve this question | | | | |
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.

| improve this answer | | | | |
  • Thanks, this was exactly the piece I needed except for one typo: newRecords.push(c); should be newRecords.push(obj); – Jenllip Jun 5 '15 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.

| improve this answer | | | | |
  • I understand that that is the answer, but I do not know how to actually write that code. – Jenllip Jun 4 '15 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&p26=myname@email.org&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&p26=myname@email.org&p5=");}
}else
{
alert('Email is already sent');
}
| improve this answer | | | | |
  • 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 '15 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 '15 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&p26=Program2Manager@email.org&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&p26=Program3Manager@email.org&p5=");} 
else{alert('Cannot find template');}}} 
} 
else 
{ 
alert('Email is already sent'); 
}
| improve this answer | | | | |

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.