Below processes run under the Automated Process User Context
- Platform event triggers/flows/process.
- Approval Process Knowledge Actions.
You can use getSessionId() both synchronously and asynchronously. In asynchronous Apex (Batch, Future, Queueable, or Scheduled Apex), this method returns the session ID only when the code is run by an active, valid user. When the code is run by an internal user, such as the automated process user or a proxy user, the method returns null.
As a best practice, ensure that your code handles both cases(Internal/External Users and Automated Process User).
With the introduction of API version 41.0, “Automated Process User” was made available.
Sample Class:
public class Sample {
public static void fetchAutomatedUser() {
System.debug(
[
SELECT Id, Name, Alias
FROM User
WHERE Name = 'Automated Process'
]
);
}
}
Code to Execute for testing:
Sample.fetchAutomatedUser();
If the API version of the Apex Class is 41 and above, the SOQL returns values.
If the API version of the Apex Class is 40 and below, the SOQL doesn’t return any value.
Profile Name with UserInfo.getProfileId() is null:
Starting api version 41.0 there are 2 internal users which are exposed in test class context. These users does not have normal profiles which is resulting in the error.
https://help.salesforce.com/articleView?id=000319488&type=1&mode=1