function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kiran Jain 15Kiran Jain 15 

Apex Method is throwing error when called it from Js and it's working fine when called it from anonymous window.

hy Experts,
I'm stuck in weird error. In calling a apex method from anonymous window,it's working fine but when i call it from js it's throwing error like this [Status=Unauthorized, StatusCode=401] .
I'm  little confused what's the main issue.
can somebody help me to solving it and tell me cause why it's throwing error.
I.m passing listViewId and getting listView Records.
Below is my Code
@AuraEnabled(cacheable = true)
    public static void getListviewFilters(String filterId) {
        HttpRequest req = new HttpRequest();
	    String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
	    String endPoinURL = baseUrl+'/services/data/v52.0/sobjects/Account/listviews/'+ filterId +'/describe';
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
	       Http http = new Http();
	       HttpResponse response = http.send(req);
        system.debug('response : ' + response);
	if( response.getStatusCode() == 200 ) {
		Map<String, Object> tokenResponse = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
		String query = (String) tokenResponse.get('query');
        List<Account> AccountList = new List<Account>();
        for(Account accountObj : database.query(query)){
		system.debug('accountObj : ' + accountObj);

Below is my Js Code
<aura:component controller="ListViewNewVersionInLwcCtrl">
    <aura:attribute name="firstName" type="String" default="world"/>
    <lightning:button label="Call server" onclick="{!c.echo}"/>


    "echo" : function(cmp) {
        var action = cmp.get("c.getListviewFilters1");
        action.setParams({ filterId : '00B5g000002WfNSEA0' });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                alert("From server: " + response.getReturnValue());
            else if (state === "INCOMPLETE") {
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                } else {
                    console.log("Unknown error");

Thanks In advance
Surya GSurya G
Hi Kiran,
I think there is a typo in method name calling from aura js.
var action = cmp.get("c.getListviewFilters1"); ---> methd name is 'getListviewFilters'
Try removing 1 appended to method name and see if that works.
Surya G
Kiran Jain 15Kiran Jain 15
Hy Surya,
Thanks for quick response but actually by mistake it stayed 1 in getListviewFilters1. Method name is getListviewFilters. please call apex method from  js and  then you will face the same error.
please solve it if it's possible.
Thank you