+ Start a Discussion
Swathi CseSwathi Cse 

Email services

How to write code for inserting the and if duplicate are there that has to be updated in email servies.
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 = '';
      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/