[SalesForce] Check if Record Exists before Insert To Avoid Duplicates

Calling all Apex Professionals !

I am attempting to write a requirement in my class that will check if there is already a record in our system before I insert the records so that I will not have duplicate records, however when I run the code as it is now I am not getting any records to load what so ever.. Could anyone take a stab at this for me ??

for (Object vacationRequestWrapper : vacationRequests) {
                Map<String, Object> vacationRequest = (Map<String, Object>) vacationRequestWrapper;
                if (vacationRequest.get('status').equals('approved')) {
                    system.debug('vacationRequests ' + vacationRequest);
                    Map<String, Object> wrapper3 = (Map<String, Object>) vacationRequest.get('creator');

                    Map<String,Object> empValues = new Map<String,Object>(); 

                    string ptoUrl = (String) wrapper3.get('url');

                    empValues.put('startDate',vacationRequest.get('start_date'));
                    empValues.put('endDate',vacationRequest.get('end_date'));
                    empValues.put('ptoId',vacationRequest.get('id'));
                    string ptoId = (string)vacationRequest.get('id');
                    string employeeId = ptoUrl.substring(ptoUrl.length() - 7, ptoUrl.length() - 0);
                    empMap.put(employeeId, empvalues);
                    ptoMap.put(ptoId, empvalues);
                    system.debug('ptoMap ' + ptoMap);
                    system.debug('empMap'+ empMap);
                }
            }
                    Map<String, Object> tempEmpValues = new Map<String, Object>();
                    Map<String,Object> contactVal = new Map<String,Object>();

                    List<Contact> contactList = [SELECT Id, FirstName, LastName, Zenefits_ID__c FROM Contact WHERE Zenefits_ID__c IN : empMap.keySet() LIMIT 200];
                    for (Contact con : contactList) {
                        System.debug('contactList ' + contactList); 

                        tempEmpValues = empMap.get(con.Zenefits_ID__C);
                        contactVal.put('ConId', con.id);
                    }

                        List<Time_Off_Request__c> torList = [SELECT ID, Zenefits_ID__c FROM Time_Off_Request__c WHERE Zenefits_ID__c NOT IN : ptoMap.keySet() LIMIT 200];
                        system.debug('torlist ' + torList);
                        for(Time_Off_Request__c tor1 : torList) {
                            if(tor1.Zenefits_Id__c == (String)ptoMap.get('ptoId')){
                                return Null;
                                 } else {
                                         Time_Off_Request__c tor = new Time_Off_Request__c();
                                         tor.Employee__c =(string)contactVal.get('ConId');
                                         tor.Project__c = 'a8q8A000000CafTQAS';
                                         tor.First_Day_Off__c = date.valueof((string)tempEmpValues.get('startDate'));
                                         tor.Last_Day_Off__c = date.valueof((string)tempEmpValues.get('endDate'));
                                         tor.Status__c = 'Approved';
                                         tor.Zenefits_ID__c = (string)tempEmpValues.get('ptoId');
                                         torToUpdate.add(TOR);
                                         system.debug('torToUpdate' + torToUpdate); 


                                }
                            }                    
                        }
                    }

                }
            return nextUrl;
            }
        }

I have shortened the class to really only show what we aree concerned about, but the list that I am using a SOQL query against that is throwing things off or giving me grief is..

 List<Time_Off_Request__c> torList = [SELECT ID, Zenefits_ID__c FROM Time_Off_Request__c WHERE Zenefits_ID__c NOT IN : ptoMap.keySet() LIMIT 200];

Is the reason why the insert is inserting zero records due to the fact that the torList returns NULL?

At this time I have literally 0 Time_Off_Request__c records in my developer SB so it should return null, but then it should go ahead and create the Time_Off_Request__c records for all oother records.

Basically I need the code to search my system's Time_Off_Request__c records and check if the Zenefits_Id__c is equal to the ptoId (which is the Key in the ptoMap) and if they do match then I do not want those records to be inserted, but for all records that do not have a match I want the records to be inserted

Best Answer

You want to look at the removeAll() method for Sets.

First create a set of all the possible Ids you want to insert, then query the Contact object with records that already exist. Then you can use the removeAll method to remove all instance of the Id that already exist in Salesforce.

Set<String> myIds =  new Set<String>{'a', 'b', 'c'};

Contact[] cList = [Select Id,External_Id__c From Contact];

Set<String> exstIds = new Set<String>();

for(Contact c : cList)
{
    exstIds.add(c.External_Id__c);
}

myIds.removeAll(exstIds); //remove instances of existing ids, and myIds will only have elements that are left
Related Topic