I have a master detail relationship between Installation__c and Account. and relation 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.

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){
        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;
        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)){
  • 1
    Your second query, the one on Account looks very strange to me. Since your two objects are in a relationship (the type doesn't really matter), shouldn't you be querying Accounts based on the Site__c field you told us about (rather than whether the time zone field is null)? The nested for loop you have is also likely a semantic error. You're iterating over all your Installations, and then all your queried Accounts, and setting time zone regardless of whether the Installation is related to that particular Account. You'd be getting "duplicate id in list" if you had any accounts to update.
    – Derek F
    Feb 26 '19 at 12:42
  • There's a way to avoid the second query altogether, but for now I think you should focus on what your requirements are, and evaluating whether this code meets those requirements or not. Another thing to consider is how will you deal with multiple Installation__c records being related to a single Account. Do you have something in place to prevent that situation? If you don't have questions about the questions I've asked, then you should edit your question to include additional information.
    – Derek F
    Feb 26 '19 at 12:45

There are too many things in your code that needs to be corrected.

  • There is no need to query again for Installation as you will get those records as parameter from your trigger i.e. as Trigger.New
  • You are querying all the Accounts where Time_Zone__c is null which is incorrect, instead you should get the AccountId's from Installation object and query only those records which are related to the Installation records which are in transaction. Or else, you can update the Accounts without having to query (as long as you have Id of Account) as shown in the code below.
  • You are looping through all the Account records and assigning the Time_Zone__c of random Installation's record to Account's Time_Zone__c field without checking if it the correct Account that is related to that respective Installation.

I would suggest you to follow below steps in order to meet your requirements

  1. Looping through the Trigger.New variable that is passed as parameter.
  2. Checking if it assigned to an Account and also checking if the value of Time_Zone__c is not null or not empty.
  3. Then creating an instance of Account with Id from Installation object or getting it from map as you might have multiple Installation's associated to same Account which might result in duplicates in List or you should add a business logic on how to deal if multiple Installation's with different Time_Zone__c are associated to single Account. The code below, updates the Time_Zone__c with the latest associated Installation's Time_Zone__c in the list.

Below code should help you in getting started with the things

public class AccountTimeZoneUpdate{
    public static void accountTimeZone(List newlist){
        Map<Id, Account> accountstoUpdate = new Map<Id, Account>();
        for(Installation__c install : newlist){
            if(install.Site__c!=null && install.Time_Zone__c != null && && install.Time_Zone__c != ''){
                Account acc = accountstoUpdate.containsKey(install.Site__c) ? accountstoUpdate.get(install.Site__c) : new Account(Id = install.Site__c);
                acc.Time_Zone__c = install.Time_Zone__c;
                accountstoUpdate.put(acc.Id, acc);
        update accountstoUpdate.values();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.