Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After Update Trigger pending Approval request OriginalActorId

The trigger below is built to pull the most recent "assigned to" user (or OriginalActorId) from the approval process list, the Approval_started__c field is checked as an initial submission action from a specific approval process.

The issue I have is that the system.debug statement below is not showing the most recent assigned to user in the last pending Approval request but shows the most recent request assigned to user before the user clicks on submit for approval, so it skips the "approval request submitted step" that the User did when he clicked on Submit for approval and the initial submission action pending Step (where the approver is selected by the user) which is pending approval/rejection and it returns the old request that was right before these 2.

My goal it to pull the latest pending request "assigned to" user or OriginalActorId value.

Any thoughts ? Thanks.

Here's a screenshot of the approval process list, with in black the value I fetch with this trigger on the debug log line and in blue the expected value.

Approval process List

trigger Assigned2testTrigger on LLC_BI__Product_Package__c(after update) {

    list < LLC_BI__Product_Package__c > listpp = new list < LLC_BI__Product_Package__c > ();
    for (LLC_BI__Product_Package__c pp: trigger.new) {
        If(!checkRecursive.SetOfIDs.contains(pp.Id)) {


            if (pp.Approval_started__c == true) {


            system.debug('---------> My ProcessInstance           ' + string.valueof([Select Id, (Select TargetObjectId, SystemModstamp, StepStatus, RemindersSent, ProcessInstanceId, OriginalActorId, IsPending, IsDeleted, Id, CreatedDate, CreatedById, Comments, ActorId From ProcessSteps order by SystemModstamp desc) from LLC_BI__Product_Package__c where Id =: pp.id].ProcessSteps[0].OriginalActorId));
            }
            checkRecursive.SetOfIDs.add(pp.Id);
        }
    }
}
like image 961
Sam Avatar asked Jul 14 '18 23:07

Sam


1 Answers

In your query, you are going to want to find the newest ProcessInstance, and then the newest associated ProcessInstanceStep. Your query would look something like this:

SELECT Id
     , SystemModStamp
     , Status
     , (SELECT SystemModstamp
             , StepStatus
             , ProcessInstanceId
             , OriginalActorId
             , Id
             , CreatedDate
             , CreatedById
             , Comments
             , ActorId
        From Steps
        ORDER BY SystemModstamp DESC)
FROM ProcessInstance
WHERE TargetObjectId = :pp.Id
ORDER BY SystemModStamp DESC

What's also important here is that in order for this trigger to fire, you need to perform an update on the record itself. By default, if there are no field update actions, changes are only made to the process instance related records and not the record being approved.

If you create separate checkbox fields and the corresponding field update actions for each step where you want the trigger to fire, you should start to see the trigger firing after each step.


One field for each step, default to unchecked:

enter image description here


One field update for each step to check the checkbox so that the trigger fires on the related object. If the approval process can happen multiple times, you would also want the initial submission to reset the checkboxes to unchecked:

enter image description here

like image 86
martin Avatar answered Sep 18 '22 00:09

martin