I have an APEX class which is making a callout to REST service services/data/v50.0/smartdatadiscovery/predictionDefinitions.

When I run this code in debug anonymous window it works fine however, when I call the same APEX class from a FLOW - I get the following error:

16:35:25.56 (56520767)|NAMED_CREDENTIAL_RESPONSE|NamedCallout[Named Credential Id=0XA4K000000GneU, Named Credential Name=KBNamedCred, 
    Status Code=401, Response Size bytes=69, Overall Callout Time ms=23, Connect Time ms=6
16:35:25.25 (67198923)|CALLOUT_RESPONSE|[13]|System.HttpResponse[Status=Unauthorized, StatusCode=401]``` 

The class has invocable method:

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('Authorization', 'Bearer '+UserInfo.getSessionId());
HttpResponse response = http.send(request);
  • could you find something worthy? I see when you execute this method "UserInfo.getSessionId()" It returns a null when It comes from a Flow and that's why you have a statuscode of 401. Aug 17 '21 at 23:38

Your log indicates that you are getting 401 in the NAMED_CREDENTIAL_RESPONSE. Is the Named Credential KBNamedCred Authorized? In Salesforce Setup, search for Named Credential and look at KBNamedCred to see what is in the "Authentication Status" field. It should display the email of the authenticator. If it says "Pending" then someone needs to authorize it. If you are using Password Authentication, the person who originally authenticated it may have changed their password. If you are using OAuth 2.0 Protocol, the refresh token may have expired and you need to re-authenticate.

Authentication Provider Salesforce Documentation

Named Credential Salesforce Documentation

New contributor
rhopper is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.