You need to sign in to do that
Don't have an account?
Bryan REVELANT 2
Trigger on Child to update the parent
Hello
I have a trigger that I have a on a child record to update the parent if the conditions are met.. The trigger however isnt writting anythign to the parent.
If child has rejected write rejected.
If the record is submit and not all approved then submit
else approved.
trigger ADRUserApproval on ADRUser__c (after update) {
list<id> ADRSubmittedList = new list<id>();
list<ADR__c> ADRUserList = new list<ADR__c>();
for(ADRUser__c lstADRTrigger : trigger.new){
if(lstADRTrigger.Status__c == 'Submit' || lstADRTrigger.Status__c == 'Approved' || lstADRTrigger.Status__c == 'Rejected')
{
ADRSubmittedList.add(lstADRTrigger.id);
}//*** End of if(lstADRTrigger.Next_Step__c...
}//*** End of for(ADR__c lstADRTrigger : trigger.new){
for(ADR__c lstADRTrigger : [ Select id, Apex_Status__c from ADR__c
where id in :ADRSubmittedList] ){
for(ADRUser__c lstADRUser : trigger.new){
if(lstADRUser.Status__c == 'Submit' || lstADRUser.Status__c == 'Approved' || lstADRUser.Status__c == 'Rejected')
{
// if(lstADRUser.Status__c == 'Submit' && lstADRUser.Status__c == 'Approved' && lstADRUser.Status__c == 'Rejected'){
lstADRTrigger.Apex_Status__c = 'Rejected';
if(lstADRUser.Status__c == 'Submit' || lstADRUser.Status__c == 'Approved'){
lstADRTrigger.Apex_Status__c = 'Submit';
}
else lstADRTrigger.Apex_Status__c = 'Approved';
// Site_Audit_Results__c FindAnswersVar = new Site_Audit_Results__c();
ADR__c ADCUpdates = new ADR__c();
ADCUpdates.Apex_Status__c = lstADRTrigger.Apex_Status__c;
System.debug('lstADRTrigger ' + lstADRTrigger);
ADRUserList.add(ADCUpdates);
System.debug('ADCUpdates ' + ADCUpdates);
update ADRUserList;
//}
}
}
}
}
I have a trigger that I have a on a child record to update the parent if the conditions are met.. The trigger however isnt writting anythign to the parent.
If child has rejected write rejected.
If the record is submit and not all approved then submit
else approved.
trigger ADRUserApproval on ADRUser__c (after update) {
list<id> ADRSubmittedList = new list<id>();
list<ADR__c> ADRUserList = new list<ADR__c>();
for(ADRUser__c lstADRTrigger : trigger.new){
if(lstADRTrigger.Status__c == 'Submit' || lstADRTrigger.Status__c == 'Approved' || lstADRTrigger.Status__c == 'Rejected')
{
ADRSubmittedList.add(lstADRTrigger.id);
}//*** End of if(lstADRTrigger.Next_Step__c...
}//*** End of for(ADR__c lstADRTrigger : trigger.new){
for(ADR__c lstADRTrigger : [ Select id, Apex_Status__c from ADR__c
where id in :ADRSubmittedList] ){
for(ADRUser__c lstADRUser : trigger.new){
if(lstADRUser.Status__c == 'Submit' || lstADRUser.Status__c == 'Approved' || lstADRUser.Status__c == 'Rejected')
{
// if(lstADRUser.Status__c == 'Submit' && lstADRUser.Status__c == 'Approved' && lstADRUser.Status__c == 'Rejected'){
lstADRTrigger.Apex_Status__c = 'Rejected';
if(lstADRUser.Status__c == 'Submit' || lstADRUser.Status__c == 'Approved'){
lstADRTrigger.Apex_Status__c = 'Submit';
}
else lstADRTrigger.Apex_Status__c = 'Approved';
// Site_Audit_Results__c FindAnswersVar = new Site_Audit_Results__c();
ADR__c ADCUpdates = new ADR__c();
ADCUpdates.Apex_Status__c = lstADRTrigger.Apex_Status__c;
System.debug('lstADRTrigger ' + lstADRTrigger);
ADRUserList.add(ADCUpdates);
System.debug('ADCUpdates ' + ADCUpdates);
update ADRUserList;
//}
}
}
}
}
Looks like the reason you're not updating the parent records is because your SOQL query isn't return any. You're attempting to query the ADR__c object using the ID values in ADRSubmittedList, but that list has been populated with ID's from the ADRUser__c. Because of that you'll always get 0 results.
To fix that you'll need to update this line...
...to refer to the parent Id field on the ADRUser__c object. Something like...
I don't see any other reference to that parent Id field in your code, so I'm not sure what it's called.
You've got a few other issues in your example code that will probably crop up once some records are returned and it starts executing more of the code. The biggest thing you'll want to avoid in the future is putting your update call inside of a loop. You'll run into your DML limit in no time like that. :)
Here is an example of how you could simplify/bulkify your code.
I've used a map to associate your business logic of the ADRUser__c status field to each ADR__c record. This makes it easier to update the ADR__c records with the results.
To use this code, you'll need to replace 'Parent_Record_Id__c with the correct parent id field name from ADRUser__c.
Hope that helps! :)