Loading

UserInfo.getProfileId() Returns Invalid Profile ID in Apex Test Class (API v41.0 and Later)

Udgivelsesdato: Jun 2, 2026
Beskrivelse

In Apex test classes, if you query the Profile object using UserInfo.getProfileId() as a SOQL filter condition while running under the Automated Process user context, the query returns no results starting from API version 41.0. This behavior does not occur in API v40.0 or earlier.
Starting in API v41.0, two internal system users — "Automated Process" and "Data.com Clean" — are exposed in the test class context. These users do not have standard Salesforce profiles (they are non-standard internal users used for system operations). When UserInfo.getProfileId() is called in their context, it returns a profile ID that does not match any standard Profile record, causing SOQL queries filtering on that ID to return no results.

Løsning

This behavior is Working as Designed (WAD). With the introduction of API v41.0, two internal users were added to the system: "Automated Process" and "Data.com Clean." These are non-standard users without normal profiles, used for internal operations. At API v41.0 and later, these users are available in the test class context and may be loaded instead of the expected user from API v40.0 or lower, causing the profile query to fail.
The fix is to create a test user in the Apex test class and run the profile-dependent logic within System.runAs(testUser), instead of relying on UserInfo.getProfileId() in the context of internal system users.

Non-Working Scenario (API v41.0+)

The following pattern fails because the test runs in the context of the "Automated Process" internal user, which has no standard profile: 

@isTest(SeeAllData = false)
private class ProfileCheckTest {
    static testMethod void testStartDate() {
        User usr = [SELECT Id FROM User WHERE Name ='Automated Process' limit 1];
        System.runAs(usr) {
            String profileName = [SELECT Name FROM Profile WHERE Id =: UserInfo.getProfileId() LIMIT 1].Name;
        }
    }
}


In this scenario, UserInfo.getProfileId() returns the non-standard profile ID of the Automated Process user. The SOQL query for Profile returns no results, causing the test to fail.

Working Scenario — Create a Test User

The correct approach creates a test user with an explicitly assigned profile, then runs the logic within that user's context:

@isTest(SeeAllData = false)
private class ProfileCheckTest {
    static testMethod void testStartDate() {
        User usr = new User();
        usr.ProfileId = [SELECT Id FROM Profile WHERE Name != 'System Administrator' limit 1].Id;                   
        usr.LastName = 'Test';
        usr.Email = 'test@test.com';
        usr.Username = 'test@test.com' + System.currentTimeMillis();
        usr.CompanyName = 'Salesforce';
        usr.Title = 'Title';
        usr.Alias = 'Roger';
        usr.TimeZoneSidKey = 'America/Los_Angeles';
        usr.EmailEncodingKey = 'UTF-8';
        usr.LanguageLocaleKey = 'en_US';
        usr.LocaleSidKey = 'en_US';

        System.runAs(usr) {
            String profileName = [SELECT Name FROM Profile WHERE Id =: UserInfo.getProfileId() LIMIT 1].Name;
        }
    }
}


n this scenario, a new test user is created with an explicitly assigned Profile ID. When System.runAs(usr) runs, UserInfo.getProfileId() returns the assigned profile ID — not an internal user's ID — so the SOQL query succeeds.
Note: The code samples above are examples. Review and modify them for your specific organization's requirements.

Vidensartikelnummer

000383816

 
Indlæser
Salesforce Help | Article