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
Pedro Garcia GPedro Garcia G 

trigger update if is duplicated and insert is not

Hi...

I'm inserting data from apex class. It's connected to an API and pull the applicants' data.

The applicant should be new or duplicated. The new applicants must be inserted and the duplicated applicants must be updated. I'm using HEDA and also create opportunities for requested courses.

I've written the following Trigger but it doesn't work properly. This avoids inserting new applicants.

How could I do?
 
trigger casContact on Contact (before insert, before update, after insert, after update) {
     
    if(Trigger.isBefore){
        if(Trigger.isInsert){
            
            Map<String, Contact> casIdMap = new Map<String, Contact>();
            
            for(Contact cc : Trigger.New){
                
                System.debug('CASSNEWINSERT'+cc.Cas_Id__c);

                // Make sure another new contact isn't also a duplicate 
   
                if (casIdMap.containsKey(cc.Cas_Id__c)) {
                    cc.Cas_Id__c.addError('Another new Contact is duplicated');
                } else {
                    casIdMap.put(cc.Cas_Id__c, cc);
                }
                
            }
            
            System.debug('CASSNEWSIZE '+Trigger.New.size());
            
            if(casIdMap.size() > 0){
                
                for(Contact ccs : [Select id, cas_id__c, Name from Contact where cas_id__c in: casIdMap.keySet() ]){
                    
                    Contact newContact = casIdMap.get(ccs.cas_id__c);
        			
                    newContact.Cas_Id__c.addError('A Contact is already exists');
                }

            }
            
        }
	}
	
}

Thanks
Pedro


 
Maharajan CMaharajan C
Hi Pedro,

Based on the above code i have seen only you are trying to stop the duplicating record creation. am not seeing updating any existing records  if duplicate is found in salesforce. so please tell me properly.

If you want to stop the record creation only based on duplicate found and not wamt to updating the existing duplicate then use the below trigger otherwise let know your expectation:

trigger casContact on Contact (before insert, before update, after insert, after update) {
     
    if(Trigger.isBefore){
        if(Trigger.isInsert){
            
            set<String> casIdSet = new set<String>();
            
            for(Contact cc : Trigger.New){
                
                System.debug('CASSNEWINSERT'+cc.Cas_Id__c);

                // Make sure another new contact isn't also a duplicate 
   
                if (casIdSet.contains(cc.Cas_Id__c)) {
                    cc.addError('Another new Contact is duplicated');
                } else {
                    casIdSet.add(cc.Cas_Id__c);
                }
                
            }
            
            
            if(casIdSet.size() > 0){
            
                List<Contact> existConList = [Select id, cas_id__c, Name from Contact where cas_id__c in: casIdSet];
                set<String> existcasIdSet = new set<String>();
                if(existConList.size() > 0)
                { 
                    for(contact con : existConList)
                    {
                        existcasIdSet.add(con.cas_id__c);
                    }
                }
                
                if(existcasIdSet.size() > 0)
                {
                    for(contact c : Trigger.New)
                    {
                        if(existcasIdSet.contains(c.cas_id__c))
                        {
                            c.addError('A Contact is already exists');
                        }
                    }
                }

            }
        }
    }
}


Thanks,
Maharajan.C
Pedro Garcia GPedro Garcia G
Thanks Maharajan for your quick reply.

As I've mentioned, my goal is to update the existing contact and insert the new one.

My code stops to insert everything, even the new contact. So, how could I make it?

Thanks,
Pedro