Share Your Experience With Others

Get List View Fields In Apex

  • Make sure before using the ui-api in apex, your Salesforce instance URL is saved in remote site settings
  • Get the list view id from url and pass it to apex and directly hit the ui-api with the list view id For example : String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm(); String restAPIURL = sfdcURL + ‘/services/data/v51.0/ui-api/list-info/00B5g00000MNw7FEAT’;
  • Get the list view id from url and query the list view name + the sobject name from ListView Object and pass it to the ui-api in apex For example : String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm(); String restAPIURL = sfdcURL + ‘/services/data/v51.0/ui-api/list-info/Opportunity/Test_List_View’;
  • Recent List View comes under Search Layouts so to update the fields for that you need to update the search layouts under the particular object
  • Using this we can fetch fields of those list view also which are only visible to me
  • After getting response you can parse it to use.
//Make sure your Salesforce instance URL is added in remote site settings


String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm();
String restAPIURL = sfdcURL + ‘/services/data/v51.0/ui-api/list-info/Opportunity/Test_List_View’;

HttpRequest httpRequest = new HttpRequest();
httpRequest.setMethod(‘GET’);
httpRequest.setHeader(‘Authorization’, ‘OAuth ‘ + UserInfo.getSessionId());
httpRequest.setHeader(‘Authorization’, ‘Bearer ‘ + UserInfo.getSessionID());
httpRequest.setEndpoint(restAPIURL);
String response = ”;
String fields=”;
try {
Http http = new Http();
HttpResponse httpResponse = http.send(httpRequest);
if (httpResponse.getStatusCode() == 200 ) {
response = JSON.serializePretty(JSON.deserializeUntyped(httpResponse.getBody()));
listViewParse jsonApex = listViewParse.parse(response);
for(listViewParse.cls_displayColumns col : jsonApex.displayColumns){
fields=fields + col.fieldApiName +’,’;
System.debug(‘fields details’+fields);
}
fields = fields.removeEnd(‘,’);
System.debug(‘final field list ‘+fields);
} else {
System.debug(‘ httpResponse ‘ + httpResponse.getBody() );
throw new CalloutException( httpResponse.getBody() );
}
} catch( System.Exception e) {
System.debug(‘ERROR: ‘+ e);
throw e;
}

Leave a comment