Find your content:

Search form

You are here

How to have a specific Email2Lead address and assign to different owners?


I want to be able to have specific email addresses, and when an email arrives to that address, it will be forwarded, and added into SF, and assigned to a specific owner.

I thought of using either Email2Lead and/or InboundEmail services.

Has anyone wrote this before?

Attribution to: Saariko

Possible Suggestion/Solution #1

Since I wanted to have something very general, and give the management to the users, with as much freedom as possible. what I did was as follow:

I created an Apex Class to process inbound email

 * Email services are automated processes that use Apex classes
 * to process the contents, headers, and attachments of inbound
 * email. 
global class ProcessInboundEmail implements Messaging.InboundEmailHandler{
    global Messaging.InboundEmailResult handleInboundEmail(
      Messaging.InboundEmail email, 
      Messaging.InboundEnvelope envelope){

     Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

      Lead lead;
      * mParams is the beginning of the email to in the email service.
      * should be divided by '.' and be placed in this order:
      * 1. mObject: values: 'lead' only supported now.
      * 2. mOwnerID: SF Alias representation (User/Partner is ok.  -- Queue is NOT supported) 
      * 3. if lead: this is source in string format NO spaces.
      *    InboundMailProcessor  
      String mFromEmail;
      String mCaseObject;
      String [] mParams; 
      String mToAddressBefore;

      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;

What this enables me is to open the processor in the future to other objects as well.

Than I created an emailService, to send the emails to this processor.

Now I created specific email addresses, according to the : object.user.lead_source that I need.

So for example: will actually become a:

  • new lead
  • assigned to ussales
  • lead source to: meeting

This gives me SUPER freedom, as I can play with both the lead source, and the lead owner, with no need to write codes (just create a new email address)

As this is something NOT presented with standard features of salesforce. I am sure that there are other apps that might give this, but this one is super easy and simple to manage.

  • I guess I can created a package/app from this and post it to app-axchange - but that's way beyond my scope and time.

  • I guess I also took the logic outside of the class.

Attribution to: Saariko

Possible Suggestion/Solution #2

You can write the assignment logic inside a class that goes along with your inboundemail service.

Attribution to: Tanuj Kumar Sharma
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content