function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonas MyhreJonas Myhre 

Need help to set up Email service

Hello, im trying to set up an email service which creates a lead. I have tried the appexchange options, but they either cost money, or doesnt have the required functionality. 

The email service needs to accept attachments, and have a way to distinguish between emails sent in with the subject "Case" or "Intel", this can either be done after the email service, the important thing is the attachments part. 

Does anyone have some simple code / guidance for me here? I would appreciate it very much! 
Jonas MyhreJonas Myhre
I have the following code set up, it doesnt work, any ideas why? No  lead is created, i've turned on email routing to my email, but not much is happening..


global class CreateTaskEmailExample implements Messaging.InboundEmailHandler {
 
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
                                                       Messaging.InboundEnvelope env){
 
    // Create an InboundEmailResult object for returning the result of the 
    // Apex Email Service
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
  
    String myPlainText= '';
    
    // Add the email plain text into the local variable 
    myPlainText = email.plainTextBody;
   
    // New Lead object to be created
    Lead[] newLead = new Lead[0];
   
    // Try to look up any contacts based on the email from address
    // If there is more than one contact with the same email address,
    // an exception will be thrown and the catch statement will be called.
    try {
      Contact vCon = [SELECT Id, Name, Email
        FROM Contact
        WHERE Email = :email.fromAddress
        LIMIT 1];
      
      // Add a new Lead to the contact record we just found above.
      newLead.add(new Lead(Comment__c =  myPlainText,
           LeadSource = 'Intel',
           Company = 'Market Intel',
           Title = email.subject,
           Status = 'Open'));
           
     
     // Insert the new Lead 
     insert newLead;    
     
     System.debug('New Lead Object: ' + newLead );   
    }
    // If an exception occurs when the query accesses 
    // the contact record, a QueryException is called.
    // The exception is written to the Apex debug log.
   catch (QueryException e) {
       System.debug('Query Issue: ' + e);
   }
   
   // Set the result to true. No need to send an email back to the user 
   // with an error message
   result.success = true;
   
   // Return the result for the Apex Email Service
   return result;
  }
}