An effort to bring a revolution in Salesforce.

Thursday, 9 February 2017

Email To Opprtunity|Contact|Lead|Sobject Salesforce Email Service

Email To Opprtunity | Email To Sobject | Email To Contact | Salesforce Email  Service


global class EmailtoOpportunityCreation implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

     Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
     System.debug('====> Created opportunity '+result);
     Contact vCon;
     for(Contact c: [Select Id, Name, Email, AccountId From Contact Where Email = :email.fromAddress Limit 1])
         vCon = c;
     if(vCon == null)
     {
        vCon = new Contact(lastname = 'Email-OPP',email = email.fromAddress);
        insert vCon;
        
        Account A = new Account(Name=email.Subject);
        vCon.AccountId = a.id;
        update vCon;
     }   
     
     Opportunity opportunity = new Opportunity();
     opportunity.Name = email.Subject;
     opportunity.StageName = 'Prospecting';
     opportunity.CloseDate = Date.today();
     opportunity.Email_Body__c = email.plainTextBody;
     //opportunity.Contact_Name__c = vCon.Id;
     opportunity.AccountId = vCon.AccountId;
     insert opportunity;
     
    
       System.debug('====> Created opportunity '+opportunity.Id);
       
      
if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
      for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
        Attachment attachment = new Attachment();
        // attach to the newly created opportunity record
        attachment.ParentId = opportunity.Id;
        attachment.Name = email.binaryAttachments[i].filename;
        attachment.Body = email.binaryAttachments[i].body;
        insert attachment;
      }
    }
  
    return result;

  }

}

Demo: Demo Link

, , ,

No comments:

Post a Comment

Get Gmail, Docs, Drive, and Calendar for business