When trying to call the toolingAPI from the toolingAPI class. I get the following error VARIABLE_ASSIGNMENT [173]|this|{"value":[{"message":"This session is not (31 more) ...","errorCode":"INVALID_SESSION_ID"}]}|0x533e4dd5
. I have created a Connected App, Auth Provider and Named Credential as described in https://salesforcecodex.com/2020/05/call-tooling-api-from-lightning-web-component/ . Attached is my Apex class and the lwc js.
Apex class
public with sharing class toolingAPI {
private static String baseUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v48.0/tooling/';
@AuraEnabled(cacheable=true)
public static List<ApexClassObject> getMethods() {
System.debug(baseUrl);
HTTPRequest req = new HTTPRequest();
req.setEndpoint(baseUrl + 'query/?q=SELECT+id,+SymbolTable,+FullName+FROM+ApexClassMember');
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
Http h = new Http();
HttpResponse res = h.send(req);
// Response to a create should be 201
List<String> names = new List<String>();
List<ApexClassObject> ApexClasses = new List<ApexClassObject>();
if (res.getStatusCode() == 201) {
System.debug(res.getBody());
names.add('error');
return ApexClasses;
}
else {
Integer total = integer.valueOf(new JSONParse(res.getBody()).get('size').getStringValue());
for(Integer i = 0; i < total; i++){
if(new JSONParse(res.getBody()).get('records.[' + i + '].SymbolTable').getValue() != null){
String name = new JSONParse(res.getBody()).get('records.[' + i + '].SymbolTable.name').getStringValue();
String test = new JSONParse(res.getBody()).get('records.[' + i + '].Id').getStringValue();
ApexClassObject apexClass = new ApexClassObject();
Id myId = Id.valueOf(test);
apexClass.FullName = name;
apexClass.myID = myId;
List<ApexClassObject.method> methods = new List<ApexClassObject.method>();
for(JSONParse node : new JSONParse(res.getBody()).get('records.[' + i + '].SymbolTable.methods').asList()){
ApexClassObject.method method = new ApexClassObject.method();
List<ApexClassObject.param> params = new List<ApexClassObject.param>();
method.FullName = node.get('name').getStringValue();
for(JSONParse child : node.get('parameters').asList()) {
ApexClassObject.param param = new ApexClassObject.param();
param.Name = child.get('name').getStringValue();
param.Type = child.get('type').getStringValue();
params.add(param);
}
method.params = params;
methods.add(method);
}
apexClass.methods = methods;
ApexClasses.add(apexClass);
}
}
system.debug(ApexClasses);
return ApexClasses;
}
}
}
apexClasses.js
import { LightningElement, wire, track } from 'lwc';
import getMethods from '@salesforce/apex/toolingAPI.getMethods'
export default class ApexClasses extends LightningElement {
@track methods;
@track error;
handleLoad() {
getMethods().then(result => {
this.methods = result
}).catch(error => {
this.error = error
})
}
}