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
sumit dsumit d 

if email is already exist in existing Contact than not to insert new contact only update

hi All,
i created a batch to bring zendesk users in to salesforce contact now i want if the email of the zendesk users is already exist in any existing contact's email than we should not insert the zendesk user in salesforce contact and just put the zendesk_id in the existing contact and if email not exist in existing salesforce contact than simply insert the zendesk users in salesforce contact.
right now i am inserting zendesk users in salesforce contact.where shouid i put the logic of the existing email if email is existing in salesforce contact.
Any suggestion? how to do it?
my batch class is given below:-
/*
 * Batch to upsert Zendesk Users into salesforce Contacts
*/
public class BatchZendeskToSalesforceCon implements Database.Batchable<String>,Database.AllowsCallouts, Database.Stateful{
    
            // start method to return the list of end points of Zendesk Users
     public Iterable<String> start(Database.BatchableContext BC){
        
        List<Contact> conList = new List<Contact>();
        List<String> endPoints = new List<String>();
         // instance of class ZendeskApis to call Contacts Api
        ZendeskApis.ContactJSON2Apex firstRecords =  ZendeskApis.ZendeskContactApi();
        Map<String, Id> existingContactMap = new Map<String, Id>();
       /*for(Contact con : [Select id,Name, Email, Zendesk_Id__c 
                           from contact 
                           where Email != null AND RecordType.DeveloperName = 'Time_Rack']){
           existingContactMap.put(con.Zendesk_Id__c , con.Id);                       
        }*/
        
       Map<String , Id> mapzendeskIdToAccountId = new Map<String, Id>();
       for(Account acc : [Select id, Zendesk_Id__c 
                          from account 
                          where Zendesk_Id__c != null]){
           mapzendeskIdToAccountId.put(acc.Zendesk_Id__c, acc.Id);                      
       }
          Map<String,Id> mapZendeskEmailToContactId = new Map<String,Id>();
         for(Contact con :[Select id,Name, Email, Zendesk_Id__c 
                           from contact 
                           where Email != null AND RecordType.DeveloperName = 'Time_Rack']){
          mapZendeskEmailToContactId.put(con.Email,con.id); 
        }
         
        conList.addAll(insertContactData(firstRecords, existingContactMap, mapzendeskIdToAccountId, mapZendeskEmailToContactId));
        if(conList.size() >0){
          Database.upsert(conList, false);
        }
        Integer totalRecords = firstRecords.count ;
        if(totalRecords > 100 && 
           firstRecords.next_page != null && 
           firstRecords.next_page != ''){
               totalRecords = totalRecords / 100;
               for(Integer i= 2; i< totalRecords+2; i++){
                   String currentEndPoints = firstRecords.next_page;
                   currentEndPoints = currentEndPoints.substringBefore('?');
                   currentEndPoints = currentEndPoints + '?page='+i;
                   endPoints.add(currentEndPoints);
               }
           }
        return endPoints;
        
    }
            //execute method to upsert Users
     public void execute( Database.BatchableContext BC, List<String> endPoints){
        List<Contact> conList = new List<Contact>();
        Map<String, Id> existingContactMap = new Map<String, Id>();
        /*for(Contact con : [Select id,Name, Email, Zendesk_Id__c 
                           from contact 
                           where Email != null AND RecordType.DeveloperName = 'Time_Rack']){
           existingContactMap.put(con.Zendesk_Id__c , con.Id);                       
        }*/
        Map<String , Id> mapzendeskIdToAccountId = new Map<String, Id>();
       for(Account acc : [Select id, Zendesk_Id__c 
                          from account 
                          where Zendesk_Id__c != null]){
           mapzendeskIdToAccountId.put(acc.Zendesk_Id__c, acc.Id);                      
       }
         Map<String,Id> mapZendeskEmailToContactId = new Map<String,Id>();
         for(Contact con :[Select id,Name, Email, Zendesk_Id__c 
                           from contact 
                           where Email != null AND RecordType.DeveloperName = 'Time_Rack']){
          mapZendeskEmailToContactId.put(con.Email,con.id); 
                               
       }
        ZendeskApis.ContactJSON2Apex ajNextRecords =  ZendeskApis.ZendeskContactNextApi(endPoints[0]);
        conList.addAll(insertContactData(ajNextRecords, existingContactMap, mapzendeskIdToAccountId, mapZendeskEmailToContactId));
        if(conList.size() > 0){
            Database.upsert(conList, false);
        }
        
    }
             // method to insert Users Records into Contact
     public static List<Contact> insertContactData(ZendeskApis.ContactJSON2Apex conData, 
                                                  Map<String, Id> existingContactMap,
                                                  Map<String, Id> existingAccountMap,
                                                  Map<String, Id> mapZendeskEmailToContactId){
        List<Contact> conList = new List<Contact>();
        //ZendeskApis.ZendeskContactApi();
        for(ZendeskApis.ContactUsers conWrapData :  conData.users){
            Contact con = new Contact();
            /*if(existingContactMap.containsKey(String.valueOf(conWrapData.id))){
              con = new Contact(Id = existingContactMap.get(String.valueOf(conWrapData.id)));
            }*/
            if(existingAccountMap.containsKey(conWrapData.organization_id) ){
                    con.AccountId = existingAccountMap.get(conWrapData.organization_id);
            }    
            if(mapZendeskEmailToContactId.containsKey(conWrapData.email)){
                con.Zendesk_Id__c = String.valueOf(conWrapData.id);
            }
            con.lastName = conWrapData.name;
            if(conWrapData.id != null){
                con.Zendesk_Id__c = String.valueOf(conWrapData.id);
            }
            con.Zd_url__c = conWrapData.url;
            con.Email = conWrapData.email;
            if(conWrapData.created_at != null){
                con.Zd_Created_at__c = Date.valueOf(conWrapData.created_at);
            }
            if(conWrapData.updated_at != null){
                con.Zd_Updated_at__c = Date.valueOf(conWrapData.updated_at);
            }
            con.Zd_time_zone__c = conWrapData.time_zone;
            con.Zd_iana_time_zone__c = conWrapData.iana_time_zone;
            if(conWrapData.phone != null){
                con.Phone = String.valueOf(conWrapData.phone);
            }
            if(conWrapData.shared_phone_number != null){
                con.Zd_Shared_phone_number__c = Boolean.valueOf(conWrapData.shared_phone_number);
            }
            if(conWrapData.photo != null){
                con.Photo__c = String.valueOf(conWrapData.photo);
            }
            con.locale_id__c = conWrapData.locale_id;
            con.locale__c = conWrapData.locale;
            if(conWrapData.organization_id != null){
                con.Zd_Organisation_id__c = String.valueOf(conWrapData.organization_id);
            }
            con.Zd_role__c = conWrapData.role;
            con.Zd_verified__c = conWrapData.verified;
            con.External_Id__c = conWrapData.external_id;
            if(conWrapData.tags != null){
                con.Zd_Tags__c = String.valueOf(conWrapData.tags);
            }
            con.Alias__c = conWrapData.alias;
            con.Active__c = conWrapData.active;
            con.Zd_Shared__c = conWrapData.shared;
            con.Zd_Shared_agent__c = conWrapData.shared_agent;
            if(conWrapData.last_login_at != null){
                con.Zd_last_login_at__c = Date.valueOf(conWrapData.last_login_at);
            }
            if(String.valueOf(conWrapData.two_factor_auth_enabled) != null && 
               String.valueOf(conWrapData.two_factor_auth_enabled) != ''){
                  con.Zd_two_factor_auth_enabled__c = conWrapData.two_factor_auth_enabled;
               }
            con.Zd_Signature__c = conWrapData.signature;
            con.Zd_details__c = conWrapData.details;
            con.Notes__c = conWrapData.notes;
            con.Role_Type__c = conWrapData.role_type;
            con.Custom_Role_id__c = conWrapData.custom_role_id;
            con.Moderator__c = conWrapData.moderator;
            con.Zd_ticket_restriction__c = conWrapData.ticket_restriction;
            con.Only_private_Comments__c = conWrapData.only_private_comments;
            con.Zd_Restricted_Agent__c = conWrapData.restricted_agent;
            con.Zd_Suspended__c = conWrapData.suspended;
            con.Chat_only__c = conWrapData.chat_only;
            con.Zd_default_group_id__c = conWrapData.default_group_id;
            con.Zd_report_csv__c = conWrapData.report_csv;
            if(conWrapData.user_fields != null){
                con.Zd_user_fields__c = String.valueOf(conWrapData.user_fields);
            }
            conList.add(con);    
        }
        return conList;
    }
    public void finish(Database.BatchableContext BC){
        
    }
 }
how to do it?
Any suggestions?
Sfdc UserSfdc User
Hi,

Could you explain your START, EXECUTE and FINISH methods? 
On which line you have access to the list of zendesk users?