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
chowdary marellachowdary marella 

Email service

i have created an email service class, but i need to do..if the record is already exists i need to update,if not i need to create a new record ...based on email id i need to check whether it is already exists or not...can any one give a logic,,kudos for ur help..!!!


 

global class POL_EMAIL implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
       
         string myPlainText= '';
         list<string> lst;
         String emailAddress = envelope.fromAddress;
         myPlainText = email.PlainTextBody;
         System.debug('@@@@@@@@@@@@@@@@@@@'+emailAddress );
         System.debug('@@@@@@@@@@@@@@@@@@@*************'+myPlainText );
          
          
          lst=myPlainText.split(';;');
          system.debug('111111'+lst);
          Member__c e=new Member__c();
          string fname = lst.get(0);
          e.First_Name__c = (fname.substringAfter(':')).trim();
         
          System.debug('@@@@@@@@'+e.First_Name__c);
          
       string name = lst.get(1);
          e.Name = (name.substringAfter(':')).trim();
       
       //   System.debug('@@@@@@@@'+e.Last_Name__c );
          
          
          
          
          string Othername = lst.get(2);
          e.Other_Name__c = (Othername.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Other_Name__c );
          
          string gender = lst.get(3);
          e.Gender__c = (gender.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Gender__c );
          
          string DOB = lst.get(4);
          e.Date_of_Birth__c =date.parse((DOB.substringAfter(':')).trim());
          System.debug('@@@@@@@@'+e.Date_of_Birth__c );
          
          string Occupation = lst.get(5);
          e.Occupation__c = (Occupation.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Occupation__c );
          
          string Nationality = lst.get(6);
          e.Nationality__c = (Nationality.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Nationality__c );
          
          string Language = lst.get(7);
          e.Language__c = (Language.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Language__c );
          
          string Street = lst.get(8);
          e.Street__c = (Street.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Street__c );
          
          string Address2 = lst.get(9);
          e.Address_2__c = (Address2.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Address_2__c );
          
          string SuburbCityTown = lst.get(10);
          e.Suburb_City__c = (SuburbCityTown.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Suburb_City__c );
          
          string StateProvidenceTerritory = lst.get(11);
          e.State_Providence_Territory__c = (StateProvidenceTerritory.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.State_Providence_Territory__c );
          
          string Country = lst.get(12);
          e.Country__c = (Country.substringAfter(':')).trim();
          e.Newsletter_Interest__c=(Country.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Country__c );
          
         string postal = lst.get(13);
          e.Posatl_Zip_Code__c = (postal.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Posatl_Zip_Code__c );
          
       string Mobile = lst.get(14);
          e.Mobile_Number__c = (Mobile.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+  e.Mobile_Number__c );
          
          
          string Skype= lst.get(15);
          e.Skype_Name__c = (Skype.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Skype_Name__c );
          
          
           string Curr= lst.get(16);
          e.CurrencyIsoCode = (Curr.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.CurrencyIsoCode );
          
          string Newsletter= lst.get(17);
          e.Newsletter_Interest__c = (Newsletter.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.Newsletter_Interest__c );
          
          string pol= lst.get(18);
          e.How_did_You_Hear_About_Path_of_Love__c = (pol.substringAfter(':')).trim();
          System.debug('@@@@@@@@'+e.How_did_You_Hear_About_Path_of_Love__c );
          
        string Email1= lst.get(19);
        e.E_Mail__c = (Email1.substringAfter(':')).trim();

         
          insert e;
          System.debug('@@@@@@@@#################'+e);
          
          return result;
      }
  }

IshwarIshwar

Hi,

 

according to ur requirement, u need to check whether a record exists based on email field, if not create new one.

 

So u can put this logic

 

Member__c e = new Member__c();

List<Member__c> mList = new List<Member__c>();

try{

mList = [select id, email__c from Member__c where email__c =:emailAddress ];

}

catch(Exception e)

{}

if(mList.size() == 1)

{

e = mList[0];

}

string Othername = lst.get(2);
e.Other_Name__c = (Othername.substringAfter(':')).trim();

string gender = lst.get(3);
e.Gender__c = (gender.substringAfter(':')).trim();

.

.

.

if(mList.size() == 1)

{

update e;

}

else

{

insert e;

}

 

thanks..

Cory CowgillCory Cowgill

If you set the Email field as an External ID you could simply perform a UPSERT command. Upsert will automatically update or insert a record if the External ID is found in the System. In this case it would be Email.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_upsert.htm