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
si risi ri 

how to update parent picklist when child picklist changes

Hi!
I have a master detail relationsip between Installation__c and Account. and ralation field between Installation__c  & Account is Site__c(API name).
I have a picklist (Time Zone) value on Account and Installation__c that are identical.
There are 5 values for the picklist.

Whenever the picklist value changes on Installation__c , I want to update the picklist value on Account  by using apex code only. But i'm getting null value at  insert accountstoUpdate; line.
Can anyone help, thanks in advance.

My apex class:
public class AccountTimeZoneUpdate {
       public static void accountTimeZone(List<Installation__c> newlist){ 
         set<Id> insId = new set<Id>();
        for(Installation__c inst : newList){
            insId.add(inst.id); 
        } 
     List<Installation__c> instList = [select id,Time_Zone__c Name from Installation__c];
          system.debug('installation list:'+instList);
           
           List<Account> accList = [select id,Time_Zone__c,name from  Account where Time_Zone__c =: null];
           
               system.debug('installation list:'+instList);
           List<Account> accountstoUpdate = new List<Account>();
          
           for(Installation__c insVar:instList){
               for(Account a: accList){
         //  Account acc = new Account();
              if(insVar.Time_Zone__c != null && insVar.Time_Zone__c != '') {
                   a.Time_Zone__c = insVar.Time_Zone__c;
                   accountstoUpdate.add(a);
              }
         }  
     }
            insert accountstoUpdate;
           system.debug('accountstoUpdate time:'+accountstoUpdate);
  }
}

My trigger:
trigger InstallationTrigger on Installation__c (after insert, after update) {
       if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
            AccountTimeZoneUpdate.accountTimeZone(trigger.new);
        }
    
Best Answer chosen by si ri
Sami ShakithSami Shakith
Hi Siri,
 
public class AccountTimeZoneUpdate {
       public static void accountTimeZone(List<Installation__c> newlist){ 
         map<Id,string> IdValueMap = new map<Id,string>();
        for(Installation__c inst : newList){
			if(inst.Site__c!=null)
				IdValueMap.put(inst.Site__c,inst.Time_Zone__c); 
        } 
     List<account> AccountList = new List<account>();
	 for(account acc:[select id from account where id in:IdValueMap.keySet()])
	 {
		acc.Time_Zone__c = IdValueMap.get(acc.id);
		AccountList.add(acc); 
	 }
          
      update AccountList;     
     }   
}

Try this and if that helps you please mark this answer as best. Thanks

All Answers

Sami ShakithSami Shakith
Hi Siri,
 
public class AccountTimeZoneUpdate {
       public static void accountTimeZone(List<Installation__c> newlist){ 
         map<Id,string> IdValueMap = new map<Id,string>();
        for(Installation__c inst : newList){
			if(inst.Site__c!=null)
				IdValueMap.put(inst.Site__c,inst.Time_Zone__c); 
        } 
     List<account> AccountList = new List<account>();
	 for(account acc:[select id from account where id in:IdValueMap.keySet()])
	 {
		acc.Time_Zone__c = IdValueMap.get(acc.id);
		AccountList.add(acc); 
	 }
          
      update AccountList;     
     }   
}

Try this and if that helps you please mark this answer as best. Thanks
This was selected as the best answer
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!


Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
public class AccountTimeZoneUpdate {
    
    public static void accountTimeZone(List<Installation__c> newlist){ 
        
        List<Account> accountstoUpdate = new List<Account>();
        
        Set<Id> insId = new Set<Id>();
        for(Installation__c inst : newList){
            insId.add(inst.Site__c); 
        } 
        
        Map<id, Account> accList = new Map<id, Account>([SELECT Time_Zone__c FROM Account WHERE Id in :insId]);
        
        for (Installation__c insVar : newlist) {           
            if(insVar.Time_Zone__c!=null && insVar.Time_Zone__c != null && insVar.Time_Zone__c != ''){ 
                Account a = accList.get(insVar.Site__c);
                a.Time_Zone__c = insVar.Time_Zone__c;
                accountstoUpdate.add(a);  
            }
        }
        if(accountstoUpdate.size()>0){
            UPDATE accountstoUpdate;
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
si risi ri
Hi! Khan Anas  
   I have tried your code but, encountering the following error...
Problem found when updating the installation.
InstallationTrigger: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 001q0000013XreyAAC Class.AccountTimeZoneUpdate.accountTimeZone: line 53, column 1 Trigger.InstallationTrigger: line 22, column 1
Sami ShakithSami Shakith
Hi Siri

Have tried the code i chattered you?
public class AccountTimeZoneUpdate {
    public static void accountTimeZone(List<Installation__c> newlist){ 
    map<Id,string> IdValueMap = new map<Id,string>();
        for(Installation__c inst : newList)
        {
            if(inst.Site__c!=null)
                IdValueMap.put(inst.Site__c,inst.Time_Zone__c); 
        } 
        
        List<account> AccountList = new List<account>();
        
        for(account acc:[select id from account where id in:IdValueMap.keySet()])
        {
            acc.Time_Zone__c = IdValueMap.get(acc.id);
            AccountList.add(acc); 
        }

        update AccountList;     
    }   
}

Try this and let know is that help you or not.