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

What more can I do to bulkify this trigger?

The below trigger is to copy record information from the child (Customer Product Line Items) records to fields on the parent (Opportunity) object, which is the max number of records attached to the Opportunity. Each child record has its own set of fields on the Opportunity. I have bulkified this trigger, is there any addition changes that I need to make to bulkify the trigger?

Here is the trigger:
trigger CustomerProductonOpp on Customer_Product_Line_Item__c (After Insert, Before Update) {

    Set<id> cid = new Set<Id>();
    Set<id> oid = new Set<Id>();
    Map<id, Customer_Product_Line_Item__c> cplimap = new Map<id, Customer_Product_Line_Item__c>();
    Map<id, Opportunity> omap = new Map<id, Opportunity>();

    for(Customer_Product_Line_Item__c cpli :{
        cplimap.put(, cpli);


    for(Opportunity o : [select id from Opportunity where id =: oid]){
        omap.put(, o);

    List<Customer_Product_Line_Item__c> cpli = [select id, Product_Field_on_Opportunity__c, Condition_1__c, Condition_2__c, Condition_3__c, Bulk_Density__c,
                                                Defect_Description__c, Capacity__c from Customer_Product_Line_Item__c where id in: cid];
    for(Opportunity o : [select id, from Opportunity where id =: oid]){
        if(o.Product_1_Name__c == null && cpli.size()>0){
            o.Product_1_Name__c = cpli[0].id;
            o.Product_1_Primary_Condition__c = cpli[0].Condition_1__c;
            o.Product_1_Additional_Condition_1__c = cpli[0].Condition_2__c;
            o.Product_1_Additional_Condition_2__c = cpli[0].Condition_3__c;
            o.Product_1_Primary_Shape__c = cpli[0].Shape_1__c;
            o.Product_1_Additional_Shape__c = cpli[0].Shape_2__c;
            o.Product_1_Bulk_Density__c = cpli[0].Bulk_Density__c;
            o.Product_1_Sort_Criteria__c = cpli[0].Defect_Description__c;
            o.Product_1_Capacity__c = cpli[0].Capacity__c;
        if(o.Product_2_Name__c == null && cpli.size()>0){
            o.Product_2_Name__c = cpli[0].id;
            o.Product_2_Primary_Condition__c = cpli[0].Condition_1__c;
            o.Product_2_Additional_Condition_1__c = cpli[0].Condition_2__c;
            o.Product_2_Additional_Condition_2__c = cpli[0].Condition_3__c;
            o.Product_1_Primary_Shape__c = cpli[0].Shape_1__c;
            o.Product_1_Additional_Shape__c = cpli[0].Shape_2__c;
            o.Product_2_Bulk_Density__c = cpli[0].Bulk_Density__c;
            o.Product_2_Sort_Criteria__c = cpli[0].Defect_Description__c;
            o.Product_2_Capacity__c = cpli[0].Capacity__c;

     for(Customer_Product_Line_Item__c cp :{
         If( =={
            o.Product_1_Primary_Condition__c = cp.Condition_1__c;
            o.Product_1_Additional_Condition_1__c = cp.Condition_2__c;
            o.Product_1_Additional_Condition_2__c = cp.Condition_3__c;
            o.Product_1_Primary_Shape__c = cp.Shape_1__c;
            o.Product_1_Additional_Shape__c = cp.Shape_2__c;
            o.Product_1_Bulk_Density__c = cp.Bulk_Density__c;
            o.Product_1_Sort_Criteria__c = cp.Defect_Description__c;
            o.Product_1_Capacity__c = cp.Capacity__c;
         } else
         If( =={
            o.Product_2_Primary_Condition__c = cp.Condition_1__c;
            o.Product_2_Additional_Condition_1__c = cp.Condition_2__c;
            o.Product_2_Additional_Condition_2__c = cp.Condition_3__c;
            o.Product_2_Primary_Shape__c = cp.Shape_1__c;
            o.Product_2_Additional_Shape__c = cp.Shape_2__c;
            o.Product_2_Bulk_Density__c = cp.Bulk_Density__c;
            o.Product_2_Sort_Criteria__c = cp.Defect_Description__c;
            o.Product_2_Capacity__c = cp.Capacity__c;

        update o;
The 'update o' DML statement should be moved out of For loop. Just move it after the next flower bracket to keep it within the IF statement but moving out of for loop.  Otherwise everything looks good !!
That works. Thank you very much.