+ Start a Discussion
Ryan McNeelyRyan McNeely 

How to update Grandparent field based on Grandchild field value; also appending to text field with several field values

Application Profile - grandparent
Application - parent
Country Availability - grandchild

I want to populate Application_Profile__c.Locales__c with Country_Availability__c.Locale__c

You'll notice:   += record.Locale__c + ";"
This is because most Application Profiles will have many Country Availability records associated with it. I need to append each Locale.

Expected result:
Application_Profile__c.Locales__c = "en_CA;en_US"

Actual result:
The script runs with no apparent errors, but Application_Profile__c.Locales__c is still a blank text field.
 
try {
    List<CountryAvailability__c> grandchildList = 
        [SELECT Application__r.Name, Locale__c, 
                Application__r.Application_Profile__r.Appscom_Approved_Locales__c
            FROM CountryAvailability__c 
            WHERE fieldX = true];
        
    System.debug('*****' + grandchildList); 
    
    for(CountryAvailability__c record : grandchildList){    
        record.Application__r.Application_Profile__r.Appscom_Approved_Locales__c 
            += record.Locale__c + ";";
    }
    
    System.debug('&&&&&&&' + countryAvailabilityList);  
    update countryAvailabilityList;

} catch(Exception e) {
    System.debug('An unexpected error has occurred: ' + e.getMessage());
}

 
Best Answer chosen by Ryan McNeely
Ryan McNeelyRyan McNeely
try {
   List<CountryAvailability__c> grandchildList =
       [SELECT Application__r.Name, Locale__c,
               Application__r.Application_Profile__r.Appscom_Approved_Locales__c
           FROM CountryAvailability__c
           WHERE Listed_On_Apps_com__c = true AND Application__r.id = 'a0BG000000zqzu7MAA'];
       
   System.debug('*****' + grandchildList);
   
   Map<Id, List<String>> appProfileIdsToLocales = new Map<Id, List<String>>();

   for(CountryAvailability__c record : grandchildList){
       System.debug('###'+record);
   
       record.Application__r.Application_Profile__r.Appscom_Approved_Locales__c
           += record.Locale__c + ';';
       
       Id appProfileId = record.Application__r.Application_Profile__c;

       if (appProfileIdsToLocales.get(appProfileId) == null) {
           appProfileIdsToLocales.put(appProfileId, new List<String>());
       }

       appProfileIdsToLocales.get(appProfileId).add(record.Locale__c);
   }
   
   System.debug('------- appProfileIdsToLocales' + appProfileIdsToLocales);  

   List<Application_Profile__c> appProfilesToUpdate = new List<Application_Profile__c>();

   for (Id appProfileId : appProfileIdsToLocales.keySet()) {
       List<String> locales = appProfileIdsToLocales.get(appProfileId);
       
       Application_Profile__c appProfile = new Application_Profile__c();
       appProfile.Id = appProfileId;
       appProfile.Appscom_Approved_Locales__c = locales + ';';
       appProfilesToUpdate.add(appProfile);
   }

   update appProfilesToUpdate;

} catch(Exception e) {
   System.debug('An unexpected error has occurred: ' + e.getMessage());
}