0

I got 2 objects Service_Request__c(Parent) and Service_Line__c(Child). 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;
}

}

0

You need to define a parent object in for loop and then added to parent list and upate this

public class StatusClosedHandler { 
     public static void statuschange    (String RecordId){ 
        integer count = 0;               List<Service_Request__c> srequest = 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 = :RecordId ]){
             if(sl.Status__c == 'Completed'){ 
                   count++;
              } 
              if(count == 3){
                    Service_Request__c sr = new         Service_Request__c();
                     sr.id = sl.Service_Request__c;
                      sr.status__c = 'Closed';
                       srequest.add(sr);
                } 
    }
               If(!srequest.isEmpty())
                      update srequest; 
   }
}
| improve this answer | |
  • Thank you so much for the help, this is exactly i was lookng for I just want to know how do we call this method via apex trigger? – MikeT May 14 at 15:43
  • Classname.methodname – Rijwandeltax May 14 at 16:06
  • I know but how to pass the record id as a parameter in it? – MikeT May 14 at 17:45
  • Classname.methodname( recordid); – Rijwandeltax May 14 at 19:41
  • But here i am passing a string Id which is not possible when calling a method. is it? – MikeT May 14 at 19:43
0

Basically, you need to create a new variable for the Parent and update it.

public class StatusClosedHandler {
public static void statuschange (String RecordId){
    List< Service_Request__c> servRequestList = new List< Service_Request__c>(); //list of Parent
    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){
           Service_Request__c thisService = new Service_Request__c();
           thisService.ID = sl.Service_Request__c;
           thisService.Status__c = 'Closed';  
           servRequestList.add(thisService)
         }
         sLine.add(sl);
 }
 if (!servRequestList.isEmpty()) {
     update servRequestList;
 }
}
| improve this answer | |
  • Thank you so much to you too mate, you're also guided me with the exact answer. wish i could mark yours too as the best answer. – MikeT May 14 at 15:44

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.