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]