1

In my test class, I call a function which query Account object with USING SCOPE Mine clause :

Test class :

@IsTest
public class MyClassTest {
    Account a = new Account(name = 'Test');
    a.BillingCity = 'xxx';
    a.OwnerId = userId; // Only used this line if I create an user and use System.runAs(myUser) at the beginning of test class
    insert a;

    MyClass controller = new MyClass();
    System.assertNotEquals(null, controller.myFunction(a.Id));
}    

Class :

public class MyClass {
    public String myFunction(Id i) {
        List<Account> res = [SELECT Id, Name FROM Account WHERE BillingCity = 'xxx' AND Id = :i USING SCOPE Mine];
        String myId;

        for(Account acc : res) {
            // Test coverage never goes inside this loop because 'res' is empty
            myId = (String)acc.Id;
        }

        return myId;
    }

But this query returns no Account !

I try with System.runas(user) {...} without success.

How can I do this ?

6
  • I think you might have to share a more complete Minimum Viable Reproduction case to demonstrate what you mean.
    – Adrian Larson
    Nov 4, 2016 at 14:59
  • I edited my post with the part of code which is never covered.
    – SF_user
    Nov 4, 2016 at 15:02
  • Yes but the data creation is a critical component of reproduction in this instance.
    – Adrian Larson
    Nov 4, 2016 at 15:04
  • Ok, I simply insert an Account. I edited my post.
    – SF_user
    Nov 4, 2016 at 15:07
  • 2
    i'm puzzled -- why is the test class using a USING SCOPE Mine ? Why not ownerId = : UserInfo.getUserId() ?
    – cropredy
    Nov 4, 2016 at 16:04

1 Answer 1

3

First of all, the syntax used in your controller:

List<Account> res = [SELECT Id, Name FROM Account 
                          WHERE BillingCity = 'xxx' AND Id = :i 
                          USING SCOPE Mine];

is not correct. It should be:

List<Account> res = [SELECT Id, Name FROM Account 
                        USING SCOPE Mine // see https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_limits.htm?search_text=using%20scope
                        WHERE BillingCity = 'xxx' AND Id = :i ];

When I tried this in my dev org, the (corrected) SOQL executes just fine, returning the mocked Account.

You must log in to answer this question.

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