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
Swathi CseSwathi Cse 

Email services

hi,
How to write code for inserting the and if duplicate are there that has to be updated in email servies.
KaranrajKaranraj
You can write apex inbound email service class to get the email information and do your logic accordingly. Check the below sample code
 
global class tasks implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
                                                  Messaging.InboundEnvelope env){
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
String myPlainText = '';
try
{
      myPlainText = email.plainTextBody.substring(0, email.plainTextBody.indexOf('<stop>'));
}
catch (System.StringException e)
{
     myPlainText = email.plainTextBody;
     System.debug('No <stop> in email: ' + e);
}
    // Try to lookup any contacts based on the email from address
    // If there is more than 1 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];
      
      Contact con = new Contact(); 
      vCon.FirstName = email.fromname.substring(0,email.fromname.indexOf(' ')); 
      vCon.LastName = email.fromname.substring(email.fromname.indexOf(' ')); 
      vCon.Email = envelope.fromAddress; 
      upsert vCon;
}
   catch (System.QueryException e) {
      System.debug('Query Issue: ' + e);
 }
  result.success = true;
  return result;
}

}

Check this link for more details about inbound email service - https://developer.salesforce.com/page/Force.com_Email_Services
Blog post - http://blog.jeffdouglas.com/2010/03/12/writing-an-inbound-email-service-for-salesforce-com/