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
U ChauhanU Chauhan 

Aura Component:- From a picklist if i select an object and from other picklist if i select listview for that object.I need to get all records of that listview in datatable


here is my code.
//Apex Controller
public class ListviewResultClass {
    
    @Auraenabled
    public static list<listview> getListViews(string obj){
        list<listview> allListViews=[select Name from listview where sobjectType=:obj];
        system.debug(allListViews);
        return allListViews;
    }
    @Auraenabled
    public static ListViewDataTable getListViewData(string obj,string listView_Name) {
        
        ListView listViewResult = [select id, developername,sobjectType  from listview where sobjectType=:obj AND Name=:listView_Name];
        system.debug(listViewResult);
        String endpoint = String.format(
            'https://proseraa5-dev-ed.my.salesforce.com/services/data/v50.0/sobjects/{0}/listviews/{1}/describe/',
            new String[] { listViewResult.sobjectType, listViewResult.id }
        );  
        
        HttpRequest req = new HttpRequest();
        req.setEndpoint( endpoint );
        req.setMethod( 'GET' );
        req.setHeader( 'Content-Type', 'application/json' );
        req.setHeader( 'Accepts', 'application/json' );
        req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
        HttpResponse res = new Http().send( req );
        System.debug( res);
        System.debug( res.getBody() );
        
        ListViewDescribeResult result = (ListViewDescribeResult)JSON.deserialize( res.getBody(), ListViewDescribeResult.class );
        system.debug('query :'+result.query);
        system.debug('deserialized result :'+result);
        
        //ApexPages.StandardSetController controller = new ApexPages.StandardSetController( Database.getQueryLocator( result.query));
        ListViewDataTable dataTable = new ListViewDataTable();
        dataTable.describeResult = result;
        dataTable.records = database.query(result.query);
        system.debug('record List===== :'+dataTable.records);
        return dataTable;  
    }
    
    public class ListViewDataTable {
        
        @Auraenabled
        public List<SObject> records { get; set; }
        
        @Auraenabled
        public ListViewDescribeResult describeResult { get; set; }
        
    }
    public class ListViewDescribeResult {
        
        @Auraenabled
        public ID id { get; set; }
        
        @Auraenabled
        public String query { get; set; }
        
        @Auraenabled
        public String sobjectType { get; set; }
        
        @Auraenabled
        public List<Map<String, String>> columns { get; set; }
        
    }      
}
************Component***************
<aura:component controller="ListviewResultClass" implements="force:appHostable,force:hasRecordId,flexipage:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name="ListView" type="List"/>
    <aura:attribute name="mydata" type="object"/>
    <aura:attribute name="mycolumns" type="List"/>
    <aura:attribute name="bShowListView" type="boolean" default="false"/>
    
    <div>
        <p class="title">Select Object</p>
        <lightning:select class="single" aura:id="InputSelectSingle" label=" " onchange="{!c.onSingleSelectChange}">
            <option text="None"/>
            <option text="Account"/>
            <option text="Contact"/>
            <option text="Lead"/>
            <option text="Opportunity"/>
        </lightning:select><br/><br/>
    </div>
    
    <div>
        <p class="title">Select List View</p>
        <lightning:select class="single" aura:id="InputSelectListView" label=" " onchange="{!c.onListViewChange}">
            <aura:iteration items="{!v.ListView}" var="item">
                <option text="{!item.Name}"/>
            </aura:iteration>
        </lightning:select><br/>      
    </div>
    
    <aura:if isTrue="{!v.bShowListView}">
        <lightning:datatable data="{!v.mydata}"
                             columns="{!v.mycolumns}"
                             keyField="id"
                             hideCheckboxColumn="true"/>
    </aura:if>
</aura:component>
***********Controller*************
({
    onSingleSelectChange : function(component, event, helper) {
        var selectCmp = component.find("InputSelectSingle").get("v.value");
        console.log(selectCmp);
        var action =component.get("c.getListViews");
        action.setParams({"obj":selectCmp});
        action.setCallback(this,function(response){
            var state=response.getState();
            console.log(state);
            if(state==="SUCCESS"){
                component.set("v.ListView",response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    },
    
    onListViewChange : function(component, event, helper) {
        component.set("v.bShowListView", false);
        var selectlist = event.getSource().get("v.value");
        var selectCmp1 = component.find("InputSelectSingle").get("v.value");
        console.log(selectlist);
        console.log(selectCmp1);
        var action=component.get("c.getListViewData");
        action.setParams({"obj":selectCmp1,"listView_Name":selectlist});
        action.setCallback(this,function(response){
            var state=response.getState();
            console.log(state);  //its giving error
            var result = JSON.stringify(response.getReturnValue());
            console.log('result2=='+result);
            if(state==="SUCCESS"){
                var displayColumns = response.getListViewData().describeResult.columns.filter( function( column ) { return column.hidden === 'false'; } ).map( function( column ) {
                return {
                    'label' : column.label,
                    'fieldName' : column.fieldNameOrPath ,
                    'type' : column.type
                };
            });

            component.set( 'v.mycolumns', displayColumns );
              component.set("v.mydata",response.getReturnValue().records);
               console.log(response.getReturnValue());
               component.set("v.bShowListView", true);
               
            }
        });
        $A.enqueueAction(action);
    },
   
})