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
tangotango 

Bulk insert trigger

I was able to get a proof of concept trigger working for single record inserts but I had difficulty with the bulk insert effort.

Trying to insert a custom object record and build a relationship to the Product2 Object based on a common custom field called SKU.

Any advice would be greatly apprecaited.


Code:
trigger GetProduct2 on Custom_Data__c (before insert) {


Map<String, Product2> pm = new Map<String,Product2> ([select Product_SKU__c, Id from Product2 where Product_SKU__c = :trigger.new[0].Product_SKU__c]); 

for (Custom_Data__c P : Trigger.new){
        if(pm.get(P.Product_SKU__c)!= null ) {
            P.Related_Product__c = pm.get(P.Product_SKU__c).id;
            }
    }
}

 

mikefmikef

Start with this post.

Try this:

Code:
trigger GetProduct2 on Custom_Data__c (before insert) {

  Set<String> skus = new Set<String>();
  for(Custom_Data__c cd : Trigger.new){
    if(cd.Product_SKU__c != null){
      skus.add(cd.Product_SKU__c);
    }
  }


Map<String, Product2> pm = new Map<String,Product2> ([select Product_SKU__c, Id from Product2 where Product_SKU__c in : skus]); 

for (Custom_Data__c P : Trigger.new){
        if(pm.containsKey(P.Product_SKU__c)) {
            P.Related_Product__c = pm.get(P.Product_SKU__c).id;
            }
    }
}


 

 

tangotango
Thanks Mike. I can follow what you suggested and it looks much clearer to me then what I had drafted..

If I read my debug window correctly the Set is working and the select seems to populate the Map with one record when I try to insert a single item

Code:
20080905000626.703:Trigger.GetProduct3: line 4, column 3: SelectLoop:LIST:SOBJECT:Custom_Data__c 
20080905000626.703:Trigger.GetProduct3: line 12, column 54: SOQL query with 1 row finished in 17 ms 
20080905000626.703:Trigger.GetProduct3: line 14, column 1: SelectLoop:LIST:SOBJECT:Custom_Data__c 

The related record field does not seem to be updated however.. . so its not finding the right match ?




mikefmikef

You need to debug the map to see what the map looks like and if it's what you need.

Can we assume that your custom field Releated_Product__c is a lookup to Product2?

Code:
trigger GetProduct2 on Custom_Data__c (before insert) {

  Set<String> skus = new Set<String>();
  for(Custom_Data__c cd : Trigger.new){
    if(cd.Product_SKU__c != null){
      skus.add(cd.Product_SKU__c);
    }
  }


Map<String, Product2> pm = new Map<String,Product2> ([select Product_SKU__c, Id from Product2 where Product_SKU__c in : skus]); 
System.debug('the pm map is ' + pm);

for (Custom_Data__c P : Trigger.new){
        if(pm.containsKey(P.Product_SKU__c)) {
            P.Related_Product__c = pm.get(P.Product_SKU__c).id;
            }
    }
}

Now without testing my guess is the map is not populating correctlly.

Try this as well:

Code:
trigger GetProduct2 on Custom_Data__c (before insert) {

  Set<String> skus = new Set<String>();
  for(Custom_Data__c cd : Trigger.new){
    if(cd.Product_SKU__c != null){
      skus.add(cd.Product_SKU__c);
    }
  }


Map<String,Id> pm = new Map<String,Id>);
for(Product2 pro : [select Product_SKU__c, Id from Product2 where Product_SKU__c in : skus]){
  pm.put(pro.Product_SKU__c,pro.Id);
} 

for (Custom_Data__c P : Trigger.new){
        if(pm.containsKey(P.Product_SKU__c)) {
            P.Related_Product__c = pm.get(P.Product_SKU__c);
            }
    }
}


 
But please do the debug first to see what is going on with the map.
 

tangotango
Thanks Mike !

So, yes the map was not populating as we hoped. The Map was keyed by the Product ID not the Product SKU (string).
It also had a copy of the Product Object with the fields used in the select statement.

So the default was this..
Code:
the pm map is {01tR0000000F3dGIAS=Product2:{Product_SKU__c=xxxxxxx, Id=01tR0000000F3dGIAS}} 

When what I needed was Product SKU = Product 2 ID .

Your second recomendation worked great. Using the below created the MAP that I needed to look up and assign the ID.

Code:
Map<String,Id> pm = new Map<String,Id>);
for(Product2 pro : [select Product_SKU__c, Id from Product2 where Product_SKU__c in : skus])
{ pm.put(pro.Product_SKU__c,pro.Id); }


 
Will test the bulk load over the weekend.
Thanks for the help!