You need to sign in to do that
Don't have an account?
![ckellie ckellie](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
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 : trigger.new){
cid.add(cpli.id);
oid.add(cpli.Opportunity__c);
cplimap.put(cpli.id, cpli);
}
for(Opportunity o : [select id from Opportunity where id =: oid]){
omap.put(o.id, 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, Product_1_Name__r.id from Opportunity where id =: oid]){
if(trigger.isInsert){
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;
cpli.remove(0);
}
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;
cpli.remove(0);
}
}
if(trigger.isUpdate){
for(Customer_Product_Line_Item__c cp : trigger.new){
If(o.Product_1_Name__r.id == cp.id){
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_Name__r.id == cp.id){
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;
}
}
}
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 : trigger.new){
cid.add(cpli.id);
oid.add(cpli.Opportunity__c);
cplimap.put(cpli.id, cpli);
}
for(Opportunity o : [select id from Opportunity where id =: oid]){
omap.put(o.id, 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, Product_1_Name__r.id from Opportunity where id =: oid]){
if(trigger.isInsert){
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;
cpli.remove(0);
}
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;
cpli.remove(0);
}
}
if(trigger.isUpdate){
for(Customer_Product_Line_Item__c cp : trigger.new){
If(o.Product_1_Name__r.id == cp.id){
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_Name__r.id == cp.id){
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 !!
![ckellie ckellie](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
That works. Thank you very much.