Wednesday, June 19, 2013

Email handler class-Salesforce

How to mock the Email to Salesforce Functionality using a inbound handler class?.....

 There might be certain cases where you have to mock the email to salesforce feature in my case the scenario was like this i wanted to integrate the sales user's Gmail Account with Salesforce so if the sales user receives the E-mail it will get auto forwarded to SFDC so that it have to create activity history logs in the related records. I can use the standard  Email to salesforce feature but the problem is Google sends the verification code to the target email address while setting up the auto forwarding feature so i wanted to fetch the verification code first where as SFDC dont have a feature to display the verification code sent via email to salesforce addres. To acheive this i have to create a inbound E-mail handler class for this. So the class which i created will automatically create tasks and activity history in all its associated records in Contact,Account and Opportunity object from the incoming email of the SFDC user's G mail address. In contacts and accounts we can associate the tasks and activity history since the Email field in contacts is related with accounts in some way. But what for opportunities?

    luckily we have something called opportunity contact roles we can query the opportunity contact role and if there is an match in the email adddress from the incoming Gmail address  with contact email address in Opportunity contact role an activity history and tasks will be created automatically.
      Similarly the activity history will be created in Accounts and  Contacts if there is an match in the email address. I've posted the code for reference below.
 /****************  
 Created by:Nirmal Christopher  
 Created Date:03.05.2013  
 Decription: from the incoming mail tasks will be created for Leads,Accounts and Opportunity  
 ****************/  
 global class GetVerificationCode implements Messaging.InboundEmailHandler {  
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope envelope) {  
  system.debug(email);  
  system.debug(envelope);  
  Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();  
    String mail = email.fromAddress;  
    list<Task> opptask = new list<Task>();  
    list<Task> ltask = new list<Task>();    
    list<task>ctask=new list<task>();   
    list<OpportunityContactRole> ocr= [select OpportunityId,ContactId from OpportunityContactRole where Contact.Email=:mail ];  
    Set<Id> opportunityIdSet = new Set<Id>();  
    for(OpportunityContactRole o:ocr){  
      opportunityIdSet.add(o.OpportunityId);  
    }  
    list<Opportunity> opportunitylist=[select id from opportunity where id in: opportunityIdSet];  
     System.debug('********************************'+opportunitylist.size());  
    list<lead> leadTask =[Select id,email from lead where Email=:mail];   
    list<contact>contask=[select id,email from contact where Email=:mail];  
    System.debug('********************************'+contask.size());  
    //Inserting Opportunity Task  
   for(Opportunity o:opportunitylist)  
    {  
    Task tsk=new Task();  
    tsk.Subject = email.Subject;  
    tsk.OwnerId=UserInfo.getUserId();  
    tsk.Description = email.plainTextBody;  
    tsk.WhatId = o.id;  
    tsk.status = 'Completed';  
    tsk.Type__c='Recieved';  
    tsk.SYS_Check_Task_Creation__c=false;  
    opptask.add(tsk);  
    }  
    try{  
    insert opptask;  
    }catch(DmlException e){  
      System.debug('********opptask******'+e);  
    }   
   //Inserting Contacts Task  
   for(contact con:contask){  
    Task tsk1=new Task();  
    tsk1.Subject = email.Subject;  
    tsk1.OwnerId=UserInfo.getUserId();  
    tsk1.Description = email.plainTextBody;  
    tsk1.WhoId = con.Id;  
    tsk1.status = 'Completed';  
    tsk1.Type__c='Recieved';  
    tsk1.SYS_Check_Task_Creation__c=false;   
    ctask.add(tsk1);   
    }    
    try{  
    insert ctask;  
     System.debug('*******contask*******'+ctask);  
    }catch(DmlException e){  
      System.debug('*******contask*******'+e);  
    }  
   //Inserting Lead task  
    for(Lead lt:leadTask)  
    {  
    Task tsk2=new Task();  
    tsk2.Subject = email.Subject;  
    tsk2.OwnerId=UserInfo.getUserId();  
    tsk2.Description = email.plainTextBody;  
    tsk2.WhoId = lt.ID;  
    tsk2.status = 'Completed';  
    tsk2.Type__c='Recieved';  
    tsk2.SYS_Check_Task_Creation__c=false;  
    ltask.add(tsk2);  
    }  
    try{  
    insert ltask;  
    System.debug('**************'+ltask);  
    }catch(DmlException e){  
      System.debug('**************'+e);  
    }   
     return result ;  
  }  
 }  

By this way we can mock the standard E-mail to Salesforce feature using a Apex email handler class.

          After writing the class you have to enable email service feature in salesforce. You can access email services feature like this setup-->develop-->Email services and create a new email service . For the class choose the class which we created above click save it will generate a email service address, Use the email service address in your Gmail account's auto forwarding feature it will prompt you for a verification code  and a code will be sent to the auto forwarding address which you added you can capture the verifiction code in the debug logs. That's it....tadahhhhh your g mail account and SFDC instance is now integrated. create few filters in your Gmail account as required to forward specific emails in SFDC instance. So from the incoming email tasks will be created as activity history in Leads,contacts and Opportunities if there is a match in the incoming email address.



4 comments:

skyjack said...

Hi Nirmal

Thats a great job creating tasks from Inbound Email!

I am writing a similar apex class to setup an Email to Case functionality wherein the incoming email will generate a new record on Case.

My question for you is that, how can we fetch the date-time stamp from the inbound email header ?

I wish to have the date & time of the moment the email was sent.

Reply awaited

Thanks!

hosted call center software said...

Your work is very good and I appreciate you and hopping for some more informative posts
hosted call center solutions

Amir Iqbal said...

I am a freelancer and blogger. This is my site.
https://freewincrack.com/


Amir Iqbal said...

Nice content, Keep it up. Thanks for sharing.
https://realcracks.org/