i am creating three child records when a record is created on parent object using process builder. now, i am writing a trigger to update 'status__c = closed' field on parent object when status of three child record's is marked as 'completed'. below is my apex method
public class StatusClosedHandler {
public static void statuschange(List<Id> 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[i] ]){
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);
}
}
update srequest;
}
}
and my trigger to call this method is :
trigger StatusClosedfinal on Service_Line__c (after update) {
if(Trigger.isAfter && Trigger.isUpdate){
List<Id> sLine = new List<Id>();
for(Service_Line__c i:trigger.new) {
sLine.add(i.Id);
}
StatusClosedHandler.statuschange(sLine);
}
}
when i am running method in anonymous window and passing parent id as a list, it's updating the status, but not while runnig the trigger. please help
:RecordId[i]
, what isi
? don't do SOQLs inside a loop (as part of the for its ok, but the[i]
would suggest you are iterating somewhere and increasing the value ofi