2

What is the best way to trigger an email when I am changing the Owner of a Lead using Apex/Sforce API? I would like the email to go out to the new owner to let them know about this.

The best that I have come up with so far is to add it to a specific campaign and have a workflow rule looking for Lead updates to that campaign. But I was hoping that there was an easier way (something like a workflow rule triggered on changes to the Owner field) - since this requires creating an artificial Campaign on the site just needed for this workflow.

3

Use workflow, criteria would be when the lead owner id changed, and then add an email alert to it ...

Apex is totally possible, but a gross overkill, and also subject to limits on how many emails can be sent in one day.

  • 1
    The "daily mass limit" doesn't apply if you use setTargetObjectId that specifies a User ID. Also, workflow emails do have a limit. – sfdcfox Jun 27 '14 at 4:52
4

To send an email to the new owner with ownership change in Apex Code or the API, use the emailHeader options. Here's Apex Code:

public class ChangeLeadOwner {
    public static void toUser(Map<Id, Id> leadToOwner) {
        Database.DmlOptions options = new Database.DmlOptions();
        options.emailHeader.triggerUserEmail = true;
        Lead[] records = new Lead[0];
        for(Id leadId: leadToOwner.keySet()) {
            records.add(new Lead(Id=leadId, OwnerId=leadToOwner.get(leadId)));
        }
        Database.update(records, options);
    }
}

It's called simply by providing a map of Lead ID values to their new respective owner:

Map<Id, Id> leadMap = new Map<Id, Id>();
// Populate leadMap here
// Then change ownership
ChangeLeadOwner.toUser(leadMap);

The API has a similar mechanism. Simply create an emailHeader, attach it to the binding using your API's interface (details vary with language, consult the API documentation), then perform your update as normal.

0

With last releases: Simple process builder on lead, and criteria of Owner.Ischanged

simple and quick

-1

A simple workflow rule should work in this case.

Else since you are already changing the owner in the code, you can make use of the SingleEmailMessage class to send email to the new Owner.

Here is the code snippet.

// create a new single email message object
// that will send out a single email to the addresses in the To, CC & BCC list.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

// Strings to hold the email addresses to which you are sending the email.
String[] toAddresses = new String[] {'user@acme.com'}; 
String[] ccAddresses = new String[] {'smith@gmail.com'};


// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);

// Specify the address used when the recipients reply to the email. 
mail.setReplyTo('support@acme.com');

// Specify the name used as the display name.
mail.setSenderDisplayName('Salesforce Support');

// Specify the subject line for your email address.
mail.setSubject('New Case Created : ' + case.Id);

// Set to True if you want to BCC yourself on the email.
mail.setBccSender(false);

// Optionally append the salesforce.com email signature to the email.
// The email address of the user executing the Apex Code will be used.
mail.setUseSignature(false);

// Specify the text content of the email.
mail.setPlainTextBody('Your Case: ' + case.Id +' has been created.');

mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created.<p>'+
     'To view your case <a href=https://na1.salesforce.com/'+case.Id+'>click here.</a>');

// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  • Could you go into more detail about how the simple workflow rule world be defined? – Yaakov Ellis Jun 26 '14 at 15:51
  • Don't use "toAddresses" to address users, because those count against the daily limit, while setTargetObjectId does not. – sfdcfox Jun 27 '14 at 4:54

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.