1

Scenario: 500 records of contacts will be getting to endpoint 'https://nurtureapi.nurturehq.com/sfdc/contacts' Which i want to insert this part i have done. if next time same contacts are found then update it if update == 1 (whose value i am getting at other endpoint). Contacts are matched with email field. if record is not matched then insert it as a new record. my approach:

  • Step 1) getting the contacts
  • Step 2) deserialize it
  • Step 3) make a map of existing contact in which key is 'email' and value is 'contact' object.
  • Step 4) for loop on deserilized object
  • Step 5) check in map if object is null or not by 'get' method of map by passing value of new contacts record's email.
  • Step 6)if object is null then insert it as new record
  • Step 7) if object is not null then update it based on 'update' variable(as said above)

the bugs i am getting:

  • 1) Getting duplicate records
  • 2) If however this is avoided and contacts are in range of 20k-50k. Then making map of this large number i will be getting 'CPU time out error'

Please tell me most feasible way to implement it here is my code,

        global class importcontacts implements Database.allowscallouts
       {
     webservice static void import()
     {
         string orgid = userinfo.getorganizationid();
         string endpoint = 'https://nurtureapi.nurturehq.com/sfdc/token?salesforce_org_id='+orgid;
         System.HTTPRequest request = new System.HTTPRequest();
         request.setTimeOut(120000); 
         request.setMethod('GET');
         request.setEndpoint(endpoint);
         system.debug('Body='+ request.getbody());
         system.HTTPResponse response = new system.HTTP().send(request);
         system.debug('Response:'+ response.getbody());
         orgwrapper oorgwrapper;
         string token,domain;
         try
         {
          oorgwrapper = (orgwrapper)JSON.deserialize(response.getbody(),orgwrapper.class);
          token = oorgwrapper.success[0].token;
          domain = oorgwrapper.success[0].subdomain;
         }
         catch(Exception E)
         {

         } 
         endpoint = 'https://nurtureapi.nurturehq.com/sfdc/rules';
         System.HTTPRequest request1 = new System.HTTPRequest();
         request1.setHeader('X-Auth-Token',token);
         request1.setHeader('Subdomain',domain);
         request1.setTimeOut(120000); 
         request1.setMethod('GET');
         request1.setEndpoint(endpoint);
         system.debug('Body='+ request1.getbody());
         system.HTTPResponse response1 = new system.HTTP().send(request1);
         system.debug('Response:'+ response1.getbody());
         RuleWrapper oRuleWrapper;
         try
         {
          oRuleWrapper = (RuleWrapper)JSON.deserialize(Response1.getbody(), RuleWrapper.class);
         }
         catch(Exception E)
         {

         } 
         integer updatecontact;
         for(lstrule objrule : oRuleWrapper.rules)
         {
          updatecontact = objrule.update_crm_contacts;
          break;
         }
         string key,value;
         map<string,string> rulemap = new map<string,string>();
         for(lstrule objrule : oRuleWrapper.rules)
         {
          key = objrule.nurture_tag;
          value = objrule.salesforce_field;
          rulemap.put(key,value);   
         } 
         endpoint = 'https://nurtureapi.nurturehq.com/sfdc/contacts';
         System.HTTPRequest request2 = new System.HTTPRequest();
         request2.setHeader('X-Auth-Token',token);
         request2.setHeader('Subdomain',domain);
         request2.setTimeOut(120000); 
         request2.setMethod('GET');
         request2.setEndpoint(endpoint);
         system.debug('Body='+ request2.getbody());
         system.HTTPResponse response2 = new system.HTTP().send(request2);
         system.debug('Contacts fetched:'+ response2.getbody());  
         TagsWrapper oTagsWrapper;
         oTagsWrapper = (TagsWrapper)JSON.deserialize(response2.getbody(),TagsWrapper.class);
         List<Contact> lstinsertcontact = new List<Contact>();
         List<Contact> lstupdatecontact = new List<Contact>();
         list<string> stagname = new list<string>();
         map<string,contact> masscon = new map<string,contact>();
         list<contact> con = [select salutation,id,firstname,lastname,mailingpostalcode,phone,MailingCountry,MailingState,MailingCity,Mailingstreet,Description,Department,Birthdate,MobilePhone,fax,email,title from contact];
         for(contact ocont : con)
         {
          masscon.put(ocont.email,ocont);
         }
         system.debug(masscon);
         string field,str; 
         contact oContact;
         id accountid;
         list<account> lstacc;
         contact dupcontact;
         for(lstcontact ocontactWrap : oTagsWrapper.contacts)
         {
             system.debug('first Contact:'+ ocontactWrap);
             system.debug('Contacts Email:'+ ocontactWrap.email);
               contact updatecon = masscon.get(ocontactWrap.email);
              system.debug('Contact Found from map:'+updatecon); 
               lstacc = [select id from account where name like : ocontactWrap.company_name];
               system.debug(lstacc.size());
              for(account obj1 : lstacc)
               {    
                accountid = obj1.id;
                break;
               } 
                dupcontact =  masscon.get(ocontactWrap.email); 
                system.debug('contact from map'+ dupcontact);
          if(dupcontact == null)
          {
               oContact = new contact();
               oContact.salutation = ocontactWrap.salutation;
               oContact.firstname =  ocontactWrap.first_name;
                if(ocontactWrap.last_name != null)
                   oContact.lastname = ocontactWrap.last_name;
                else
                   oContact.lastname = 'NA';
                oContact.AccountId= accountid;
                oContact.email = ocontactWrap.email;
                oContact.Title= ocontactWrap.title ;
                oContact.phone = ocontactWrap.phone;
                oContact.MobilePhone = ocontactWrap.mobile;
                oContact.fax = ocontactWrap.fax;
                oContact.Birthdate = ocontactWrap.birthdate;
                oContact.Department = ocontactWrap.department;
                oContact.Description = ocontactWrap.description;
                oContact.Mailingstreet = ocontactWrap.address;
                oContact.MailingCity =  ocontactWrap.city;
                oContact.MailingState = ocontactWrap.state;
                oContact.MailingCountry = ocontactWrap.country;   
                oContact.phone = ocontactWrap.phone;
                oContact.mailingpostalcode = ocontactWrap.zip;
                if(updatecontact == 1)
                {
                 for(tagdemo obj : ocontactWrap.tags)
                 {
                  stagname.add(obj.name);
                 }
                 for(string rname : stagname)
                 {
                  field = rulemap.get(rname);
                  if(field != null)
                  {
                    if(field.equalsignorecase('title'))
                         oContact.title = rname;
                    else if(field.equalsignorecase('department'))
                         oContact.department = rname;
                    else
                         oContact.Description = rname;
                  }
                 } 
                }
          lstinsertcontact.add(oContact);
         }
         else
         {
             if(updatecontact == 1)
             {   
              dupcontact.salutation = ocontactWrap.salutation;
              dupcontact.firstname =  ocontactWrap.first_name;
              if(ocontactWrap.last_name != null)
              { 
               dupcontact.lastname = ocontactWrap.last_name;
               }
              else
              {
               dupcontact.lastname = 'NA';
              } 
               dupcontact.AccountId= accountid;
                dupcontact.email = ocontactWrap.email;
                dupcontact.Title= ocontactWrap.title ;
                dupcontact.phone = ocontactWrap.phone;
                dupcontact.MobilePhone = ocontactWrap.mobile;
                dupcontact.fax = ocontactWrap.fax;
                dupcontact.Birthdate = ocontactWrap.birthdate;
                dupcontact.Department = ocontactWrap.department;
                dupcontact.Description = ocontactWrap.description;
                dupcontact.Mailingstreet = ocontactWrap.address;
                dupcontact.MailingCity =  ocontactWrap.city;
                dupcontact.MailingState = ocontactWrap.state;
                dupcontact.MailingCountry = ocontactWrap.country;   
                dupcontact.phone = ocontactWrap.phone;
                dupcontact.mailingpostalcode = ocontactWrap.zip;

              if(updatecontact == 1)
              {
                     for(tagdemo obj : ocontactWrap.tags)
                     {
                      stagname.add(obj.name);
                     }

                 for(string rname : stagname)
                 {
                      field = rulemap.get(rname);
                      if(field != null)
                      {
                        if(field.equalsignorecase('title'))
                             dupcontact.title = rname;
                        else if(field.equalsignorecase('department'))
                             dupcontact.department = rname;
                        else
                             dupcontact.Description = rname;
                      }
                 }  
              }
          lstupdatecontact.add(dupcontact);
         }
        } 
      } 
    database.insert(lstinsertcontact,false);
    database.update(lstupdatecontact,false);
 }  
}
0

You could avoid some of the "hoops" your jumping through and the timeout issues by simply using an Upsert call instead of splitting your data into separate insert and update lists, then making the two DML calls.

If you're doing all of this based on the email address of the record, you'll automatically insert the first record found with the new email address. Any records that follow with the same email address, will be updates to that record. Any existing records with that email address with updated. It would appear that all you'd need do is make certain your record has an email address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.