0

Following code, works fine when invoked by the flow while in debug mode, but as soon as I run the flow in custom app it doesn't work.

global with sharing class GovernorLimits {
    private class Limits {
    public integer max;
    public integer remaining;
}
    @InvocableMethod
    public static void CalculateGovernorLimit() {
        String fullFileURL = System.Url.getSalesforceBaseUrl().toExternalForm();
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        req.setHeader('Content-Type', 'application/json');
        req.setEndpoint('callout:My_Named_Credential/services/data/v53.0/limits');
        req.setMethod('GET');
        Http h = new Http();
        HttpResponse res = h.send(req);
        List<ezberp__Governor_Limits__c> governorLimits=new List<ezberp__Governor_Limits__c>();
        List<ezberp__Governor_Limits__c> governorLimitsToBeDeleted = [select id from ezberp__Governor_Limits__c];
        delete governorLimitsToBeDeleted;
        JSONParser parser = JSON.createParser(res.getBody());
        System.debug(UserInfo.getName());
        while (parser.nextToken() != null) 
        {
             if (parser.getCurrentToken() == JSONToken.FIELD_NAME 
                 && (parser.getText() != 'max' && parser.getText() != 'remaining'))
             {
                string header = parser.getText();
                parser.nextToken();
                Limits lim = (Limits)parser.readValueAs(Limits.class);
                ezberp__Governor_Limits__c governorLimit=new ezberp__Governor_Limits__c();
                governorLimit.ezberp__Max__c = lim.max;
                governorLimit.ezberp__Remaining__c = lim.remaining;
                governorLimit.Name = header;
                governorLimits.Add(governorLimit);
             }
        }
       insert governorLimits;
   }
}

This is the error I am getting

System.HttpResponse[Status=Unauthorized, StatusCode=401]

In debug mode I get successfull response:

System.HttpResponse[Status=OK, StatusCode=200]

2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Jan 21, 2022 at 19:19
  • When I am trying to invoke an apex code that is making an HTTP callout using flows. It works fine in debug mode but when I run the same flow in my custom app the flow is not able to make callout (HTTP REST), and I get Unauthorize response with status code 401. But in Debug mode I am getting ok status and status code 200 and I also getingt the desired result. This seems to me a bit of strange behavior. Jan 21, 2022 at 19:36

1 Answer 1

0

Issue is resolved!!!. The answer is to call the callout asynchronously :).

You must log in to answer this question.

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