Skip to main content
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;

}

 
2 respostas
  1. 13 de set. de 2017, 09:32
    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;

    }

     
0/9000