You need to sign in to do that
Don't have an account?
tango
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.
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; } } }
Start with this post.
Try this:
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
The related record field does not seem to be updated however.. . so its not finding the right match ?
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?
Now without testing my guess is the map is not populating correctlly.
Try this as well:
But please do the debug first to see what is going on with the map.
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..
When what I needed was Product SKU = Product 2 ID .
Thanks for the help!