6
// Insert an account

Account a = new Account(Name='Test',
                     annualRevenue=100.0);

insert a;

// Create an approval request for the account
Approval.ProcessSubmitRequest req1 = 
      new Approval.ProcessSubmitRequest();
req1.setObjectId(a.id);
req1.setNextApproverIds(new Id[] {'***************'});
// Submit the approval request for the account
Approval.ProcessResult result = 
                   Approval.process(req1);

Am trying to execute this code in anonymous block. How ever when tried in trigger i got same error all the time. Hard coded next approvers ID. No luck.

Any suggestions on what am I doing wrong ? Please help!!

2
  • 1
    I have one more doubt on this. to execute this or run approval via Apex should i have a approval process defined on Account in prior ? because once i define a approval process this error disappears.
    – Soberano
    Commented Sep 4, 2013 at 12:32
  • 1
    Yes, the approval process must exist in your system. Check out my answer for more details. Commented Sep 4, 2013 at 13:34

7 Answers 7

10

Is there a chance you created the approval process but don't have it activated? Does your account meet the entry criteria for your approval process? This error is thrown when an object is attempted to be submitted for approval and no approval process can be found, either because it isn't activated or your object doesn't meet the entry criteria.

You can not just instantiate this code. There must already be an approval process in the system for you to be able to run this code.

1
  • I was actually trying to have an approval process with more than 15 steps. I got to know what exactly error meant . And also now I am able to chain the Approval processes and get away with the limitation of 15 steps. Thank you people . thank you @Jesse Altman.
    – Soberano
    Commented Sep 4, 2013 at 14:34
4

I think there is a problem with profile permissions. Try with this code:

Account acc1 = new Account(Name='acc1');
        insert acc1

        if([select count() from ProcessInstance where targetobjectid=:acc1.id] < 1)
        {       
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Approve.');
            req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
            req.setObjectId(acc1.Id);

            //Submit the approval request
            Approval.ProcessResult result = Approval.process(req);

        }
1
  • No luck. And also i am the system admin and have "Modify all data " permission. Am wondering what is the meaning of error and why is it throwing the error on Approval.process(); line.
    – Soberano
    Commented Sep 4, 2013 at 11:18
0

Anyone who runs into this problem, this might help: Your record that is submitted to approval do not have correct sharing settings. The submitter might have read access to the record but not access to lock the record, hence the submit to approval is failing.

0

There May be a few reasons:

  1. Approval Process Maybe deActivated.
  2. Approver may be inActive
  3. Approver may not be in initial Submitter role,..etc.

Hope it helps.

1
0

Go to Approval Process edit it and add record creator as initial submitter under allowed initial submitters.

Hope it helps

0

I was having the similar issue, and found that the submitter also needs to be added to the Initial Submitter list in the approval process settings.

0

I fixed this by adding this to the Approval.ProcessSubmitRequest. Mine was on the Quote object, and I also needed to add an approver to a custom field. I don't know how this code ever made it to production originally.

req1.setProcessDefinitionNameOrId('name_of_approval_process');
req1.setSkipEntryCriteria(true);

You must log in to answer this question.

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