You need to sign in to do that
Don't have an account?
Populate a field with a mapped value
With this trigger I am querying each value of a multi-value field and creating a new child record for each value. I am using the field values of the queried record to be field values in the newly created record. One of the values is the Name and the other is the record ID. I now want to take the value of the Vendor__c (which is a lookup field) from that record and plug it into the VendorNEW__c field of the newly created record. Since Vendor__c is a lookup the data type will be an id. But I don't know how to put another value into the map and to carry it down to the newly created record.
trigger AutoCreateSubsServOnContrOv on Policy_Profile__c (After insert, after update) { List<Product_Affected_Entry__c> subs = new List<Product_Affected_Entry__c>(); List<String> subAccNames=new List<String>(); for (Policy_Profile__c newCont : Trigger.New) { if (newCont.Products_Affected3__c != '[]') { // split out the multi-select picklist using a comma delimiter System.debug('Products_Affected3__c ' + newCont.Products_Affected3__c); String temp = newCont.Products_Affected3__c; temp.normalizeSpace(); temp = temp.replace(']',''); temp = temp.replace('[',''); String[] all = temp.split(','); subAccNames.addAll(all); System.debug('************************temp'+temp); for (String acctName : all) { subAccNames.add(acctName.normalizeSpace()); System.debug('subAccNames !!! ' + subAccNames); } } } // get the ids for all vendor products and store in a map keyed by name Map<String, String, Id> subAccIdsByName=new Map<String, String, Id>(); System.debug('FIRSTsubAccIdsByName='+subAccIdsByName); for (Vendor_Product__c subacc : [select id, Vendor__c,Name from Vendor_Product__c where Name in :subAccNames]) { subAccIdsByName.put(subacc.Name, subacc.id); System.debug('subAcc Name and ID=' + subacc.Name +'Id=' + subacc.id + 'Vendor_c=' + subacc.Vendor__c); } System.debug('SECONDsubAccIdsByName=' + subAccIdsByName); //For each position processed by the trigger, add a new //Product_Affected_Entry__c record for the specified Products_Affected3__c. //Note that Trigger.New is a list of all the new positions //that are being created. for (Policy_Profile__c newContract : Trigger.New) { if (newContract.Products_Affected3__c != '[]') { // split out the multi-select picklist using a comma delimiter System.debug('Products_Affected3__c ' + newContract.Products_Affected3__c); String temp = newContract.Products_Affected3__c; temp = temp.replace(']',''); temp = temp.replace('[',''); String[] all = temp.split(','); for(String productsonpolicy: all) { productsonpolicy = productsonpolicy.normalizeSpace(); Product_Affected_Entry__c ssoc = new Product_Affected_Entry__c( Policy__c = newContract.Id, Vendor_Product__c = subAccIdsByName.get(productsonpolicy), VendorNEW__c = ***NEED VENDOR__C FIELD OF QUERY RECORD HERE***** Policy_and_Product__c = newContract.Name + '~' + subAccIdsByName.get(productsonpolicy)); subs.add(ssoc); } } } upsert subs Policy_and_Product__c; }
I have assumed that the value in the Products_Affected3__c is something like VP-0001, VP-0002, VP-0010, VP-0009, VP-0006. (Comma Seperated Values)
I have modified the Trigger and explained it. Please find below the Trigger.
Please let me know if this helps.
If yes, please mark the question as Solved.
Thanks and Regards,
Anirudh Singh
All Answers
In map at line 31, instead of creating String -> String, create map of String -> Vendor Product. Here is the updated trigger. Let me know, if you need any other help.
Thanks,
Neetu
There were a few changes required in the Trigger:
1. I have highlighted the changes I have made in bold with comment starting with my Name.
2. You need to iterate over the subAccIdsByName Map and compare the Name to match the Name from the productsonpolicy. (Important) See underlined comment below.
3. Fetch any of the field values from the subAccIdsByName Map. I have fetched Vendor__c and Id from the Map as required by you.
Do remember to match the Names otherwise you will put wrong values.
Please let me know if this helps.
If yes, please mark the question as Solved.
Thanks and Regards,
Anirudh Singh
Anirudh - thank you very much for your input. It works - but only for the first selection in the list. I'm trying to figure it out. Do you have any ideas ?
Thanks again - I've already learned a great amoount from this.
Please can you claify if Products_Affected3__c is a multi-picklist or a text field containing comma seperated values?
Thanks and Regards,
Anirudh Singh
I have assumed that the value in the Products_Affected3__c is something like VP-0001, VP-0002, VP-0010, VP-0009, VP-0006. (Comma Seperated Values)
I have modified the Trigger and explained it. Please find below the Trigger.
Please let me know if this helps.
If yes, please mark the question as Solved.
Thanks and Regards,
Anirudh Singh
Anirudh - OUTSTANDING !!!!!!
Thank you so much !