function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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];

 

rule.execute();

 

 

 

Thanks in advance.

 

 

bob_buzzardbob_buzzard

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.

 

 

MandyKoolMandyKool

Hi,

 

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

Hello,

 

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.

S DS D
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?