Monday, May 20, 2013

The Quick Email Button Revisited: Sending One-And-Done Emails with Workflow

In my popular post The Quick Email Button I reference a means of making a button that just sends the email immediately with no edit window.  I then mention that it's been deprecated for orgs that have been created since 2010.  

Salesforce.com deprecated this URL parameter for a good reason: the save=1 parameter is not particularly secure, and a clever attacker could make spoofed links to Salesforce.com to save random data to your org.  I will say this about URL hacks in general: they are hacks, and that makes them inherently unreliable.  Sometimes they will stop working.  So despite my tacit encouragement in my original post, try not to use them if you can avoid them.

The question remains, then:

How can you make a button that sends a one-and-done email?

This simple but roundabout method uses Workflow email alerts and a Javascript custom button.  Note that this method uses the Salesforce.com API, so it will only work with orgs that have the API (generally Enterprise Edition and above, or Professional Edition with the API add-on).  Here's how to do it:

1. Make a custom checkbox field on your object called "Send Email."  Don't show it on any page layouts -- it should only be used as a proxy for the workflow we'll define below.
2. Make a new workflow rule on this object and set it to run when a record is "created, and any time it’s edited to subsequently meet criteria."  Change the Rule Criteria to run if the "formula evaluates to true," and in the formula box, enter ISCHANGED(Send_Email__c).
3. Press Next and add an Email Alert that selects the proper template and sends to the person you want to send to (generally the email address of the Lead or Contact you're putting the button on).  Save the email alert.  Don't forget to activate your workflow rule!
4. Now go to your object and create a custom button on it.  Set this custom button to Execute Javascript, and put code in that looks like this (the below example is for the Lead object, you'll need to modify it slightly for any other object type):

{!REQUIRESCRIPT("/soap/ajax/16.0/connection.js")}
var leadObj= new sforce.SObject("Lead");
leadObj.Id='{!Lead.Id}';
//Update that checkbox to true, which will trigger the workflow
leadObj.Send_Email__c = true;

var result=sforce.connection.update([leadObj]); /*updating the object*/

if (result[0].success=='false') {
    alert(result[0].errors.message);
} else {
    location.reload(true);
}


5. Add this custom button to your page layouts.

And voila!  You now have a custom button that immediately sends out an email without any further user intervention.

There's another way to do this with just a custom button alone and no workflow, but it's a bit more involved on the code side.  I'll cover that in a future post on this topic.

Happy emailing!


2 comments:

  1. Thumbs up! Just one remark, ISCHANGED() does NOT work with "created, and any time it’s edited to subsequently meet criteria." only with Evaluate the rule when a record is created, and every time it’s edited!

    Best

    ReplyDelete
  2. Is there any way to re-create the auto send function in Group edition?

    ReplyDelete