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
Mohan PalaniappanMohan Palaniappan 

How per-user named credential works when called from Asynchronous Apex?

We want to use Named credentials with per-user identity and JWT Token Exchange for API authentication from Salesforce to an external API. The user's email ($User.Email) or some other user info will be passed in the "Per User Subject". This will help the external service to identify the user accessing the functionality and apply some controls in their side.

We will be referring these named credentials from Apex code, so what will happen when we use this named credential from future or queueable apex that is triggered from a User context? Will it be able to still pick the correct $User details?
Best Answer chosen by Mohan Palaniappan
AbhishekAbhishek (Salesforce Developers) 
Asynchronous Apex is run as whichever user initiated it, so it should be able to retrieve user-specific data just fine

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks.