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 

update picklist value of one object based on other object's picklist value

I am trying to update a Picklist value "status ='Closed'" of a custom object Service_Request__c based on Picklist value "Status = 'Completed' on Service_Line__c record, basically this should happen after I update Status = 'Completed' on Service_Line__c. Service_Line__c Custom Object has a Master-Detail with Service_Request__c. Thanks in Advance.
AnudeepAnudeep (Salesforce Developers) 
Hi Mayank, 

You can write a parent child SOQL query to meet your requirement. Below is the sample code, please make changes as required
 
for(Service__Request__c sr: [Select id, Status, (select id, Status from Service_Line__r) from Service__Request__c]) {
    for(Service_Line__r sl: sr.Service_Line__r) {
          if(sl.Status == 'Completed') {
           sr.Status = 'Closed';
          }
    }
}
If you find the above infomation helpful, please mark this answer as solved by selecting it as best. It may help others in the community. Thank You!

Anudeep
 
Mayank07Mayank07
Hi Anudeep, Thanks for the reply.
Let me take you through the whole process, in this case first a process builder is created so that when a record is created on Service_Request__c accordingly three records are created on Service_Line__c object(Service Line A, Service Line B, Service Line C) now i want when the status of all three records is set to ''completed'' then only the Status on Service_Request__c will update to 'Closed'.
Hope now things are much clear.
kindly look into the issue
 
viswanath reddy 53viswanath reddy 53
Hi  Mayank Tripathi 30,

It looks like our team of experts can help you resolve this ticket.
We have Salesforce global help-desk support and you can log a case and our Customer Success Agents will help you solve this issue. You can also speak to them on live chat. Click on the below link to contact our help-desk. Trust me it is a support service that we are offering for free!

https://jbshelpdesk.secure.force.com

Thanks,
Jarvis SFDC team
AnudeepAnudeep (Salesforce Developers) 
Hi Mayank, 

The below code will ensure the Service__Request__c status is updated only when all the service_line__c records have the same status
 
List<String> myStrings = new List<String>();
Integer countRecordsWithAllStatus = 0;
Integer countRecordsWithCompletedStatus = 0;
for(Service__Request__c sr: [Select id, Status, (select id, Status from Service_Line__r) from Service__Request__c]) {
    for(Service_Line__r sl: sr.Service_Line__r) {
          countRecordsWithAllStatus = countRecordsWithAllStatus+1;
          if(sl.Status=='Completed') {
          myStrings.add(sl.Status);
          countRecordsWithCompletedStatus = countRecordsWithCompletedStatus+1;
          }
    }

    if(countRecordsWithAllStatus=countRecordsWithCompletedStatus) {
       sr.Status='Completed;
    }
 }
}

Anudeep