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
Masechaba Maseli 8Masechaba Maseli 8 

Help with trigger to not create records already created

Hi all

I have a trigger that creates new child records from a parent. Parent is called Roll Out and the Child is called Cost Estimate. I would like that should it be refired that it checks for existing records before so as it avoid duplication. The duplicate check should be on the value and the the desitnation. 

Any assistance with the duplication check and also ensuring that the code is bulkified will be appreciated.  
 
rigger AutoCreateCostEstimates on Roll_Out__c (after insert, after update) {

    List<Cost_Estimate__c> interviewers = new List<Cost_Estimate__c>();
    List<string> Temp;


    for (Roll_Out__c newPosition: Trigger.New) {
      if (newPosition.Destinations__c != null && newPosition.Create_Roll_Out__c == True){
            // split out the multi-select picklist using the semicolon delimiter
            for(String destinationlist: newPosition.Destinations__c.split(';')){
                interviewers.add(new Cost_Estimate__c(
                Client_Name__c = newPosition.Client_Name__c,
                Shipment_Value_in_USD__c = newPosition.Shipment_Value_in_USD__c,
                Destination__c = destinationlist,
                Roll_Out__c = newPosition.Id));
                
            }
        }
        
    }
    insert interviewers;
    
}

 
Best Answer chosen by Masechaba Maseli 8
pradeep kumar yadavpradeep kumar yadav
Hi Masechaba,
 
Trigger AutoCreateCostEstimates on Roll_Out__c (after insert, after update) {

    List<Cost_Estimate__c> interviewers = new List<Cost_Estimate__c>();
    List<string> Temp;
    
    Map<ID, Roll_Out__c> mapOfRollOut = new Map<ID, Roll_Out__c>([SELECT ID,(SELECT Destination__c,Shipment_Value_in_USD__c FROM Cost_Estimates__r) FROM Roll_Out__c WHERE ID IN: Trigger.New]);
	Map<ID,Map<String,String>> mapOfCE = new Map<ID,Map<String,String>>();
    
    //Loop on map of fetched Roll_Out__c
    for(ID roID : mapOfRollOut.keySet()) {
        mapOfCE.put(roID, new Map<String,String>());
        if(mapOfRollOut.get(roID).Cost_Estimates__r.size() > 0) {
            for(Cost_Estimate__c ce : mapOfRollOut.get(roID).Cost_Estimates__r)
                mapOfCE.get(roID).put(ce.Destination__c,ce.Shipment_Value_in_USD__c);
        }
            
    }

    for (Roll_Out__c newPosition: Trigger.New) {
      if (newPosition.Destinations__c != null && newPosition.Create_Roll_Out__c == True){
          
            // split out the multi-select picklist using the semicolon delimiter
            for(String destinationlist: newPosition.Destinations__c.split(';')){
                
                if(!mapOfCE.get(newPosition.ID).containsKey(destinationlist)) {
                    
                    interviewers.add(new Cost_Estimate__c(
                    Client_Name__c = newPosition.Client_Name__c,
                    Shipment_Value_in_USD__c = newPosition.Shipment_Value_in_USD__c,
                    Destination__c = destinationlist,
                    Roll_Out__c = newPosition.Id));
                    
                }
                
            }
        }
        
    }
    insert interviewers;
    
}

 

All Answers

pradeep kumar yadavpradeep kumar yadav
Hi Masechaba,
 
Trigger AutoCreateCostEstimates on Roll_Out__c (after insert, after update) {

    List<Cost_Estimate__c> interviewers = new List<Cost_Estimate__c>();
    List<string> Temp;
    
    Map<ID, Roll_Out__c> mapOfRollOut = new Map<ID, Roll_Out__c>([SELECT ID,(SELECT Destination__c,Shipment_Value_in_USD__c FROM Cost_Estimates__r) FROM Roll_Out__c WHERE ID IN: Trigger.New]);
	Map<ID,Map<String,String>> mapOfCE = new Map<ID,Map<String,String>>();
    
    //Loop on map of fetched Roll_Out__c
    for(ID roID : mapOfRollOut.keySet()) {
        mapOfCE.put(roID, new Map<String,String>());
        if(mapOfRollOut.get(roID).Cost_Estimates__r.size() > 0) {
            for(Cost_Estimate__c ce : mapOfRollOut.get(roID).Cost_Estimates__r)
                mapOfCE.get(roID).put(ce.Destination__c,ce.Shipment_Value_in_USD__c);
        }
            
    }

    for (Roll_Out__c newPosition: Trigger.New) {
      if (newPosition.Destinations__c != null && newPosition.Create_Roll_Out__c == True){
          
            // split out the multi-select picklist using the semicolon delimiter
            for(String destinationlist: newPosition.Destinations__c.split(';')){
                
                if(!mapOfCE.get(newPosition.ID).containsKey(destinationlist)) {
                    
                    interviewers.add(new Cost_Estimate__c(
                    Client_Name__c = newPosition.Client_Name__c,
                    Shipment_Value_in_USD__c = newPosition.Shipment_Value_in_USD__c,
                    Destination__c = destinationlist,
                    Roll_Out__c = newPosition.Id));
                    
                }
                
            }
        }
        
    }
    insert interviewers;
    
}

 
This was selected as the best answer
Masechaba Maseli 8Masechaba Maseli 8
Thank you Pradeep Kumar Yadav, that worked like a charm.