Can some one please guide me where I am going wrong. For some reason i have to invoke the same instance. But i am getting

[Status=Unauthorized, StatusCode=401]

Error when I Debug.

public class FetchAccountUsingREST{

    public FetchAccountUsingREST(){

    public void onLoadActionMethod(){
        //get all list views

        httpRequest req = new HttpRequest();
        httpResponse resp = new HttpResponse();
        http httpMeth = new http();
        //String oAuthtt = 'Bearer' + UserInfo.getSessionID();

From Winter 2017, Salesforce has removed session ID. So Created one Dummy VFP and ran this chunk to get the session ID. If I Debug UserInfo.getSessionID(), It shows SESSION_ID_REMOVED


Later i passed the same session id in my controller.

        String oAuthtt = ' Bearer' + '00D90000000*********************glGTC4hC1YiEirY7FSeOFPmS17SXbIWzwFZGHTTY5Hre8Je40Zwas3o9P9N';
        req.setHeader('Authorization', oAuthtt );
        req.setHeader('Content-Type', 'application/json');


            resp = httpMeth.send(req);
            system.debug('resp >>'+resp);

        }catch(Exception e){
            system.debug('Exception >>'+e.getMessage());

I can do it other ways using Connected and Named Credentials, but I am curious to see where i am going wrong on this.

1 Answer 1


You don't need the Visualforce page. UserInfo.getSessionID() still works, you just cannot System.debug() a session ID - the logs will show SESSION_ID_REMOVED.

Your 401 error usually indicates that you have not created a Remote Site Setting for the instance your code is running on. Make sure to create a Remote Site Setting for https://ckdomaintest-dev-ed.my.salesforce.com to allow REST access from within the same instance.

You appear to be missing a space between 'Bearer' and the Session ID in your authorization header.

  • Thanks @David , But i already have a Remote Site Setting with value https://ckdomaintest-dev-ed.my.salesforce.com Jan 15, 2018 at 16:05
  • I am wondering why this field service snippet which calls into the same org is giving me a 401 error - I got it to work by transforming it into callout style URL with an Auth. Provider + Managed App. But it oddly requires each user initially to go into their personal settings and obtain the refresh token.
    – bkwdesign
    Feb 21, 2022 at 21:13
  • See my full question here
    – bkwdesign
    Feb 21, 2022 at 21:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .