1

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

@AuraEnabled(cacheable=true)
    public static  Map<String, Object> GetWorkflowRuleList(String userId)
    {

        if(String.isEmpty(userId))
        {

            return null;
        }

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


        try{        
            
            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.setTimeout(60000);
            hr.setHeader('Content-Type', 'application/json');
            hr.setEndpoint(endPoint);
            hr.setMethod('GET');
            HttpResponse r = h.send(hr);

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

        if(!String.isBlank(body)){
            flowJsonData = (Map<String, Object>)JSON.deserializeUntyped(body);
            System.debug('ResponseBody:::'+flowJsonData);
        }
        if(!flowJsonData.isEmpty()){
            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?

1

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()

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

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

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.