+ Start a Discussion
TrudgeTrudge 

Trigger That Updates a Lookup Field Not Firing When Referencing Same Record During Bulk Upload

I have a trigger that updates a lookup field based on the value of a text field.  It's working great with the bulk uploads I'm doing, except when the bulk uploads reference the same record for lookup.  The lookup field (Customer Product Mix) populates on the first record, but fails to do so on subsequent records only when the same lookup record is referenced.

User-added image

Here is the code we're using, which I thought was bulkified since all other records populate just fine on a bulk upload.  Any help would be greatly appreciated.
 
trigger UpdateCustomerProductMix on Sales_Order_Line_Item__c (before insert, before update ){
 
    Map<String, Sales_Order_Line_Item__c> CPMToSOLIMap = new Map<String, Sales_Order_Line_Item__c>();

    for(Sales_Order_Line_Item__c soli:Trigger.new)
    {
        CPMToSOLIMap.put(soli.Customer_Product_Mix_UploadID_HIDDEN__c, soli);
 
    }
 
    List<Customer_Product_Mix__c> cpmLst = [SELECT ID, Name FROM Customer_Product_Mix__c where Name in :CPMToSOLIMap.keySet()];
 
    for(Customer_Product_Mix__c cpm : cpmLst){
        CPMToSOLIMap.get(cpm.Name). Customer_Product_Mix__c = cpm.ID;
    }
}



 
Best Answer chosen by Trudge
VamsiVamsi
Hi,

Maps don't allow adding up duplicate keys. As a result, you are unable to perform updates on same reference look-up.

You need to reform your code as below.
 
trigger UpdateCustomerProductMix on Sales_Order_Line_Item__c (before insert, before update ){
 
    Set<String> CPMToSOLIMap = new Set<String>();

    for(Sales_Order_Line_Item__c soli:Trigger.new)
    {
        CPMToSOLIMap.add(soli.Customer_Product_Mix_UploadID_HIDDEN__c);
 
    }

Map<string,id> CustomerproductID = new Map<string,id>();
for(Customer_Product_Mix__c cpm : [SELECT ID, Name FROM Customer_Product_Mix__c where Name in :CPMToSOLIMap]) 
{
  CustomerproductID.put(cpm.Name,cpm.id);
}
    
 
    for(Sales_Order_Line_Item__c sol : trigger.new)
  {
   
    if(CustomerproductID.containsKey(sol.Customer_Product_Mix_UploadID_HIDDEN__c))
     {
           sol.Customer_Product_Mix__c = CustomerproductID.get(sol.Customer_Product_Mix_UploadID_HIDDEN__c);
      }
  }
}

Hope this helps ...!!!
 

All Answers

VamsiVamsi
Hi,

Maps don't allow adding up duplicate keys. As a result, you are unable to perform updates on same reference look-up.

You need to reform your code as below.
 
trigger UpdateCustomerProductMix on Sales_Order_Line_Item__c (before insert, before update ){
 
    Set<String> CPMToSOLIMap = new Set<String>();

    for(Sales_Order_Line_Item__c soli:Trigger.new)
    {
        CPMToSOLIMap.add(soli.Customer_Product_Mix_UploadID_HIDDEN__c);
 
    }

Map<string,id> CustomerproductID = new Map<string,id>();
for(Customer_Product_Mix__c cpm : [SELECT ID, Name FROM Customer_Product_Mix__c where Name in :CPMToSOLIMap]) 
{
  CustomerproductID.put(cpm.Name,cpm.id);
}
    
 
    for(Sales_Order_Line_Item__c sol : trigger.new)
  {
   
    if(CustomerproductID.containsKey(sol.Customer_Product_Mix_UploadID_HIDDEN__c))
     {
           sol.Customer_Product_Mix__c = CustomerproductID.get(sol.Customer_Product_Mix_UploadID_HIDDEN__c);
      }
  }
}

Hope this helps ...!!!
 
This was selected as the best answer
TrudgeTrudge
It works brilliantly!!!  Thank you so much, Vamsi!

Cheers,
Chad