You need to sign in to do that
Don't have an account?
Is it possible to update the status on a parent record based on the status of multiple child object records with a lookup relationship?
Hi everyone,
I am very new to Apex (as in completed the introductory Apex Trailhead badge a few days ago), so apologies if I say anything stupid or that doesn't make sense.
I have a custom object called Rulecloseout__c and a second custom object called Rule_change_milestone__c which has a lookup relationship to Rulecloseout__c.
There will be multiple milestones for each rulecloseout.
Each milestone has a status, and I want to update the parent Rulecloseout's status based on these (and every time they're edited).
So if all child milestones for a rulecloseout record are updated to be either Completed or No longer required (picklist values), then I want the Rulecloseout status to automatically be updated to Completed.
I've done quite a bit of reading, and the only possible solution I can find is an Apex trigger rollup summary of all the child milestone statuses on the Rulecloseout, and then a formula field for the status using the contains function.
1) Will this work?
2) Could someone please help me with what I need to put in the Apex Trigger?
You probably think I'm trying to run before I can walk, which is true! But I am going to go away and do the Trailhead units on Apex Triggers in the meantime.
Thanks in advance for your help.
Rowena
The trigger layout - to handle insert, edits and deletion of the child record. It is after as the saved child needs to be read to update the parent. The Class
As noted, the purpose was to update the Work Order if none of the associated skills were of type "site induction".
From notes - SiteInductionCheck__c is a formula number field on the Skill Requirement object testing for 'site induction' in the Name field (value being 1 or 0 )
So I used the aggregrate function to sum the value int records and then update a field on the Service Appointment. I then had validation on teh Work Order to raise an alert in that interface. Based on the value being equal to 0 (zero).
Instead of the aggregate function, you could loop your collection of child records testing the status and then escape if a "non closing" status was seen before end of loop. On escape, don't update the parent record. If you complete the loop, update the field in the parent record. Alternately, set a boolean at the start of the loop and then update if a non closing status is tested.
Psuedo
Boolean closeTheParent = True
Loop
If status <> "closed status"
Update closeTheParent = False
End Loop
If Boolen closeTheParent
Update parent
Else
//do nothing - don't update parent.
I'm not sure what you mean by using a formula using contains.
HTH
Regards
Andrew