Find your content:

Search form

You are here

Applying changes when the approval process is approved

 
Share

I need to apply changes when my approval process is approved, here is my code behind:

public static Set<Id> setParentID = new Set<Id>();
public static List<User> lstObjParent = new List<User>();

public static void MyCredit(List<Time_Off__c> lstObjFils){
 try{
    List<Approval.ProcessSubmitRequest> approvalReqList=new List<Approval.ProcessSubmitRequest>();
    List<Approval.ProcessResult> resultList = Approval.process(approvalReqList);       

for(Approval.ProcessResult result: resultList )
{        
    if(result.isSuccess() == true)
    {
        if(result.getInstanceStatus() == 'Approved')
        {
        for(Time_Off__c obj : lstObjFils)
        {
                setParentID.add(obj.CreatedById);
        }
        lstObjParent = [Select Credit__c from User where Id in :setParentID];
        System.debug('###lstObjParent  : '+lstObjParent );

        if(lstObjParent.size()>0)
        {

            for(Time_Off__c obj : lstObjFils)
            {
                    for(User objPar: lstObjParent)
                    {
                            if(obj.CreatedById == objPar.Id)
                            { 
                                if (obj.Pattern__c == 'Payed time off'){
                                    System.debug('###obj.Days__c  : '+obj.Days__c);
                                    System.debug('###objPar.Credit__c  : '+objPar.Credit__c);
                                    if(objPar.Credit__c > obj.Days__c){
                                    objPar.Credit__c -= obj.Days__c;
                                    }
                                    else
                                    objPar.Credit__c= 0;
                                    System.debug('No credit left');
                                    System.debug('###objPar.Credit__c After : '+objPar.Credit__c);
                                }
                            }
            }
            }
        }
          update  lstObjParent;
        }
        }
 }
 }      catch(Exception e){
     System.debug('###Exception E :'+ e.getMessage());
 }

But even when I approve my record, there's no changes !!!


Attribution to: LoveLace

Possible Suggestion/Solution #1

It is not clear from your example what SObject is being approved but we'll assume it is Credit__c

  1. Add a custom Boolean field to Credit__c called is_approved__c
  2. Change your Approval Process so in the Final Approval Step you do a field update setting is_approved__c to true
  3. The before / after trigger on Credit__c will execute allowing you to do your logic

Or, you could generalize, and have a custom field on Credit__c called approval_status__c and in the final approval actions, set the custom field to approved, in the final reject actions, set to rejected, and in the recall action, set to recalled - allowing more variability in your processing


Attribution to: cropredy
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31646

My Block Status

My Block Content