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
Zoom_VZoom_V 

Creating Child records from values in multi-value fields

I apologize for a duplicate entry here. I made this entry earlier but my code has changed and I was unable to edit the previous entry.

 

I have a list ('names') which is being created by a VF page which contains the Names of other records. I am attempting to create a new child record (object : Product_Affected_Entry__c) for each of the entries in that list. That is working properly.

However, I am also attempting to query each record which those entries represents and place the recordID into the Vendor_Product__c field of the child record I am creating - and that field is only being populated in the first child record created.

I believe the problem may be in this portion of the trigger :
 

// get the ids for all vendor products and store in a map keyed by name
           Map<String, Id> subAccIdsByName=new Map<String, Id>();
System.debug('FIRSTsubAccIdsByName='+subaccNames);   
           for (Vendor_Product__c subAcc : [select id, 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);
                   }

System.debug('SECONDsubAccIdsByName=' + subAccIdsByName);
The FIRSTsubAccIdsName debug is producing :
 
FIRSTsubAccIdsByName={}

But the SECONDsubAccIdsName debug is only producing :
 
SECONDsubAccIdsByName={Red=a7v5600000000O3AAI}
And later this portion of the trigger :
 
String temp = newContract.Products_Affected3__c;
                 temp = temp.replace(']','');
                 temp = temp.replace('[','');
                 String[] all = temp.split(',');

                 for(String productsonpolicy: all)

                 {

                    productsonpolicy = productsonpolicy.normalizeSpace();
System.debug('productsonpolicy'+productsonpolicy);
                    //for(String productsonpolicy: newContract.Products_Affected3__c.split(',')){

                    Product_Affected_Entry__c ssoc = new Product_Affected_Entry__c(

                            //Name = productsonpolicy,
                            Policy__c = newContract.Id,
                            Vendor__c = newContract.Vendor__c,
                            Vendor_Product__c = subAccIdsByName.get(productsonpolicy), // GET THE SUB ACCOUNT ID BASED ON NAME
                            Policy_and_Product__c = newContract.Name + '~' + subAccIdsByName.get(productsonpolicy)); 
                            //Contract_Start_Date__c = newContract.Contract_Start_Date__c,
                           // Contract_End_Date__c = newContract.Contract_End_Date__c,
                           // Logo_Usage_Allowed__c = 'Yes');

                    subs.add(ssoc);
                 }
              } 
           }

           upsert subs ;

The 'productsonpolicy' debug is passing through multiple times and properly producing a properly stripped single value each time.

Here is the full trigger :
 
trigger AutoCreateSubsServOnContrOv on Policy_Profile__c (After insert, after update) 
        {
           List<Product_Affected_Entry__c> subs = new List<Product_Affected_Entry__c>();

           // get the full list of sub account names for all records being processed by the trigger

           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 = temp.replace(']','');
                 temp = temp.replace('[','');
                 String[] all = temp.split(',');
                 subAccNames.addAll(all);
                 for (String acctName : all) 
System.debug('acctname ' + acctName);
              }
           }

           // get the ids for all vendor products and store in a map keyed by name
           Map<String, Id> subAccIdsByName=new Map<String, Id>();
System.debug('FIRSTsubAccIdsByName='+subaccNames);   
           for (Vendor_Product__c subAcc : [select id, 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);
                   }

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 subsoncontract: all)
                 {

                    subsoncontract = subsoncontract.normalizeSpace();
System.debug('subsoncontract'+subsoncontract);
                    //for(String subsoncontract: newContract.Products_Affected3__c.split(',')){

                    Product_Affected_Entry__c ssoc = new Product_Affected_Entry__c(

                            //Name = subsoncontract,
                            Policy__c = newContract.Id,
                            Vendor__c = newContract.Vendor__c,
                            Vendor_Product__c = subAccIdsByName.get(subsoncontract), // GET THE SUB ACCOUNT ID BASED ON NAME
                            Policy_and_Product__c = newContract.Name + '~' + subAccIdsByName.get(subsoncontract)); 
                            //Contract_Start_Date__c = newContract.Contract_Start_Date__c,
                           // Contract_End_Date__c = newContract.Contract_End_Date__c,
                           // Logo_Usage_Allowed__c = 'Yes');

                    subs.add(ssoc);
                 }
              } 
           }

           upsert subs ;

        }

I'm really at a loss here. Can anybody give any insight to this ? I really appreciate it.

Thank you.
 



 

Karan ShahKaran Shah
Hi,

This is strange on multiple counts.
1. If your debugging suggests that FIRSTsubAccIdsByName = {}.. this means that your If loop was ineffective. It sggests that Products_Affected3__c was '[]'
2. Even strange, SECONDsubAccIdsByName={Red=a7v5600000000O3AAI}. This is based off on earlier List "subaccNames"
3. Upsert operation rely on the ID or external Id field. If the key is matched multiple times, then an error is generated and the object record is neither inserted or updated
4 Try something similar to below to understand what may be causing the issue.. 
try {
        upsert SObject <fieldname>;  // This line upserts the SObject list with
                                        // the <fieldname> field on the SObject that should be used for       //matching the record that should be upserted.
    } 
    catch (DmlException e) 
    {
        System.debug(e.getMessage());
    }