function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MattD88MattD88 

Read access denied for *custom object* - Test Execution failure

Hi, I'm testing a controller extension, run as a standard System Admin user, but my test class fails with the error "Read access denied for IBOS_Request__c", which I don't understand since Sysem Admin has full access to the IBOS_Request__c custom object. Thoughts?

 

TEST CLASS:

@isTest
class TestIBOSRequest
{
    static testMethod void TestIBOSRequest()
    {
        User u_adm = TestUtils.getAdminUser('adm');
        insert u_adm;
        
        ApexPages.StandardController std = new ApexPages.StandardController(ir1);
        IBOSRequestContExt irce = new IBOSRequestContExt(std);
        
        system.runAs(u_adm)
        {
            std = new ApexPages.StandardController(new IBOS_Request__c());
            irce = new IBOSRequestContExt(std);
        }
    }
}

 TestUtils Class:

Public class TestUtils
{
 private static User getTestUser(String userName, String profileName)
    {
        String profileId;
        try
        {
            profileId = [SELECT Id FROM Profile WHERE Name like: '%'+profileName+'%' limit 1].Id;
        }
        catch(Exception ex)
        {
            System.debug(ex);
            System.assert(false, 'No profile exists with name '+ profileName);
        }
        User testUser = new User();
        testUser.LastName = 'test ' + UserName;
        testUser.Alias = UserName;  
        testUser.Email = UserName+'@accruent.com';
        testUser.Username = UserName+UserName+'@test.com';
        testUser.CommunityNickname = 'a'+UserName;
        testUser.TimeZoneSidKey = 'America/New_York';
        testUser.LocaleSidKey = 'en_US';
        testUser.EmailEncodingKey = 'ISO-8859-1';
        testUser.ProfileId = profileId;
        testUser.LanguageLocaleKey = 'en_US';
        testUser.IsActive = true;
        
        return testUser;    
    }
    
    public static User getAdminUser(String adminName)
    {
        return getTestUser(adminName, 'System Administrator');
    }
}

 

Devendra@SFDCDevendra@SFDC

Hi,

 

You can check two things here,

 

1. Make sure your profile has access to the object which you are using

2. Also check the field level security of the field and make sure your profile has read permission

 

Hope this helps :)

 

Thanks,

Devendra

 

MattD88MattD88

Hi Devendra, the user is of the standard System Admin profile. This profile has access to the IBOS_Request__c object, and all objects. And this profile has at lead read access to every field on that object.

JHayes SDJHayes SD

One thing you may want to try is to insert the IBOS_Request__c object prior to instantiating the standard controller.  Something like this:

 

        system.runAs(u_adm)
        {
	    IBOS_Request__c ir = new IBOS_Request__c(Name='TestIBOSRequest');
	    insert ir;
            std = new ApexPages.StandardController(ir);
            irce = new IBOSRequestContExt(std);
        }