2

I've followed instructions here to submit records into an approval process. This seems to be running fine without errors, and my debug logs have confirmed the submission has occured:

16:50:09:101 USER_DEBUG [13]|DEBUG|@@@o.Id: 006m0000005TZs1AAG
16:50:09:101 USER_DEBUG [14]|DEBUG|@@@processName: VP_Approval_Final
16:50:11:677 USER_DEBUG [25]|DEBUG|@@@result.isSuccess: true
16:50:11:677 USER_DEBUG [26]|DEBUG|@@@result.getInstanceStatus: Pending

However, upon navigation to this records page, I'd expect to see the record inserted into an approval flow with approval process history. Instead, I see nothing. What am I doing wrong? The code I'm using is nearly an exact copy/paste of the above link.

 private static void runApproval(Opportunity o){

        // Create an approval request for the account
        Approval.ProcessSubmitRequest req1 = 
            new Approval.ProcessSubmitRequest();
        req1.setObjectId(o.Id);

        // Submit on behalf of a specific submitter
        req1.setSubmitterId(UserInfo.getUserId()); 

        System.debug('@@@o.Id: ' + o.Id);
        System.debug('@@@processName: ' + o.Migration_ProcessName__c);
        // Submit the record to specific process and skip the criteria evaluation
        req1.setProcessDefinitionNameOrId(o.Migration_ProcessName__c);
        req1.setSkipEntryCriteria(true);    

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

        // Verify the result
        System.assert(result.isSuccess());

        System.debug('@@@result.isSuccess: ' + result.isSuccess());
        System.debug('@@@result.getInstanceStatus: ' + result.getInstanceStatus());

        System.assertEquals(
            'Pending', result.getInstanceStatus(), 
            'Instance Status'+result.getInstanceStatus());

            if(o.Migration_Approve__c){
            // Approve the submitted request
            // First, get the ID of the newly created item
            List<Id> newWorkItemIds = result.getNewWorkitemIds();

            // Instantiate the new ProcessWorkitemRequest object and populate it
            Approval.ProcessWorkitemRequest req2 = 
                new Approval.ProcessWorkitemRequest();
            req2.setComments('Approving request.');
            req2.setAction('Approve');
            req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});

            // Use the ID from the newly created item to specify the item to be worked
            req2.setWorkitemId(newWorkItemIds.get(0));

            // Submit the request for approval
            Approval.ProcessResult result2 =  Approval.process(req2);

            // Verify the results
            System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());

            System.assertEquals(
                'Approved', result2.getInstanceStatus(), 
                'Instance Status'+result2.getInstanceStatus());
            }
    }

enter image description here

2 Answers 2

3

The code I'm using is an exact copy/paste of the above link

The reason you do not see anything in the database via the UI when you look at an opportunity is because the code:

  1. Creates a random account and attempts to submit it, not an opportunity in your org
  2. I doubt you have a user with the alias 'someStandardUser'
  3. You probably do not have a process named 'PTO_Request_Process'

Since the code has asserts it is weird that is is not throwing errors because of #2 and #3. But lets say you have them, since there is no error is comes back to number #1

7
  • That was tricky to identify what sort of mistake he might have made. Commented Dec 8, 2015 at 23:25
  • Alias caps at 7 characters iirc.
    – Adrian Larson
    Commented Dec 8, 2015 at 23:25
  • @AdrianLarson - Oh, then there is a typo in the code posted by SF. Should we let them know?
    – Eric
    Commented Dec 8, 2015 at 23:41
  • Not sure who we would tell, but it's 8, actually. system.assertEquals(8, User.Alias.getDescribe().getLength());
    – Adrian Larson
    Commented Dec 8, 2015 at 23:48
  • @AdrianLarson Post feedback on the page the code came from to report any errors. You can also report errors of that kind to 'at'AskSalesforce (Salesforce Support) on Twitter and they'll pass it along to the appropriate people who are responsible for it.
    – crmprogdev
    Commented Dec 9, 2015 at 14:08
0

I believe I have identified the problem.

I was running the approval process on a list of opportunities, and while some of them seemed to be working, the anonymous apex I was running to do this task failed once it hit certain opportunities that didn't have a defined 'manager'. This seemed to stop the process altogether, even though my asserts and debugs claimed otherwise. This is my inexperience talking - I guess if a bit of anonymous apex fails, can you not trust the logs / asserts of anything previous?

I simply adjusted my SOQL statement to only pull opportunities that have defined managers, and now everything is working as expected.

1
  • No, you can trust them. But if your asserts are only working on a single record then then will not catch other records. We would need you code to tell u exactly what is going on
    – Eric
    Commented Dec 9, 2015 at 16:04

You must log in to answer this question.

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