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
Mayank07Mayank07 

how to update parent object's picklist value based on child object's picklist value

I have a master-detail relationship between Service_Request__c(Parent) & Service_Line__c(Child). for each child record there are three records created (Line_Type__c :- Line A Type, Line B Type, Line C Type). there's a field Status__c in the child Object which corresponds to each of the three records created. how to access the relationship between Each Line type record with their status?. here i want to check if for every Line_Type__c the status__c is 'Completed' so that the Parent object's (Service_Request__c) Status can be updated to 'Closed'.  
for(Service_Line__c sl : [Select id, Name, Status__c,Line_Type__c, Service_Request__r.Status__c,Service_Request__r.Name from Service_Line__c ]){
          if( sl.Status__c == 'Completed') {
           sl.Service_Request__r.Status__c = 'Closed';
          }

 
VinayVinay (Salesforce Developers) 
Hi Mayank,

Review below snippet.
 
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();
    }
}

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar