Using the Tooling API I need to fetch All Workflow rule Details. My below code work perfectly on the developer console Anonymous Window, but in LWC when I envoke the method with @wire shows me an error 'This session is not valid for use with the REST API errorCode INVALID_SESSION_ID'. What to do?

Note: I assume that the Error is because tooling API runs in future when it gets the free salesforce Resource and in future, Salesforce is not able to identify the Session-Id even if the same user is logged in. I don't know I am right or wrong?

My syntax

    public static  Map<String, Object> GetWorkflowRuleList(String userId)


            return null;

        String sToken=UserInfo.getSessionId();        
        Map<String, Object> flowJsonData = new Map<String, Object>();
        String body = null;

            String toolingSOQL='/services/data/v36.0/tooling/query?q=Select+Id,Name,TableEnumOrId,createdById,lastModifiedById+From+WorkflowRule';
            String baseURL=URL.getSalesforceBaseUrl().toExternalForm();
            String endPoint=baseURL+toolingSOQL;
            Http h = new Http();
            HttpRequest hr = new HttpRequest();
            hr.setHeader('Authorization', 'Bearer ' + sToken);
            hr.setHeader('Content-Type', 'application/json');
            HttpResponse r = h.send(hr);

            if(r.getStatusCode() == 200){
                body= r.getBody();                
            } else {
                throw new CalloutException(r.getBody());
        } catch(Exception ex){
            throw ex;

            flowJsonData = (Map<String, Object>)JSON.deserializeUntyped(body);
            return flowJsonData;
        } else {
            return null;

How to avoid INVALID_SESSION_ID error? Without using tooling API is there any way to fetch detailed data for workflow rules?

2 Answers 2


You can use the visualforce page session to access tooling ID

Create a visualforce page as shown below,

<apex:page contentType="application/json"> {!$Api.Session_ID} </apex:page>

Controller to fetch the session ID

public inherited sharing class SessionHelper { 

   public static String getSessionId()  {
     return Test.isRunningTest() ? '' :  Page.session.getContent().toString().trim(); 

You can obtain the session using SessionHelper.getSessionId()

  • thanks it's worked perfectly.
    – shamim
    Jul 5, 2021 at 3:40

You need to use named credentials for making this call. The session id in LWC cannot be used for accessing API by design due to security considerations.

Reference: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_api_calls.htm#apex_api_calls

You must log in to answer this question.

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