You need to sign in to do that
Don't have an account?
urgent!! Trigger to update Project State field on parent object
Hi All,
I have two object in the related list and if Approal Status on both the object changes to "Approved " then the Project_State__c field on the parent object changes to "Closed".
I have written the trigger but It's not working...please help me
trigger Update_Project_State on Project__c (before update) {
for(Project__c Prj:trigger.New){
List<Quality_of_Delivery__c> Lstqod= new LIST<Quality_of_Delivery__c>();//object-1 MD relation
List<Root_Cause__c> LstRC= new LIST<Root_Cause__c>(); //object-2 lookup
Lstqod=[Select project__c,Final__c,Approved_Status__c from Quality_of_Delivery__c where project__c=: prj.Id];//Query qod
LstRC=[Select Project_Name__c,Final__c,Approval_Status__c from Root_Cause__c where Project_Name__c=: j.Id];
List<Project__c> prjupdate= new List<Project__c>();
if(LstRC!=null){ for(Root_Cause__c r:LstRC){
if(r.Approval_Status__c=='Approved' && r.Final__C==True)
{
if(Lstqod!=null){
for(Quality_of_Delivery__c q:lstqod) {
if(q.Approved_Status__c=='Approved' && q.Final__c==True){
if(prj.Project_State__c=='Active'){
prj.Project_State__c='Closed';
prjupdate.add(prj);
}
else if(prj.Project_State__c=='In Cancellation')
{
prj.Project_State__c='Cancelled';
prjupdate.add(prj);
}
}
}
update prjupdate;
}
}
}
}
}
}
Since your trigger logic needs to be fired once the approval status of the child record is set to approved, then your trigger should run from the child object, not the parent. I've written up some code for you that should help you understand this:
Let me know if you have any questions.
if the approval status on both the object changes to approved the the project_State__c changes to closed
You have one Parent object (Project__c) and two child objects (Quality_of_Delivery__c, Root_Cause__c), so you need to write trigger on both the child objects.
Try this.