2

I need some help

Aim- To create a basic approval process.

I have created the following trigger “AutomateApprove” on the campaign object, however I am keep getting error messages when populating fields “Next Step” And “Next Approver”. “Next Step” and “Next Approver” are both Custom fields and both of these fields have the data type as “Text(255)

When I populate the field “Next Step” with “Submit” And “Next Approver” as “Daniel Hotmail mason” I get the following error

Error:Invalid id: Daniel Hotmail Mason.

User : Daniel Hotmail Mason User iD : 00520000003Cznv

When I put the user id into ““Next Approver” field I get this error message “Error:Process failed. First exception on row 0; first error: NO_APPLICABLE_PROCESS, No applicable approval process found.: []

trigger AutomateApprove on Campaign(After insert, After update)
{

    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
     try
     {
        //Insure that previous value not equal to current, else if there is any field update on approval process action
        //there will be recurrence of the trigger.

        if(Trigger.new[i].Next_Step__c == 'Submit' && Trigger.old[i].Next_Step__c != 'Submit')
        {
           submitForApproval(Trigger.new[i]);
        }
        else if(Trigger.new[i].Next_Step__c == 'Approve' && Trigger.old[i].Next_Step__c != 'Approve')
        {
             approveRecord(Trigger.new[i]);
        }
        else if(Trigger.new[i].Next_Step__c == 'Reject' && Trigger.old[i].Next_Step__c != 'Reject')
        {
             rejectRecord(Trigger.new[i]);
        }
     }catch(Exception e)
     {
         Trigger.new[i].addError(e.getMessage());
     }
    }

    // This method will submit the Campaign automatically
    public void submitForApproval(Campaign opp)
    {
        // Create an approval request for the Campaign
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval automatically using Trigger');
        req1.setObjectId(opp.id);
        req1.setNextApproverIds(new Id[] {opp.Next_Approver__c});

        // Submit the approval request for the Campaign
        Approval.ProcessResult result = Approval.process(req1);

    }

    //Get ProcessInstanceWorkItemId using SOQL
    public Id getWorkItemId(Id targetObjectId)
    {
        Id retVal = null;

        for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p
            where p.ProcessInstance.TargetObjectId =: targetObjectId])
        {
            retVal  =  workItem.Id;
        }

        return retVal;
    }

    // This method will Approve the Campaign
    public void approveRecord(Campaign opp)
    {
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Approving request using Trigger');
        req.setAction('Approve');
        req.setNextApproverIds(new Id[] {opp.Next_Approver__c});
        Id workItemId = getWorkItemId(opp.id);
        //opp.addError(workItemId);

        if(workItemId == null)
        {
            opp.addError('Error Occured in Trigger');
            //opp.addError(workItemId);
        }
        else
        {
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }

    // This method will Reject the Campaign
    public void rejectRecord(Campaign opp)
    {
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Rejected request using Trigger');
        req.setAction('Reject');
        //req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
        Id workItemId = getWorkItemId(opp.id);
        //opp.addError(workItemId);

        if(workItemId == null)
        {
            opp.addError('Error Occured in Trigger');
            //opp.addError(workItemId);
        }
        else
        {
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }

}
3

The error message you want to get is:

Error:Process failed. First exception on row 0; first error: NO_APPLICABLE_PROCESS, No applicable approval process found.: []

This error is basically telling you that it actually ran through the logic, but couldn't find an approval process. The other error means you were not setting that field properly. As far as why you are receiving that error, there are two possible problems.

1) The data on the object you are attempting to submit for approval does not meet the entry criteria. Look at Step 2 in your setup wizard and make sure it matches the data on your object:

enter image description here

You can also access this through the edit menu

enter image description here

2) Your approval process is inactive. I have run into this myself before when I forgot to activate an approval process, but make sure your approval process is marked as Active.

enter image description here

Once you have Approval Steps entered, you should see an Activate button you can click

enter image description here


If you are absolutely brand new to Approval Processes, you will want to read up on some of the documentation Salesforce provides. They break down how to use them in more detail than I can, but take your time and go through each of them. You can only access an Approval Process in Apex once you have it fully set up declaratively. There is a good chance you may not need your trigger at all.

  • Hi Jesse Altman Thank you for swift response. I never created an approval process before so i am a little stuck. If you dont mind, can we go through this together ? I have created a button on the "Campaign" Object called, "My new Button". I want the approval process to start when the "My new Button" is pressed. More than happy to give you access to my test org, so you can have a look at what i am trying to achieve – user8746 Jun 13 '14 at 12:43
  • @user8746 - Oh, so you haven't set up an approval process at all yet? The method you are taking to get it set up is incorrect. I will update my answer for you to include some details. – Jesse Altman Jun 13 '14 at 12:46
  • No I havent. I googled approval process, and i merged 4 ideas into one. So not sure if its the correct approach. but i was hoping to build a basic approval process and then build out accordingly – user8746 Jun 13 '14 at 12:50
  • 1
    @user8746 Ok I added some details about getting started with Approval Processes. As I noted above, Salesforce provides a ton of great documentation on Approval Process so start there and read through it all. I think what you are looking to do can probably be done declaratively with no code. Use that approach for now and put your Trigger on hold. If this helps you and solves your problem, feel free to upvote and mark as the answer. – Jesse Altman Jun 13 '14 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.