14

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

12

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.setMethod('POST');
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>';

req.setBody(b);
req.setCompressed(false);
// Set this to org's endpoint and add it to the remote site settings.
req.setEndpoint('https://ap1.salesforce.com/services/Soap/m/25.0');
HTTPResponse resp = h.send(req);
System.debug(resp.getBody());
| improve this answer | |
  • 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 Jan 19 '14 at 1:47
  • 1
    Hey @max your very welcome, glad it worked out for you! :-) – Andrew Fawcett Jan 19 '14 at 3:44
4

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">
. . .
    <listViews>
        <fullName>All_Mileages</fullName>
        <filterScope>all</filterScope>
        <label>All Mileages</label>
    </listViews>
    <listViews>
        <fullName>My_Mileages</fullName>
        <booleanFilter>1 AND 2</booleanFilter>
        <columns>NAME</columns>
        <columns>CREATED_DATE</columns>
        <filterScope>mine</filterScope>
        <filters>
            <field>NAME</field>
            <operation>equals</operation>
            <value>Eric Bristow</value>
        </filters>
        <filters>
            <field>City__c</field>
            <operation>equals</operation>
            <value>Paris</value>
        </filters>
        <label>My Mileages</label>
    </listViews>
. . .
</CustomObject>

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.

| improve this answer | |
2
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';
filterListToAdd.add(filter);

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]);
| improve this answer | |
  • Try running this code in Developer Console – happy Oct 22 '15 at 13:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.