+ Start a Discussion
Zoom_VZoom_V 

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;
        
        }
Best Answer chosen by Zoom_V
Anirudh SinghAnirudh Singh
Hi,

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.
trigger AutoCreateSubsServOnContrOv on Policy_Profile__c(after insert, after update)
{
    //Since, we need two values from Policy_Profile__c to be populated on the new Product_Affected_Entry__c record.
    //So, taking a Map instead of list. Earlier, List<String> subAccNames=new List<String>();
    //Map of subAccountNames and Policy_Profile__c Record.
    Map<String, Policy_Profile__c> subAccNames=new Map<String, Policy_Profile__c>();
    
    for(Policy_Profile__c newCont: Trigger.New) 
    {
        //Checking of the field is empty or not.
        if(newCont.Products_Affected3__c!=Null && newCont.Products_Affected3__c!='') 
        {
            system.debug('newCont.Products_Affected3__c---->'+newCont.Products_Affected3__c);
            String temp=newCont.Products_Affected3__c.normalizeSpace();
            //temp=temp.replace(']',''); //No need for this if comma seperated values as I have taken.
            //temp=temp.replace('[',''); //No need for this if comma seperated values as I have taken.
            
            //Iterate the number of values in the temp variable by slitting them by comma
            //add put them in subAccNames
            for(String acctName: temp.split(','))
            {
                subAccNames.put(acctName.normalizeSpace(), newCont);
                system.debug('subAccNames !!! '+subAccNames); 
            }
        }
    }
    
    //Take a Map of Name and Record for holding Vendor_Product__c Name and Vendor_Product__c Record.
    Map<String, Vendor_Product__c> subAccIdsByName=new Map<String, Vendor_Product__c>();
    
    //Iterate over the Vendor Product records and create a Map with Venfor Product Id as Key and Vendor Product Record as Value.
    for(Vendor_Product__c subacc: [SELECT Id, Vendor__c, Name FROM Vendor_Product__c WHERE Name IN :subAccNames.keySet()]) 
    {
        //Putting record in place of Id, as value in the map.
        subAccIdsByName.put(subacc.Name, subacc);
        System.debug('subAcc Name and ID='+subacc.Name +'Id='+subacc.id+'Vendor_c='+subacc.Vendor__c);
    }
    
    //This will hold the Product_Affected_Entry__c records to be upserted.
    List<Product_Affected_Entry__c> subs = new List<Product_Affected_Entry__c>();
    
    //Iterating over subAccNames Map.
    //No need to iterate again over Policy_Profile__c records as we have already taken the Policy_Profile__c record in the subAccNames Map.
    //Earlier: for (Policy_Profile__c newContract : Trigger.New)
    for(String ref1: subAccNames.keySet()) 
    {
        //Iterate over the subAccIdsByName Map.
        for(String ref2: subAccIdsByName.keySet())
        {
            //Match if the Name in the subAccNames Map is equal to Name in the subAccIdsByName Map.
            if(ref1==ref2)
            {
                Product_Affected_Entry__c ssoc = new Product_Affected_Entry__c();
                ssoc.Policy__c=subAccNames.get(ref1).Id;
                //Access Vendor Product Id from the Map.
                ssoc.Vendor_Product__c=subAccIdsByName.get(ref2).Id;
                //Access Vendor__c from the Map.
                ssoc.VendorNEW__c=subAccIdsByName.get(ref2).Vendor__c;
                //Access Name from the Map.
                ssoc.Policy_and_Product__c=subAccNames.get(ref1).Name+'~'+subAccIdsByName.get(ref2).Id;
                //Put the records in the subs list.
                subs.add(ssoc);
            }
        }
    }
    
    upsert subs Policy_and_Product__c;
}

Please let me know if this helps.
If yes, please mark the question as Solved.


Thanks and Regards,
Anirudh Singh

All Answers

Neetu_BansalNeetu_Bansal
Hi,

In map at line 31, instead of creating String -> String, create map of String -> Vendor Product. Here is the updated trigger.
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(',');
			
			for( String acctName : temp.split( ',' ))
			{
				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, Vendor_Product__c> subAccIdsByName=new Map<String, Vendor_Product__c>();
	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);
		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('[','');

			for( String productsonpolicy : temp.split(',' ))
			{
				productsonpolicy = productsonpolicy.normalizeSpace();
				
				Product_Affected_Entry__c ssoc = new Product_Affected_Entry__c( Policy__c = newContract.Id,
																				Vendor_Product__c = subAccIdsByName.get(productsonpolicy).Id, 
																				VendorNEW__c = subAccIdsByName.get(productsonpolicy).Vendor__c,
																				Policy_and_Product__c = newContract.Name + '~' + subAccIdsByName.get(productsonpolicy)); 
				subs.add(ssoc);
			}
		} 
	}

	upsert subs Policy_and_Product__c;
}
Let me know, if you need any other help.

Thanks,
Neetu
Anirudh SinghAnirudh Singh
Hi,

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.
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); 
            }
        }
    }
    
    //Anirudh----> Take a Map of Name and Record, instead of Name and Id.
    Map<String, Vendor_Product__c> subAccIdsByName=new Map<String, Vendor_Product__c>();
    System.debug('FIRSTsubAccIdsByName='+subAccIdsByName);   
    
    for (Vendor_Product__c subacc : [select id, Vendor__c, Name from Vendor_Product__c where Name in :subAccNames]) 
    {
        //Anirudh----> Putting record in place of Id, as value in the map.
        subAccIdsByName.put(subacc.Name, subacc);
        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;
            
            //Anirudh----> Added normalizeSpace here instead of inside for loop below
            temp.normalizeSpace();
            temp = temp.replace(']','');
            temp = temp.replace('[','');
            String[] all = temp.split(',');
            
            for(String productsonpolicy: all)
            {
                //Anirudh----> Iterate over the subAccIdsByName Map.
                for(Integer i=0; i<subAccIdsByName.keySet().size(); i++)
                {
                    //Anirudh----> Match if the Name in the productsonpolicy is equal to Name in the 
subAccIdsByName Map.
                    if(productsonpolicy==(new List<String>(subAccIdsByName.keySet()))[i])
                    {
                        Product_Affected_Entry__c ssoc = new Product_Affected_Entry__c(
                            Policy__c = newContract.Id, 
                            //Anirudh----> Access Id from the Map
                            Vendor_Product__c = subAccIdsByName.values()[i].Id, 
                            //Anirudh----> Access Vendor__c from the Map.
                            VendorNEW__c = subAccIdsByName.values()[i].Vendor__c;
                            //Anirudh----> Access Name from the Map.
                            Policy_and_Product__c = newContract.Name + '~' + (new List<String>(subAccIdsByName.keySet()))[i]
                        );
                        subs.add(ssoc);
                    }
                }
            }
        } 
    }
    
    upsert subs Policy_and_Product__c;
}

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
Zoom_VZoom_V

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.

Anirudh SinghAnirudh Singh
Hi,

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
Zoom_VZoom_V
It is a text field containing comma separated values. Thank you Anirudh.
Anirudh SinghAnirudh Singh
Hi,

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.
trigger AutoCreateSubsServOnContrOv on Policy_Profile__c(after insert, after update)
{
    //Since, we need two values from Policy_Profile__c to be populated on the new Product_Affected_Entry__c record.
    //So, taking a Map instead of list. Earlier, List<String> subAccNames=new List<String>();
    //Map of subAccountNames and Policy_Profile__c Record.
    Map<String, Policy_Profile__c> subAccNames=new Map<String, Policy_Profile__c>();
    
    for(Policy_Profile__c newCont: Trigger.New) 
    {
        //Checking of the field is empty or not.
        if(newCont.Products_Affected3__c!=Null && newCont.Products_Affected3__c!='') 
        {
            system.debug('newCont.Products_Affected3__c---->'+newCont.Products_Affected3__c);
            String temp=newCont.Products_Affected3__c.normalizeSpace();
            //temp=temp.replace(']',''); //No need for this if comma seperated values as I have taken.
            //temp=temp.replace('[',''); //No need for this if comma seperated values as I have taken.
            
            //Iterate the number of values in the temp variable by slitting them by comma
            //add put them in subAccNames
            for(String acctName: temp.split(','))
            {
                subAccNames.put(acctName.normalizeSpace(), newCont);
                system.debug('subAccNames !!! '+subAccNames); 
            }
        }
    }
    
    //Take a Map of Name and Record for holding Vendor_Product__c Name and Vendor_Product__c Record.
    Map<String, Vendor_Product__c> subAccIdsByName=new Map<String, Vendor_Product__c>();
    
    //Iterate over the Vendor Product records and create a Map with Venfor Product Id as Key and Vendor Product Record as Value.
    for(Vendor_Product__c subacc: [SELECT Id, Vendor__c, Name FROM Vendor_Product__c WHERE Name IN :subAccNames.keySet()]) 
    {
        //Putting record in place of Id, as value in the map.
        subAccIdsByName.put(subacc.Name, subacc);
        System.debug('subAcc Name and ID='+subacc.Name +'Id='+subacc.id+'Vendor_c='+subacc.Vendor__c);
    }
    
    //This will hold the Product_Affected_Entry__c records to be upserted.
    List<Product_Affected_Entry__c> subs = new List<Product_Affected_Entry__c>();
    
    //Iterating over subAccNames Map.
    //No need to iterate again over Policy_Profile__c records as we have already taken the Policy_Profile__c record in the subAccNames Map.
    //Earlier: for (Policy_Profile__c newContract : Trigger.New)
    for(String ref1: subAccNames.keySet()) 
    {
        //Iterate over the subAccIdsByName Map.
        for(String ref2: subAccIdsByName.keySet())
        {
            //Match if the Name in the subAccNames Map is equal to Name in the subAccIdsByName Map.
            if(ref1==ref2)
            {
                Product_Affected_Entry__c ssoc = new Product_Affected_Entry__c();
                ssoc.Policy__c=subAccNames.get(ref1).Id;
                //Access Vendor Product Id from the Map.
                ssoc.Vendor_Product__c=subAccIdsByName.get(ref2).Id;
                //Access Vendor__c from the Map.
                ssoc.VendorNEW__c=subAccIdsByName.get(ref2).Vendor__c;
                //Access Name from the Map.
                ssoc.Policy_and_Product__c=subAccNames.get(ref1).Name+'~'+subAccIdsByName.get(ref2).Id;
                //Put the records in the subs list.
                subs.add(ssoc);
            }
        }
    }
    
    upsert subs Policy_and_Product__c;
}

Please let me know if this helps.
If yes, please mark the question as Solved.


Thanks and Regards,
Anirudh Singh
This was selected as the best answer
Zoom_VZoom_V


Anirudh - OUTSTANDING !!!!!!

Thank you so much !