You need to sign in to do that
Don't have an account?

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);
}
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);
}
Try this and if that helps you please mark this answer as best. Thanks
All Answers
Try this and if that helps you please mark this answer as best. Thanks
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.
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
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
Have tried the code i chattered you?
Try this and let know is that help you or not.