1

I want to find a single (any) user who has an approval assigned to them. I can't seem to find the right search term.

 User U = [select id from User (select actorid from processInstanceWorkItem) limit 1];

My goal class:

@isTest 
public without sharing class ApprovalsExtTEST { 

 static testMethod void TestSalesHome(){ 

 User U2 = [SELECT Id FROM User WHERE Id IN ( SELECT ActorId FROM ProcessInstanceWorkItem) LIMIT 1];


  ApexPages.StandardController stdController4 = new ApexPages.StandardController(u2); 
     UsersItemsToApprove MyController4 = new UsersItemsToApprove(stdController4); 


        System.runAs(U2) {
     MyController4.getWorkItems();   
     }

     }


     }

Currently errors:

System.QueryException: List has no rows for assignment to SObject

1 Answer 1

3

You want to use a Left Inner Join. I have also seen it called an inner-join sub-select. In this case, you need:

SELECT Id FROM User WHERE Id IN (
    SELECT ActorId FROM ProcessInstanceWorkItem
) LIMIT 1

Note that tests do not have access to org data by default. You will have to actually submit a test record for approval in order to get this query to return records.

static testMethod void demoTestItems()
{
    SomeObject__c record = new SomeObject__c();
    insert record;

    Test.startTest();
        Approval.ProcessSubmitRequest submitRequest = new Approval.ProcessSubmitRequest();
        submitRequest.setObjectId(record.Id);
        Approval.process(submitRequest);
    Test.stopTest();

    system.assertNotEquals(0, [SELECT count() FROM ProcessInstanceWorkItem]);
    system.assertNotEquals(0, [SELECT count() FROM User WHERE Id IN (
        SELECT ActorId FROM ProcessInstanceWorkItem
    )];
}
6
  • Hmm. I am getting an error of System.QueryException: List has no rows for assignment to SObject when I know there are active approvals in the org
    – jaw999
    Oct 31, 2016 at 20:35
  • Hmm, it works for me. Maybe a permissions issue? Can you try running the code without sharing?
    – Adrian Larson
    Oct 31, 2016 at 20:37
  • updated with more info
    – jaw999
    Oct 31, 2016 at 20:45
  • @jaw999 There aren't any approvals yet in your test unless you use @SeeAllData=true.
    – Adrian Larson
    Oct 31, 2016 at 21:00
  • 1
    Understood. Either I create the approval processes and items in the test or I use see all data and pass. thanks
    – jaw999
    Nov 1, 2016 at 12:01

You must log in to answer this question.

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