6

We have a requirement where we have to delete list views. So I thought we could do something like this (this is similar to the create example mentioned here):

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 += '<delete xmlns="http://soap.sforce.com/2006/04/metadata">';
b += '<metadata xsi:type="ns2:ListView" xmlns:ns2="http://soap.sforce.com/2006/04/metadata">';
b += '<fullName>customObject__c.listView2</fullName>';
b += '</metadata>';
b += '</delete>';
b += '</soapenv:Body>';
b += '</soapenv:Envelope>';

req.setBody(b);
req.setCompressed(false);
req.setEndpoint('https://ap1.salesforce.com/services/Soap/m/25.0');
HTTPResponse resp = h.send(req);
System.debug(resp.getBody());

Even though when I run this in dev console, it succeeds but the list view is not deleted. listView2 is the name of the list view. Please advice on how to go about it. Thanks

2
  • How do you see them not deleted, in the gui or query ? (are they recycle bin subjected?) – Samuel De Rycke Dec 20 '13 at 19:20
  • In the UI, they still appear.. Also i checked the status of the async Id in the workbench and status had failed. – Sam Dec 20 '13 at 19:23
8
+50

I've updated the Metadata API Apex library you referenced in your question, to include examples on creating and deleting List Views. In writing the example I discovered two things.

  • Make sure it is not a personal list view these are not supported

    List views with the Visible only to me Restrict Visibility option are not accessible in Metadata API. Each of these list views is associated with a particular user.

  • Ensure that you pass in your delete request the FilterScope. You should not have to pass this for a delete request in my view, but it seems Salesforce requires it.
The following is my create example...

    MetadataService.MetadataPort service = createService();     
    MetadataService.ListView listView = new MetadataService.ListView();
    listView.fullName = 'Test__c.MyListView';
    listView.label = 'My List View';
    listView.filterScope = 'Everything';
    listView.columns = new List<String> { 'NAME' };
    MetadataCreateJob.run(
        new List<MetadataCreateJob.Item> { new MetadataCreateJob.Item(listView) },
        new MetadataCreateJob.EmailNotificationMetadataAsyncCallback());                        

NOTE: Example uses the MetadataCreateJob utility class, described here (there are other options). This ensures that any errors are retrieved and communicated back (in the default case via email).

The following is my delete example...

    MetadataService.MetadataPort service = createService();     
    MetadataService.ListView listView = new MetadataService.ListView();
    listView.fullName = 'Test__c.MyListView';
    listView.filterScope = 'Everything';
    MetadataService.AsyncResult[] results = service.deleteMetadata(new List<MetadataService.Metadata> { listView });                

In your case, if you want to stick with the raw XML approach, you should be able to replicate what is being done above by adding the FilterScope element at the same level as the fullName. If your still having issues run the examples above an review the XML output in the Debug logs to compare.

5
  • Note once Spring'14 arrives we have immediate versions of create and delete operations, meaning that it will become a lot easier to determine failures as they will be reported immediately and you will not have to implement polling approaches to retrieve error messages. – Andrew Fawcett Dec 23 '13 at 11:55
  • 1
    greased lightning! – Matt and Neil Dec 23 '13 at 11:57
  • 1
    Thanks Andrew. How would anyone know that we have to add the filterscope?? We actually had a task to delete ~35k list views from PROD and we didn't have the metadata class implemented there, so I thought I would run the above raw xml in dev console. But Since I couldn't get it running, I wrote a chrome extension to accomplish it. Anyways thank you very much. – Sam Dec 23 '13 at 13:20
  • Will award the bounty once it is enabled :) – Sam Dec 23 '13 at 13:23
  • Is it possible via HTTP request or only via SOAP call? – Łukasz Kojzar Nov 29 '18 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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