You need to sign in to do that
Don't have an account?
Bryan REVELANT 2
Apex Trigger IF Else
Having a bit of an issue with an if else Trigger.
If Rejected --- marked rejected on Parent
If Submitted not all Approved -- Mark Submit
If all Approved -- Approved.
trigger ADRUserTriggerV2 on ADRUser__c (after update) {
list<ADR__c> ListOfADRRecords;
set<Id> PrimaryKeyId = new set<Id>();
for(ADRUser__c ADRUserRecords :trigger.new){
if(trigger.oldmap.get(ADRUserRecords.id).ApproveReject__c != 'Submit'){
if(ADRUserRecords.ApproveReject__c =='Submit' || ADRUserRecords.ApproveReject__c =='Approved' || ADRUserRecords.ApproveReject__c =='Rejected'){
PrimaryKeyId.add(ADRUserRecords.ADR__c);// you child object must having parent object field, put that field Api Name here
}
}
}
if(!PrimaryKeyId.isEmpty()) {
list<ADR__c> listOfParentRecordsToUpdate = [select id, Apex_Status__c from ADR__c where Id =: PrimaryKeyId];
ListOfADRRecords = new list<ADR__c>();
for(ADRUser__c ADRUserRecords :trigger.new){
for(ADR__c ADRRecordTOUpdate : listOfParentRecordsToUpdate) {
if (ADRUserRecords.ApproveReject__c =='Rejected' ) {
ADRRecordTOUpdate.Apex_Status__c = 'Rejected';
} else if (ADRUserRecords.ApproveReject__c =='Submit' || ADRUserRecords.ApproveReject__c =='Approved') {
ADRRecordTOUpdate.Apex_Status__c = 'Submit';
} else {
ADRRecordTOUpdate.Apex_Status__c = 'Approved';
}
ListOfADRRecords.add(ADRRecordTOUpdate);
Update ListOfADRRecords;
}
}
}
}
// }
If Rejected --- marked rejected on Parent
If Submitted not all Approved -- Mark Submit
If all Approved -- Approved.
trigger ADRUserTriggerV2 on ADRUser__c (after update) {
list<ADR__c> ListOfADRRecords;
set<Id> PrimaryKeyId = new set<Id>();
for(ADRUser__c ADRUserRecords :trigger.new){
if(trigger.oldmap.get(ADRUserRecords.id).ApproveReject__c != 'Submit'){
if(ADRUserRecords.ApproveReject__c =='Submit' || ADRUserRecords.ApproveReject__c =='Approved' || ADRUserRecords.ApproveReject__c =='Rejected'){
PrimaryKeyId.add(ADRUserRecords.ADR__c);// you child object must having parent object field, put that field Api Name here
}
}
}
if(!PrimaryKeyId.isEmpty()) {
list<ADR__c> listOfParentRecordsToUpdate = [select id, Apex_Status__c from ADR__c where Id =: PrimaryKeyId];
ListOfADRRecords = new list<ADR__c>();
for(ADRUser__c ADRUserRecords :trigger.new){
for(ADR__c ADRRecordTOUpdate : listOfParentRecordsToUpdate) {
if (ADRUserRecords.ApproveReject__c =='Rejected' ) {
ADRRecordTOUpdate.Apex_Status__c = 'Rejected';
} else if (ADRUserRecords.ApproveReject__c =='Submit' || ADRUserRecords.ApproveReject__c =='Approved') {
ADRRecordTOUpdate.Apex_Status__c = 'Submit';
} else {
ADRRecordTOUpdate.Apex_Status__c = 'Approved';
}
ListOfADRRecords.add(ADRRecordTOUpdate);
Update ListOfADRRecords;
}
}
}
}
// }
Could you provide some more information with what you need help on? The first issue that stands out is your "update" statement towards the bottom. I would move this out of the for loop. I made a guess as to what you needed help with. See the sample code here:
As you can see, the first for loop gathers the information from the child object and sets the status. Because multiple records could have the same parent, the three IF statements add a priority to the various statuses. The last for loop makes the change on the parent object stored in the map.
If this doesn't help, could you clarify your question so I can help?
Thanks!
Once the approval has been setup I want the master object ADR status to become Submit.
If one record is rejected I want the master object to say rejected.
If no rejection, and there is an approved status, then I want the master to stay as submitted until all child records state approved.
I think that the below code would loop through the records, it would hit the IF approved then write approved on the master even if there are other approvals in submit status.
When all records were approved the status on the parent stayed as submit rather than approved once all child records were approved.
Also I rejected one of the child records and the status of the ADR - Apex_Status stayed submit
I think I understand your question better. Your problem lies in that you aren't taking into account all of the ADR Users when determining the status. See the code below:
What would be the maximum number of ADRUser__c for each ADR__c? If this number is large, that could cause problems.
Let me know if this doesn't help!