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

How best to access Partner/Metadata API from ApexJob?

Im trying to make both partner and Metadata API callouts from within apex jobs. but since the UserInfo.getSession() does not work within an apex job I need to authenticate.  Is there any way to use an OAuth named credential to do this,  I been try to find a way to get the access_token and/or refresh_token from a named credential but have not found a way.
ShashankShashank (Salesforce Developers) 
Please see if this helps:
Sorry,  this wont work these are nightly Jobs the sessions timeout.  

I ended up solving this using Apex callout to OAuth withing the start event (not out-of-box named credentials) to do it.  I would have like to find a way to use the native named credentials howoever was unsuccessful.

Its nice to have this all running inside SFDC, but another alternative would have been using an external scheduler running the batch via execute anonymous.