+ Start a Discussion
Jacek DJacek D 

How to run Apex trigger after successful approval workflow?

I'd like to run an Apex trigger that updates the owner field on the contract page once the contract is successfully approved. I know how to update the owner field, but I don't know how to make the trigger run at the given time.

Any suggestions on how to do that?

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
goabhigogoabhigo

I hope your approval process changes the approval status field, if not try it[ create a picklist Approval Status with values 'Pending', 'Approved', 'Rejected']. Once the record is submitted set the status to Pending, via approval process's workflow.

When approved change status to Approved. In the trigger(after update) check the Approval status, if it is approved, then change the owner.

 

Hope it helps.

All Answers

bob_buzzardbob_buzzard

If you have a before/after update trigger on your object, then when the approval is successful you can carry out a workflow field update which will cause the trigger to fire.  

Jacek DJacek D

Ah, so I use field update to change the status of a field (e.g. status__c) and then have the trigger run a logic like this:

 

If status__c = approved

then lookupfield = WhateverTheSource

 

???

 

Thanks for helping me out on this, I am a newbie in Apex. 

goabhigogoabhigo

I hope your approval process changes the approval status field, if not try it[ create a picklist Approval Status with values 'Pending', 'Approved', 'Rejected']. Once the record is submitted set the status to Pending, via approval process's workflow.

When approved change status to Approved. In the trigger(after update) check the Approval status, if it is approved, then change the owner.

 

Hope it helps.

This was selected as the best answer
bob_buzzardbob_buzzard

One additional point - you'd usually check that the value of the field has changed rather than just checking its value, if it was left as true from a previous transaction.

 

E.g. if you have a custom field called "RunApprovalTrigger__c:

 

 

trigger MyObjectTrigger on MyObject (before update) 
{
   for (MyObject myObj : trigger.new)
   {
      if ( (myObj.RunApprovalTrigger__c) &&
           (trigger.old.get(myObj.id).RunApprovalTrigger__c) )
      {
         // do stuff here

         // unset the flag
         myObj.RunApprovalTrigger__c=false;
      }
   }
}

 

 

 

 

Jacek DJacek D

Yes, that will work. I won't even have to reset the field causing the trigger to run!

 

Thanks a bunch!

BrianDunzweilerBrianDunzweiler

Minor edit to Bob's code sample that confused me at first.

 

was:

trigger.old.get(myObj.id).RunApprovalTrigger__c

 

should be:

trigger.old.getMap(myObj.id).RunApprovalTrigger__c

 

Thanks for the code sample!

 

--Brian