Monday, 3 June 2013

Submit For Approval Through Workflow Rules

After much time studying how to master Apex in,  I have finally created the solution to something that has limited me and others for a long time.  Submitting for Approval through workflows.

The solution is available for you through two avenues:

Option 1:  Install the package

Use this URL to install the package into any organization:
Note: If you are installing into a sandbox organization you must replace the initial portion of the URL with

Option 2: Enter the code yourself


trigger initiateApprovals on Task (After Insert) {

    // Roy Lloyd -
    // This code is available for you to use/copy/amend as required.  All I seek is my name remains attached to my work.  Sharing greatness.
    // 3rd June 2013.

    // Loop through the inserted Tasks and get the record ID if they are 'Automatically Submitted For Approval'
    list<id> recordId = new list<id>();
    for (task t :{
        if(t.subject == 'Automatically Submitted For Approval'){
            if(t.WhoID != null){recordId.add(t.WhoId); continue;}            
            if(t.WhatId != null){recordId.add(t.WhatId);}        
    for(id r: recordId){
        // Create an approval request
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setComments('Automatically submitting request for approval.');
        // Submit the approval request
        Approval.ProcessResult result = Approval.Process(req);


public class testInitiateApprovals{

    // Roy Lloyd -
    // This code is available for you to use/copy/amend as required.  All I seek is my name remains attached to my work.  Sharing greatness.
    // 3rd June 2013.

    static testMethod void approvalTest(){
        account a = new account(Name = 'Test Account 3.141592654');
        insert a;
        task t = new task(Subject = 'Automatically Submitted For Approval', WhatId = a.Id);
        insert t;



The Requirements

Somethings are worth knowing.   You will need:
- The ability to customise the setup.
- ‘Track Activities’ enabled on the Standard/Custom object that your target Approval process runs on (does not need to be added to page layout)
- To create a workflow to create a task (preferably a completed one) with the subject “Automatically Submitted For Approval”.  This is critical to start the approval process.
- If you want 100% test coverage, a dummy Approval process on the Account object, using entrance criteria that is highly unlikely to trigger under normal conditions, “Account Name EQUALS Test Account 3.141592654”
- Some actual Approval processes which you want automatically triggered…
- Your record to meet the entrance criteria for active Approval Processes, when your workflow is triggered.   Do consider both the workflow rule criteria and the approval entrance criteria.

What happens in brief
- A record is updated to meet certain criteria, for example an Opportunity with a higher discount level, triggering a workflow.
- The workflow creates a  task (preferably a closed one)
- The creation of the task triggers the Apex trigger, which then submits the record to the Approval processes.

Sunday, 20 January 2013


to my first post. This blog was created to share ideas and coding that has helped to make Salesforce more friendly for me. Nothing here is new, nor is it copyrighted. Simply sharing greatness.