2

When creating a Lead through the UI, the system is sending a New Lead Alert to the New lead owner.

However, I discovered, that if using Email Services - that New Lead alert is NOT been sent.

  • Anyone knows if this is a bug or by design?
  • Do I need to update my Email service?

Here is the relevant class.

  • Is the automated respons needs to go by: the insert lead line?
  • or the fact that I have set the lead.OwnerId actually disables the assignment rules I have?

    try{
    mToAddressBefore = envelope.toAddress.subString(0,envelope.toAddress.indexOf('@'));
    mParams = mToAddressBefore.split('\\.');
    mFromEmail = email.fromAddress; 
    // Get the user id
    User u1 = [SELECT Id FROM User WHERE Alias = :mParams[1]];
    
    mCaseObject = mParams[0];
     if (mCaseObject == 'lead'){
        lead = new Lead();
        lead.LastName = mFromEmail.subString(0,mFromEmail.indexOf('@'));
        lead.Company = mFromEmail;
        lead.OwnerId = u1.Id;
        lead.LeadSource = mParams[2];
        lead.Email = mFromEmail;
        lead.RequirementsDescription__c = email.subject + '\\n' + email.plainTextBody;
    
        insert lead;
        result.success = true;
    }  else if (mCaseObject == 'case'){
      result.success = true;
    }  else {
      result.success = false;
    }
      }catch(Exception e){
    //   result.success = false;
    //   result.message = 'Oops, I failed. **' + mFromEmail + '**' + mParams[1] + '**' + sourcetext + '**';
    }
    return result;
    

The leads are assigned correctly ( I set the owner within the email lead name - u1.Id)

The problem is that the email notification is not sent, again, this is only when the email service is used. Not when doing a regular new lead creation using the UI.

2
  • Is the problem that it is not being assigned properly, or that the email isn't being sent? Or both?
    – pchittum
    Commented Jan 23, 2013 at 11:12
  • You might want to take a look at the Database DMLOptions. Commented Jan 23, 2013 at 11:27

2 Answers 2

7

I think you might need to set the DMLOptions to trigger autoresponse email. So something like this:

Id assignRuleId = [select Id from AssignmentRule where Name = ...].Id;

Database.DMLOptions dlo = new Database.DMLOptions();
dlo.assignmentRuleHeader.assignmentRuleId = assignRuleId; //specify assignment rule to tigger
//dlo.EmailHeader.triggerAutoResponseEmail = true; //this triggers lead queue assignment emails
Lead myLead = new Lead(...set field values here...);
database.insert(myLead, dlo);

Typically, I think you'd most likely use the assignment rule. The autoresponse header is typically only for when you manually assign a queue according to the docs. I don't think you would do both, as the assignment rule should trigger the Lead notification email anyway.

1
  • In my testing, I found that you need to use triggerUserEmail to get emails sent to the Assignee. I posted a different answer even though I'm using the same ideas. Commented Jan 24, 2013 at 20:39
4

You need to explicitly tell it to use assignment rules and to send the emails using the Database.DMLOptions.

Lead l = new Lead(FirstName = 'Joe', LastName = 'Smith', Company = 'XYZ Co');
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
dmo.EmailHeader.triggerUserEmail = true;
Database.insert (l, dmo);

You may not need to do the assignment rule option since you are setting the owner in your code, but setting the email header option should have it send the email.

You must log in to answer this question.

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