0

Below is my code to retrieve my org's limits. However, access token refreshes every now and then. How can I get my own org's limits programmatically without using HttpRequest to avoid access token?

HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(Url.getSalesforceBaseUrl().toExternalForm()+'/services/data/v43.0/limits');
req.setHeader('Authorization','Bearer ' + UserInfo.getSessionId());

But, I get the error:

Session expired or invalid","errorCode":"INVALID_SESSION_ID

So I wanted to find another way to retrieve my org's limits.

  • What do you mean by "supposedly" is the line in your code the first or second version. And how are you running this code? Is it in a batch per chance. Named credentials are something you should look at as well – Eric Aug 9 '18 at 2:15
  • This is my real code. It's not in a batch. It's inside a Scheduled class. But I'm getting the Invalid/Expired Session ID error: HttpRequest req = new HttpRequest(); req.setMethod('GET'); req.setEndpoint(Url.getSalesforceBaseUrl().toExternalForm()+'/services/data/v43.0/limits'); req.setHeader('Authorization','Bearer ' +UserInfo.getSessionId()); – cookie Aug 9 '18 at 2:17
  • 1
    Why would you not put your "real" code in the question. Also scheduled is still async. You will need to use named credentials or review the existing answer on session ids in scheduled – Eric Aug 9 '18 at 2:18
  • Based on your suggested duplicate @eric, What if I'm going to make this a package and install this to client orgs? The similar question would work if you would only use this in 1 org and so you can define the client secret & client ID but how can you use that so that it would work with different orgs? – cookie Aug 9 '18 at 2:33
  • thats not what you asked – Eric Aug 9 '18 at 2:34

Browse other questions tagged or ask your own question.