Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
    
Is the problem that it is not being assigned properly, or that the email isn't being sent? Or both? –  Peter Jan 23 '13 at 11:12
    
You might want to take a look at the Database DMLOptions. –  Peter Knolle Jan 23 '13 at 11:27
add comment

2 Answers

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.

share|improve this answer
    
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. –  Daniel Hoechst Jan 24 '13 at 20:39
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.