+ Start a Discussion
Kevin MoormannKevin Moormann 

Execute Workflow Rules from Apex

Re-Posting to the Apex Board per a suggestion...(http://boards.developerforce.com/t5/Formulas-Validation-Rules/Execute-Workflow-Rules-from-Apex/td-p/272059)


Is it possible to execute a workflow rule from an Apex class?


For example could I query the metadata of an Opportunity and get back the array of all workflow rules (e.g., WorkflowRule[]) then execute a specific one?



//pseudo code//

//could one perform something to the effect of...


WorkFlowRule[] rules = gatherRules();


WorkflowRule rule = WorkflowRules[0];






Thanks in advance.




I'm not aware of any way to achieve this, as you can't query metadata from Apex, only via the metadata API.  


I'm curious as to why you would want to do this - typically you'd use workflow rules to avoid Apex, and Apex to handle things that are too complex for workflow rules.






Could you please explain in detail, what exactly want to achieve?


I dont think its possible to execute the workflow from within Apex Class. 

Also as you can fire workflows based on different conditions on objects itself, there will hardly be any need to invoke the workflow from within the Apex class.

Pratibh PrakashPratibh Prakash



Workflow rules, trigger etc get fired whenever a DML is performed on the concerned object. For is there is a trigger and WF rule on Account say testTrigger on update event and WF named change TestWF for which entry cretria is if Account.SLA = GOLD.


Now when a Account record will be updated both trigger and WF will get fired and process as per logic specifies. Here if SLA is set GOLD then WF Action will take place otherwise not.


So in order to execute your WF from Apex Class fir DML on that object and try to set data that particular record so that it satisfies the WF entry criteria.

Kevin, were you able to find an answer to your query. I too have a similar requirement,which I feel would have been easily possible if I could call the workflow metadata and execute it in my Apex code?