3

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/HwzHauB.png

Class Code:

/**
     * @description - method to load the history from objects in order to display data in LWC.
     * @return list of records from the httpeakdev1__Lazy_Admin_Search_Log__c object.
     */
    @AuraEnabled(cacheable=true)
    public static List<httpeakdev1__Lazy_Admin_Search_Log__c> getHistory(){
            return [select Id , Name , httpeakdev1__SOQL_Query__c , httpeakdev1__Is_Valid__c , 
            httpeakdev1__Created__c , httpeakdev1__Last_Modified__c , CreatedDate from 
            httpeakdev1__Lazy_Admin_Search_Log__c WITH SECURITY_ENFORCED ORDER BY CreatedDate DESC];
      
    }

Test Method:

/**
     * @description - This metod tests to fetch an Search History Records.
     */
    @isTest
    static void testGetHistory() {
        // create test data
        List<httpeakdev1__Lazy_Admin_Search_Log__c> logs = new List<httpeakdev1__Lazy_Admin_Search_Log__c>();
        for (Integer i = 0; i < 10; i++) {
            httpeakdev1__Lazy_Admin_Search_Log__c log = new httpeakdev1__Lazy_Admin_Search_Log__c(
                Name = 'Log ' + i,
                httpeakdev1__SOQL_Query__c = 'SELECT Id, Name FROM Account WHERE Name = \'Test\'',
                httpeakdev1__Is_Valid__c = true
            );
            logs.add(log);
        }
        insert logs;
        
        // call the method
        List<httpeakdev1__Lazy_Admin_Search_Log__c> history = LazyAdminSearchLog.getHistory();
        
        // assert the results
        System.assertEquals(10, history.size(), 'History should contain 10 records');
        System.assertEquals('Log 1', history[1].Name, 'Most recent log should be at the beginning of the list');
    }
    

When I run the Apex tests manually, all tests succeed and no errors appear. (Screenshot: https://i.imgur.com/v0dOpbd.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.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.

5
  • Could you please edit the question adding the code of the test method? Please do not add screenshots of code or error messages, you should add code snippet instead.
    – RubenDG
    May 27, 2023 at 15:55
  • I'll take care of this from now on. Updating the question with test code May 28, 2023 at 8:14
  • 1
    Since you created no user in the test method, you should either follow the sfdcfox's advice or you could edit sfdx-project.json file adding apexTestAccess setting as per docs. If you have several permission sets, you might want to test what happens when different users (with different permission) run the same code, in this case you have to create the right users in the test methods and use System.runAs, instead of assigning every permission in sfdx-project.json file
    – RubenDG
    May 28, 2023 at 13:46
  • Thanks @RubenDG May 29, 2023 at 11:39
  • I followed the instructions from sfdcfox as well as yours. Both ways seem to work out. :) May 29, 2023 at 13:18

1 Answer 1

2

Your unit tests should use System.runAs to run as a test user that is created during the unit test (typically via @testSetup). After this user is created, you can then add a PermissionSetAssignment for that user so it has the correct permissions. Finally, your unit tests (@isTest methods) should be able to correctly get code coverage.

2
  • Thanks. I'll give it a try and let you know the outcome. Didn't know running tests would require a creation of test user. May 28, 2023 at 8:13
  • Hi @sfdcfox, Thanks for the help. I followed your instructions and turns out that's all I needed to do. :) May 29, 2023 at 11:33

You must log in to answer this question.

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