Create a list view filter via apex for a custom object that would then be available to all users. Is this even possible ? and if so please provide a code example Thanks


3 Answers 3


Yes you can create list view from Apex. You can run the below code in the developer console to see a list view being created on a custom object. This is a little crude way of doing it. You can consume the Metadata API WSDL and then use that to create the list views, which is more cleaner way of doing it. :)

HTTP h = new HTTP();
HTTPRequest req = new HTTPRequest();
req.setHeader('Content-Type', 'text/xml');
req.setHeader('SOAPAction', 'create');

String b = '<?xml version="1.0" encoding="UTF-8"?>';
b += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
b += '<soapenv:Header>';
b += '<ns1:SessionHeader soapenv:mustUnderstand="0" xmlns:ns1="http://soap.sforce.com/2006/04/metadata">';
b += '<ns1:sessionId>' + UserInfo.getSessionId() + '</ns1:sessionId>';
b += '</ns1:SessionHeader>';
b += '</soapenv:Header>';
b += '<soapenv:Body>';
b += '<create xmlns="http://soap.sforce.com/2006/04/metadata">';
b += '<metadata xsi:type="ns2:ListView" xmlns:ns2="http://soap.sforce.com/2006/04/metadata">';
//This is the API name of the list view
b += '<fullName>Car__c.listView2</fullName>';
b += '<booleanFilter>1</booleanFilter>';
//Columns you want to display
b += '<columns>NAME</columns>';
b += '<columns>CREATED_DATE</columns>';
//Filterscope should be set to Everything for every one to be able to access this List view
b += '<filterScope>Everything</filterScope>';
// Enter the filter that you want to set
b += '<filters>';
b += '<field>NAME</field>';
b += '<operation>equals</operation>';
b += '<value>Mercedes </value>';
b += '</filters>';
b += '<label>Mercedes View</label>';
b += '</metadata>';
b += '</create>';
b += '</soapenv:Body>';
b += '</soapenv:Envelope>';

// Set this to org's endpoint and add it to the remote site settings.
HTTPResponse resp = h.send(req);
  • 1
    Very nice! Thank you. Upon seeing "metadata" in your post I finally uploaded Andrew Fawcett's metadata api package - the listview example worked perfectly so my thanks to you too, Andy.
    – max
    Commented Jan 19, 2014 at 1:47
  • 1
    Hey @max your very welcome, glad it worked out for you! :-) Commented Jan 19, 2014 at 3:44

Generally, no. There isn't an Apex API specifically for creating list views. List views are stored as Metadata. See the ListView Metadata documentation.

<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
. . .
        <label>All Mileages</label>
        <booleanFilter>1 AND 2</booleanFilter>
            <value>Eric Bristow</value>
        <label>My Mileages</label>
. . .

However, you could look at the answers to a question about accessing the Metadata API from Apex for a way to access the Metadata API from Apex and perform CRUD operations.

MetadataService.MetadataPort service =  MetadataServiceExamples.createService();
MetadataService.ListView listView = new MetadataService.ListView();
MetadataService.ListViewFilter filter = new MetadataService.ListViewFilter();
List<MetadataService.ListViewFilter> filterListToAdd = new List<MetadataService.ListViewFilter>();

listView.fullName = 'Lead.MyListView6';
listView.label = 'My List View 6';
filter.field = 'LEAD.COMPANY';
filter.operation = 'equals';
filter.value = 'EMEA';

listView.filters = filterListToAdd;
listView.filterScope = 'Everything';
listView.columns = new List<String> { 'Available_Deals__c','FULL_NAME','LEAD.COMPANY','LEAD.STATE','LEAD.EMAIL'};

List<MetadataService.SaveResult> results = service.createMetadata(new MetadataService.Metadata[] { listView });
System.debug('result is '+results[0]);
  • Try running this code in Developer Console
    – happy
    Commented Oct 22, 2015 at 13:28
  • I get this error when trying that: COMPILE ERROR: Invalid type: MetadataService.MetadataPort Commented Mar 19 at 9:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .