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?