4

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 !!!

3
  • You are not populating the approvalReqList, its a new List with no data. Are u see anything in the ###lstObjParent : debug?See here on how to populate the data for approvalReqList salesforce.stackexchange.com/questions/2244/…
    – Rao
    Apr 2, 2014 at 17:07
  • Did you consider inserting a Field Update in the Approval Process final approval step that sets a custom field is_approved__c to true; then use a Trigger on that custom object? This can avoid in many cases the querying of SFDC objects used in approval processes
    – cropredy
    Apr 2, 2014 at 19:05
  • Very good idea indeed .. I'm working on it right now! thank you
    – LoveLace
    Apr 3, 2014 at 9:09

1 Answer 1

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

2
  • Yes tis is exactly what I did, I created a Is_approved__c checkbox setting to true once the record is approved, then I'll manipulate my credit according to this checkbox whether is checked or not :)
    – LoveLace
    Apr 11, 2014 at 8:04
  • great - please mark as solution so others know the answer has been vetted
    – cropredy
    Apr 11, 2014 at 15:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .