1

Sometimes when creating UnitTests I'm unable to save classes for hours at a time. It'd be great - as a workaround - to develop UnitTests using the "force cli".

However, I'm unable to execute code that has system.runas in it.

An "ERROR: UNKNOWN_EXCEPTION: admin operation already in progress" error occurs.

Is this possible?

PS C:\salesforce\dev04> force apex -test
 Start typing Apex code; press CTRL-D(for Mac/Linux) / Ctrl-Z (for Windows) when finished

Test.startTest();
Id foProfile = FO_UnitTest_V2_Helper.getProfileId(FO_UnitTest_V2_Helper.FRONT_OFFICE_PROF_NAME);
User testUser = FO_UnitTest_V2_Helper.createUser(foProfile, 'T', 'User');
insert testUser;
System.runAs(testUser){
    \/\/ code here
}
Test.stopTest();

^Z

Executing code...
ERROR: UNKNOWN_EXCEPTION: admin operation already in progress

0

2 Answers 2

2

System.runAs only runs in actual unit tests. Execute Anonymous is not a unit test context; any changes you make in Execute Anonymous successfully will save.

If you want to run tests, you need to do a metadata deployment. If you don't want to actually save the changes, use the -c (check only) deployment mode. However, metadata deployments are still always in serial, so you'd still have to wait.

If you're experiencing challenges with deployments taking excessively long, you probably need to start using Scratch Orgs or Sandboxes. This gives you own dedicated developer org to test changes, reducing the number of deployments you'll have to wait on.

You should not have more than one developer deploying to an org because of the limitations around deployments.

Your question appears to be an X-Y Problem; you're trying to solve a congestion problem, and this "solution" is a non-starter. You may want to take a step back and reevaluate what you actually need.

1

Based on its documentation, the force apex command executes Anonymous Apex, not an Apex unit test. The runAs() construct, along with Test.startTest() and Test.stopTest(), is available only in the context of an Apex unit test.

Note that executing Anonymous Apex also does not save anything - it is not a development activity. The Force CLI is an open source project and is not the official Salesforce CLI.

This error:

ERROR: UNKNOWN_EXCEPTION: admin operation already in progress

is linked to your broader problem (below), not to this specific Apex construct.

Sometimes when creating UnitTests I'm unable to save classes for hours at a time. It'd be great - as a workaround - to develop UnitTests using the "force cli".

The recommended approach to solve this is twofold:

  1. Develop your code in version control, not in an org. Deploy your code using the official Salesforce CLI (sfdx). You can use an IDE or third-party CLI if you wish but it won't fundamentally change the paradigm.
  2. Develop in isolated environments - either a scratch org, best of all, or a single-user Developer sandbox. If you are encountering frequent admin locks in the org, that is an indicator that it is being used or deployed against by multiple persons, which is not best practice.
11
  • You can execute Unit tests - that is - execute assert statements - using system.runas like this: \n Test.startTest(); List<Account> accounts = [SELECT Id FROM Account limit 2]; System.assertEquals(3, accounts.size(), 'number of accounts'); Test.stopTest();
    – Emmett
    Apr 27, 2021 at 18:11
  • Yes, you can do that in an Apex unit test. An assertion is not the same thing as a unit test.
    – David Reed
    Apr 27, 2021 at 18:15
  • No. You can do that in the force-cli. Copy and try it.
    – Emmett
    Apr 27, 2021 at 18:17
  • It's fine if you are able to execute Test.startTest() and assertions in Anonymous Apex, but that is not the same thing as writing and saving an Apex unit test to the org. I do not use Force CLI.
    – David Reed
    Apr 27, 2021 at 18:25
  • I don't want to save it to the Org. That's the point.
    – Emmett
    Apr 27, 2021 at 18:26

You must log in to answer this question.

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