+ Start a Discussion
K SrikanthK Srikanth 

Create list view from Apex

Hi,

I have a requriment to create list view from VF page.

Is there any way to create listview from apex
Best Answer chosen by K Srikanth
Dushyant SonwarDushyant Sonwar
You can create public listview from metadata api using apex class.



First you need to create remote site setting with your salesforce URL.

Then you need to create a connection in your class
 MetadataService.MetadataPort metadataservice = new MetadataService.MetadataPort();
metadataservice .SessionHeader = new MetadataService.SessionHeader_element();
metadataservice .SessionHeader.sessionId = UserInfo.getSessionId();

MetadataService.ListView listViewObj = new MetadataService.ListView();
listViewObj.fullName = 'Test_View';
llistViewObj.label = 'Test View';
llistViewObj.filterScope = 'Everything';
metadataservice.createMetadata(listViewObj );

You need to fewe modification in above code to create list view
 https://www.cloras.com/blog/creating-custom-object-and-custom-fields/

You need to fill out those fields that is required for listView
https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_listview.htm

Note : Private list view cannot be created using metadata API.

All Answers

VinayVinay (Salesforce Developers) 
Hi Srikanth,

You can use an apex:enhancedList on your Visualforce page.

Review below links which have working examples.

https://salesforce.stackexchange.com/questions/32067/custom-list-view-to-display-vf-page
http://sfdcsrini.blogspot.com/2015/01/how-to-use-apexlistviews-tag-in.html

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
K SrikanthK Srikanth
Hi Vinay, 

Thanks for response.

Those are to display listviews on visualfroce page from apex. I need to create new list view from apex.
Dushyant SonwarDushyant Sonwar
You can create public listview from metadata api using apex class.



First you need to create remote site setting with your salesforce URL.

Then you need to create a connection in your class
 MetadataService.MetadataPort metadataservice = new MetadataService.MetadataPort();
metadataservice .SessionHeader = new MetadataService.SessionHeader_element();
metadataservice .SessionHeader.sessionId = UserInfo.getSessionId();

MetadataService.ListView listViewObj = new MetadataService.ListView();
listViewObj.fullName = 'Test_View';
llistViewObj.label = 'Test View';
llistViewObj.filterScope = 'Everything';
metadataservice.createMetadata(listViewObj );

You need to fewe modification in above code to create list view
 https://www.cloras.com/blog/creating-custom-object-and-custom-fields/

You need to fill out those fields that is required for listView
https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_listview.htm

Note : Private list view cannot be created using metadata API.
This was selected as the best answer
K SrikanthK Srikanth
Thanks Dushyant.

Can we put id's as filter in list view?




 
Dushyant SonwarDushyant Sonwar
Yes we can add filter in listview using metadata API.

You need to create a ListViewFilter in which you need to define field, operation and value
example

Id (field) 
Equals (operation)
yourid (value)

Below you can check the url for more details
https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_listview.htm

Hope this helps!
 
K SrikanthK Srikanth
Hi Dushyant,

Can you help me how to create connection in class to excute above mentioned code.



Thanks.
Dushyant SonwarDushyant Sonwar

//this the code that will create connection

MetadataService.MetadataPort metadataservice = new MetadataService.MetadataPort();
metadataservice.SessionHeader = new MetadataService.SessionHeader_element();
metadataservice.SessionHeader.sessionId = UserInfo.getSessionId();
K SrikanthK Srikanth
For that code I am getting error like invalid type: Metadataservice.Metadataport
Dushyant SonwarDushyant Sonwar
First create the Metadataservice Class in your org.
https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataService.cls
Dushyant SonwarDushyant Sonwar
Also you can look for this class there are few examples of creating metadata such as objects,fields,listview etc using metadata api.
https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataServiceExamples.cls
This will surely help you.
K SrikanthK Srikanth
Fixed this, thankyou Dushyant.