Can we create workflows in Apex?. In Apex documentation there is information only regarding creation of approval processes.I was wondering if we can create workflows in Apex. can anyone show me some example code?

  • Why do you need this? What are you trying to achieve that can't be done with a regular workflow, visual flow or writing a trigger in Apex? Oct 4, 2014 at 8:59
  • I was going through the Apex documentation. There is code for creating approval processes. But i didn't find any code for creating workflow.So i want to know whether it can be done in apex or not. Oct 4, 2014 at 9:06
  • Yes, but why? Is this just a curiosity or do you have a requirement that needs to be fulfilled? Triggers are the primary way of creating workflow-like functionality in Apex. Oct 4, 2014 at 9:51
  • can u please show me a example code? .I want to learn in and out of apex programming.Thanks in advance :) Oct 4, 2014 at 11:55

3 Answers 3


You can not create approval process or workflow with Apex system classes.

These are configurable business logics which can not be created generally in Apex.

But there is one way of creating these components is Metadata API. In metadata API you can create Approval process, workflows, workflow rules, Assignment rules and other admin and development logics.

In apex, a record can be submit for approval, a record can be rejected and approved. You can only access already created Approval process on object and perform submit, approve and reject operations on record. Workflow rules can not be accessed in this way.

A simple way to create approval process by Metadata API is given in Must specify a {http://www.w3.org/2001/XMLSchema-instance}type during creating Approval process using metadata API question earlier asked by me.


In Winter 15, you'll be able to create Visual Flows that can be used to trigger workflow, which is essentially one way of utilizing Apex and VisualForce, along with other logic to create complex workflows. These workflows essentially act as triggers. They can be both instant, or time based.

These Flows can only be used with Visual Flows that don't require any user interaction. A recent webinar titled The Power of Flow: Advanced Workflow Techniques, walks through an example of a lead assignment that could be used as workflow when this feature is enabled in your org. It was discussed in the webinar that the feature could be enabled in an org by special request at this time.


You can crate workflows and approval processes via apex triggers. But for approval processes I'd recommend you to go through the configuration out of the box feature for it as it is pretty extensive and salesforce has built it so beautifully.

For Workflows, yes some workflows cannot be created using the out of the box workflow feature. You can create them using triggers. If you are pretty new to salesforce trigger take a look at this. https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_HelloWorld.htm.

Thanks Akash

You must log in to answer this question.

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