Activity › Forums › Salesforce® Discussions › How to fetch list view columns without using MetaData Api Call?
Tagged: List View, Metadata API, Salesforce Apex, Salesforce SOQL
-
How to fetch list view columns without using MetaData Api Call?
Posted by Neha on April 8, 2016 at 12:00 PMWhen I click button on list view, I want to fetch list view column’s api name without using metadata api call.
Shuvam replied 3 years ago 5 Members · 6 Replies -
6 Replies
-
Hi Neha,
Metadata APi would be needed to be consumed if we are wanting to get the ListView Column names. Can I some more Insights to the requirement if this does not help answer your query. May be I would be of more of a help then.
- [adinserter block='9']
-
Hi Anup,
I need to access listview with non-admin user for fetching dynamic coloumn in respect of current listview.
-
Thanks Anup
Please refer below link for solution related to above query.
http://forcetalks.com/blog/running-metadata-api-in-apex-as-a-specific-user/
-
Hi Neha,
You can retrieve the records based on the list view id using HTTP request. It can be done without consuming WSDL. You can use below code for reference –
public static List<Account> getFilteredAccounts(String filterId){
HttpRequest req = new HttpRequest();
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
String endPoinURL = baseUrl+’/services/data/v32.0/sobjects/Account/listviews/’+filterId+’/describe’;
req.setEndpoint(endPoinURL);
req.setMethod(‘GET’);
req.setHeader(‘Authorization’, ‘Bearer ‘ + UserInfo.getSessionId());
Http http = new Http();
HTTPResponse response = http.send(req);
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)){
AccountList.add(accountObj);
}
return AccountList;}
In the above example, I have retrieved accounts based on the selected list view. Any other profile which is API enabled and has read access on the Account object can retrieve the records.
-
but this get only some fields not all the fields, why?
-
Fetching list view columns without using Metadata API in Salesforce can be tricky because the list view columns are not directly available via any standard object or SOQL query. However, you can use the ListView describe call from the REST API, which will give you the list view’s columns along with other information.
Here’s an example of how you might do this in Apex:
public class ListViewColumns {
public static void getListColumns() {
String sObjectApiName = ‘Account’; // replace with your sObject API Name
String listViewId = ’00BT0000001Kxmu’; // replace with your List View Id
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()
+ ‘/services/data/v53.0/sobjects/’
+ sObjectApiName + ‘/listviews/’
+ listViewId + ‘/describe’);
req.setMethod(‘GET’);
req.setHeader(‘Authorization’, ‘Bearer ‘ + UserInfo.getSessionId());
Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res.getBody());
}
}
The debug log will show the JSON response of the ListView describe call, which includes the columns in the list view.
Please note that this requires the user to have access to the list view and the sObject, and it involves a callout, so it can’t be used in triggers or synchronous processes that don’t allow callouts.
Log In to reply.