+ Start a Discussion
Bryan REVELANT 2Bryan 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;
}
          }
   }
    }
    // }
Bradley DelauneBradley Delaune
Hi Bryan,

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:
trigger ADRUserTriggerV2 on ADRUser__c (after update)
{
	list<ADR__c> ListOfADRRecords;
	map<Id,String> adrIdToStatusMap = new map<Id,String>();
    for(ADRUser__c ADRUserRecords :trigger.new)
	{
		if(trigger.oldmap.get(ADRUserRecords.id).ApproveReject__c != 'Submit')
		{
			if(ADRUserRecords.ApproveReject__c == 'Submit')
			{
				if(!adrIdToStatusMap.containsKey(ADRUserRecords.ADR__c))
					adrIdToStatusMap.put(ADRUserRecords.ADR__c,'Submit');
			}
			else if(ADRUserRecords.ApproveReject__c == 'Approved')
			{
				if(!adrIdToStatusMap.containsKey(ADRUserRecords.ADR__c) || adrIdToStatusMap.get(ADRUserRecords.ADR__c) == 'Submit')//this gives priority of Approved over Submit
					adrIdToStatusMap.put(ADRUserRecords.ADR__c,'Approved');
			}
			else if(ADRUserRecords.ApproveReject__c == 'Rejected')
			{
				if(!adrIdToStatusMap.containsKey(ADRUserRecords.ADR__c) || adrIdToStatusMap.get(ADRUserRecords.ADR__c) == 'Submit' || adrIdToStatusMap.get(ADRUserRecords.ADR__c) == 'Approved')//this gives priority to Rejected
					adrIdToStatusMap.put(ADRUserRecords.ADR__c,'Rejected');
			}
		}
	}

   if(!adrIdToStatusMap.keyset().isEmpty())
   {
		list<ADR__c> listOfParentRecordsToUpdate = [select id, Apex_Status__c from ADR__c where Id =: adrIdToStatusMap.keyset()];
		
		for(ADR__c ADRRecordTOUpdate : listOfParentRecordsToUpdate)
		{
			ADRRecordTOUpdate.Apex_Status__c = adrIdToStatusMap.get(ADRRecordTOUpdate.Id);
		}
		Update listOfParentRecordsToUpdate;
	}
}

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!
Bryan RevelantBryan Revelant
I created a dynamic concurrent approval flow. The master object ADR has multiple child records ADRuser.

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.
Bryan RevelantBryan Revelant
Actually I tried the above. 

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
Bradley DelauneBradley Delaune
Hi Bryan,
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:
trigger ADRUserTriggerV2 on ADRUser__c (after update)
{
	list<ADR__c> ListOfADRRecords;
	set<Id> adrIdSet = new set<Id>();
	map<Id,String> adrIdToStatusMap = new map<Id,String>();
    for(ADRUser__c ADRUserRecords :trigger.new)
	{
		adrIdSet.add(ADRUserRecords.ADR__c);
	}
	
	for(ADRUser__c u : [select Id, ApproveReject__c, ADR__c from ADRUser__c where ADR__c in :adrIdSet]) //this SOQL query could be very large, make sure your data model won't allow this to get out of hand.
	{
		if(!adrIdToStatusMap.containsKey(u.ADR__c))
			adrIdToStatusMap.put(u.ADR__c,u.ApproveReject__c);
		else
		{
			if(u.ApproveReject__c == 'Rejected')
				adrIdToStatusMap.put(u.ADR__c,'Rejected');
			else if(u.ApproveReject__c == 'Submit' && adrIdToStatusMap.get(u.ADR__c) != 'Rejected')
				adrIdToStatusMap.put(u.ADR__c,'Submit');
			else if(u.ApproveReject__c == 'Approved' && adrIdToStatusMap.get(u.ADR__c) != 'Rejected' && adrIdToStatusMap.get(u.ADR__c) != 'Submit')
				adrIdToStatusMap.put(u.ADR__c,'Approved');
		}
	}

   if(!adrIdToStatusMap.keyset().isEmpty())
   {
		list<ADR__c> listOfParentRecordsToUpdate = [select id, Apex_Status__c from ADR__c where Id =: adrIdToStatusMap.keyset()];
		
		for(ADR__c ADRRecordTOUpdate : listOfParentRecordsToUpdate)
		{
			ADRRecordTOUpdate.Apex_Status__c = adrIdToStatusMap.get(ADRRecordTOUpdate.Id);
		}
		Update listOfParentRecordsToUpdate;
	}
}

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!
Bryan RevelantBryan Revelant
I would bet that it would be less than 10 for every ADR so i dont think that I will reach a SOQL issue