You need to sign in to do that
Don't have an account?
CodeHeartsSFDC
Error: Update parent status based on child statuses
Hi Guys,
The functionality i'm trying to perform here is to update the parent object status to complete when all related child object records have a status complete. But i'm receiving the error "Error: Compile Error: Variable does not exist: id at line 11"
I'm pasting the code below. Can you please help me out here. Thanks!!
trigger updateParentStatus on Child_Object__c (after insert, after update) {
Set<Id> parentIds = new Set<Id>();
Integer count = 0;
for(Child_Object__c wd: Trigger.new) {
parentIds.add(wd.Parent_Object__c);
}
List<Parent_Object__c> parentList = [Select id, Status__c from Parent_Object__c where id =: parentIds];
List<Child_Object__c> childList = [Select id, Status__c from Parent_Object__c Where parentIds =: parentList.id];
for(Child_Object__c wd: Trigger.new) {
if(wd.Status__c != 'Completed') {
count++;
}
}
for(Parent_Object__c wo: parentList) {
wo.Status__c = 'Completed';
}
if(count==0) {
update parentList;
}
}
The functionality i'm trying to perform here is to update the parent object status to complete when all related child object records have a status complete. But i'm receiving the error "Error: Compile Error: Variable does not exist: id at line 11"
I'm pasting the code below. Can you please help me out here. Thanks!!
trigger updateParentStatus on Child_Object__c (after insert, after update) {
Set<Id> parentIds = new Set<Id>();
Integer count = 0;
for(Child_Object__c wd: Trigger.new) {
parentIds.add(wd.Parent_Object__c);
}
List<Parent_Object__c> parentList = [Select id, Status__c from Parent_Object__c where id =: parentIds];
List<Child_Object__c> childList = [Select id, Status__c from Parent_Object__c Where parentIds =: parentList.id];
for(Child_Object__c wd: Trigger.new) {
if(wd.Status__c != 'Completed') {
count++;
}
}
for(Parent_Object__c wo: parentList) {
wo.Status__c = 'Completed';
}
if(count==0) {
update parentList;
}
}
Then Add the child query and modify the trigger.new to childList.
Thanks
Anil.B
All Answers
Its the problem with your queries
Remove the child object query as your are not using it and change the parent Query to this Thanks
Anil.B
I had tried the query you had mentioned above previously, but the issue is, that does not check if all the related child records has a status of completed. It updates the parent if any of the child record status is set to complete.
I want the parent status to be set to complete only if all the related child records statuses are set to complete
Then Add the child query and modify the trigger.new to childList.
Thanks
Anil.B
But that would only be the first element in the list.
Note, we are also querying a list, so we would use IN and the the equals .. a single Id would equal a list of Ids
Next, we note that we are bulkifying the trigger. Nice. But why do we bulkify? So that we can do a dataload and have the trigger work correctly. Under the current structure, what would happen if I loaded a file with records for two parent records? All the children would be determined against a single parent or all the parents would have the same status regardless of what their children were actually set to.
So, what to do:
We could do a Map .... Map<Id, List<childrecord>> where the ID is the Id of the parent record, and it has a list of children records. We could then loop through the Map and test the list and update the record if required.
So some code
Note, we could also use the Aggregate function in SOQL as we are simply after a number: And do a size compare to the Aggregate count of all children records Note, that becomes tricky because you can end up with parentIds in one Aggregate result and not the other. (i.e. they are all Completed or none are completed) So your logic needs to be very precise.
Or you could get funky and do a combo on AggregateResult with Map<Id,List<child>> and loop the Aggregate and use the .get method to pull and compare to the Map.
Just some ideas to consider.'
Regards
Andrew
Note, we are also querying a list, so we would use IN and the the equals .. a single Id would equal a list of Ids
should read
Note, we are also querying a list, so we would use IN and not the equals .. a single Id would equal a list of Ids
and in the code
if(toUpdate.size() > ==0) {
should read
if(toUpdate.size() > 0) {
regards
please go through given below code,hope this will help you If it solve your problem please mark it as best answer.
Thanks,