1

I'm new to Programming and Salesforce. I had to retrieve the API Usage and Data Storage of the org programmatically. Is this even correct?

RestRequest req = RestContext.request;
String sysov = req.requestURI.substring(req.requestURI('/services/data/v37.0/limits/'));

1 Answer 1

1

To make a callout, you can use the HttpRequest class.

It looks like this:

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

You can check the documentation for more information.

7
  • Your code works. That's when my class is not scheduled. Now, I had to schedule it to retrieve those details every 2 hours. How to get around with your code if it's inside a scheduled class? I need to make this a package and install to client orgs. Will it be possible?
    – cookie
    Aug 9, 2018 at 3:13
  • 1
    @cookie Asynchronous code naturally doesn't have a session ID. You'd have to have each client set up a Named Credential back in to Salesforce. Try looking at this question for a quick description of how it's done (and there's a link to a blog post where someone did that).
    – sfdcfox
    Aug 9, 2018 at 3:59
  • I have checked the blog post and tried what he did. Unfortunately, my Authentication Status is still pending unlike his which has the "Authenticated as <his email>". How does this Status change from Pending to being Authenticated?
    – cookie
    Aug 10, 2018 at 2:53
  • 1
    @cookie You have to complete the authentication process. There's a thing on the named credential when you're editing saying "launch login flow after saving." You need to complete the process to make the named credential active.
    – sfdcfox
    Aug 10, 2018 at 3:02
  • Oh right. However, I'm getting this error "error=redirect_uri_mismatch&error_description=redirect_uri%20must%20match%20configuration" when saving even when my Connected APP Callback URL is the same with NamedCredential URL. Or maybe my URL is not valid login.salesforce.com/services/oauth2/token ?
    – cookie
    Aug 10, 2018 at 3:06

You must log in to answer this question.

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