• Ekin Van Winkle
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Hi All,

Hope you could assist me modifying a code or adding a new one. I currently have email services that creates an Opportunity.  I am not really a coder, I am just looking around how to get this done.

My goal is that when user send an email from that Opportunity record (generated from Email services) and customer replies back (with threadId generated initially), the response of the customer should be attached to existing Opportunity. How can I achieve that? Should I attach it to existing code that I have?

Here is my existing Code that generates Opportunity record:
global class OpportunityCreation implements Messaging.InboundEmailHandler {

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

     Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
     
     String fName = email.fromname.substring(0,email.fromname.indexOf(' '));
     String lName = email.fromname.substring(email.fromname.indexOf(' '));
     
     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(
        FirstName = fName,
        LastName =  lName,
        Email = email.fromAddress);
        insert 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;

  }

}


Thank you in advance.


Our current business process requires the need to track email conversations which are tied to a specific opportunity.  We have several employees that will work on a specific Oppty, so leveraging the Outlook plugin doesn't quite work.  Does anyone know if the "email to case" agent can be modified to post/manage emails to Opportunities? 
 
 
As an Opportunity is going thru its phases we have current and new customers who will email in questions specifically related to an Opportunity.  Our current workaround is that we then create a case and create a custom object which then associates the Oppty to that case.  Then we implement "email to case" and use that agent to log the emails associated to the case, which is tied to the opportunity.  But, obviously this is a HUGE work around. 
 
We also looked at the Outlook plugin, but we have several employees who will work on an opportunity.  If a customer emails in a question specific to the opportunity then it may be handled by a different CSR, so the ability to manage the emails from within SF is the key. 
 
Using "Web to Opportunity" is also not ideal as our customers prefer to use email. 
 
Any suggestions?
thanks!