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 

unable to update parent field from child

I got 2 objects Service_Request__c(Parent) and Service_Line__c(Child). All I'm trying to do is update a field of parent object from child.based on the following condition.
In the following code, I'm not getting how to update the parent.
please suggest how to proceed.
public class StatusClosedHandler {
    public static void statuschange (String RecordId){
        integer count = 0;
        List<Service_Line__c> sLine = new List<Service_Line__c>();
     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 WHERE Service_Request__c = :RecordId ]){
         if(sl.Status__c == 'Completed'){
             count++; 
    }
             if(count == 3){  
             sl.Service_Request__r.Status__c = 'Closed';   
    }
             sLine.add(sl);
    }
             update sLine;
    }
  }

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Mayank,

Are you getting any errors as such??
 
Regards,
Anutej
Mayank07Mayank07
Hi ANUTEJ,
I am getting no errors so far but my desirable output is also not coming,  like i want to update my parent object field based on child's condition but no positive result is coming
ANUTEJANUTEJ (Salesforce Developers) 
>> I also found an implementation where in when a child record is updated parent record is updated according to the condition:

https://developer.salesforce.com/forums/?id=906F0000000kIzEIAU

>> In case you want a scenario to update the parent record from child record using flow you, please have a look at the below example:

https://developer.salesforce.com/forums/?id=9060G000000UVjrQAG

Can you try looking at the above links once.
Mayank07Mayank07
Thanks ANUTEJ  but my requirement is different from the one you gave me the scenario for.
i am still stuck in updating the parent field.
 
ANUTEJANUTEJ (Salesforce Developers) 
Can you try checking if you are getting any records in the soql query?
Mayank07Mayank07
it's a child-parent query so soql query is fine. after putting the condition i'm not able to update the parent required which is my main concern here
 
ANUTEJANUTEJ (Salesforce Developers) 
Yeah I understand but in the query

Select id, Name, Status__c,Line_Type__c, Service_Request__r.Status__c,Service_Request__r.Name from Service_Line__c WHERE Service_Request__c = :RecordId

I think you need to compare Service_Request__c.id with recordid.

Can you try it.

Do let me know of the results.
SidPSidP
Mayank,

Don't konw why are you trying to put the trigger on the parent object, such logics should alwawys be evaluated from the child. Ex. when a Service_Line__c status is updated to 'Completed', then 'Closed' the status to the parent Service_Request__c object.

Anyways, you can use below code which will work per your question (from the parent object trigger to upate parent). I've updated the code by using coding best practices too. 

Make sure your Service_Request__c trigger is firing on "after update" and you are passing a new map to the handler method. 

Ex.
StatusClosedHandler.statusChange(Trigger.newMap);

Mark it the best answer if it solves your issue
 
public class StatusClosedHandler {

    public static void statusChange (Map<Id, Service_Request__c> serviceReqMap){
	    integer count = 0;
	    List<Service_Request__c> reqList = new List<Service_Request__c> ();
	    	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
	     							WHERE Service_Request__c = :serviceReqMap.keySet()
	     							AND Status__c == 'Completed']){
     		count++;
	        if(count == 3){
	        	Service_Request__c req = serviceReqMap.get(sl.Service_Request__c); // Changing the context
	        	req.Status__c = 'Closed';
	        	reqList.add (req);
		    }
	    }
        update reqList;
    }
}

 
firdoss mohd 1firdoss mohd 1
Hi Mayank,

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