Skip to main content

Greetings,  

 

Hope you're having a good day. 

 

I need some help resolving a problem I am facing while developing a 2nd Generation Managed Package. Following the ISV and SFDX Guide, when I create a 2G Package version via SFDX CLI, it generates an error while evaluating code coverage. 

 

ErrorMessage: Apex Test Failure: Class.httpeakdev1.LazyAdminSearchLog.getHistory: line 24, column 1

Class.httpeakdev1.LazyAdminSearchLogTest.testGetHistory: line 36, column 1 System.QueryException: Insufficient permissions: secure query included inaccessible field

The command: sfdx force:package:version:create --package "LazyAdmin" --codecoverage --wait 10 --installationkey ******

Screenshot: https://i.imgur.com/oEgUPVY.png  (The code is visible as well).Error while creating a 2G Package version: Insufficient permissions: secure query included inaccessible field

 

When I run the Apex tests manually, all tests succeed and no errors appear. (Screenshot: https://i.imgur.com/KEfMvCE.png) 2.apex_test_success.png

 

I have implemented CRUD/FLS Security Validation properly. If it was a permissions problem, I believe the manual test would have failed as well. Besides, I have created a permission set which is assigned to the user prior to running the commands. The object is accessible in the scratch org as well as by SOQL Query in Dev Console (User mode enabled). Therefore, I cannot understand the permissions error.

 

Permission set screenshot: https://i.imgur.com/17lDT9U.png & https://i.imgur.com/nwGkCXp.png4.permission_set.png

 

I perform these operations on a scratch org. The steps I follow are:

1. I create a scratch org.

2. I deploy the apex code, custom objects, permission set, metadata type, lightning components and everything in the package to the scratch org.

3. I apply permission set to the scratch org as without it, the user wouldn't have permission to access custom objects.

4. I run the tests manually via SFDX. All tests pass with more than the required code coverage.

5. I create a package. It is created successfully.

6. Then I create the package version which eventually fails while evaluating the code coverage.

 

I have tried searching across the web for the last two days. Finally, I decided to contact support. Your help in this regard would be appreciated. 

Thank you.

 

@All About AppExchange

2 respuestas
  1. 29 may 2023, 16:52

    Hi @Kermit Del Rosario, luckily I did. Someone in the partner community, as well as on salesforce StackExchange, provided some reasons behind these errors.

     

    So what was I missing? I was running the tests in system mode. However, some of my code was being run in user mode (security enforced). Creating a package version was failing due to permission restrictions for the user.

    Here's the solution: Following suggestions from sfdcfox at SFSE, I created a test user via @testSetup in the apex code and assigned the necessary permission set. Also, I added ApexTestAccess to the package directory and executed the code using System.runAs test user. Then the package version was created passing all the apex tests.

0/9000