1

I am trying to use the apex metadata api I found here: https://github.com/financialforcedev/apex-mdapi

I want to get all of the filters and other properties of Lead Views. I looked at the example code for readListView()

I ran the following code on my org:

MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();  

MetadataService.ListView leadView = (MetadataService.ListView) service.readMetadata('ListView', new String[] { 'Lead.Test_Lead_View' }).getRecords()[0];

and when I ran it all the Lead Views I looked at had null filters, as well as basically all of the fields listed here https://www.salesforce.com/us/developer/docs/api_meta/Content/meta_listview.htm. It even had a null fullName this should definitely not be the case as these lead views definitely have filters set on them and I searched for them by fullName.

Once this had failed I tried to use the Metadata Retrieve Demo provided by that library to see if I had just failed to use the API properly. When I selected the ListView Metadata Type it successfully listed all of my List Views and I selected the one I wanted to retrieve, but there was also a "Metadata Folder" field and I have no idea what to set that field to, and without it set I get the following error: "null: Need to specify full name, Name:, Delimiter:."

Am I doing something wrong with either of these methods of getting Lead View metadata? Is there a better way I could get this information via Apex? My end goal is to be able to recreate a Lead View as an SOQL query.

1
  • You should probably post some brief code snippets to help us help you.
    – Adrian Larson
    Commented Mar 6, 2015 at 14:48

1 Answer 1

1

It actually works - if certain conditions are met. First try this code (which is similar to yours):

public class SFSEMetadataListView {
    public static void readListView() {
        MetadataService.MetadataPort            service         = createService();
        MetadataService.ListView                listView        = (MetadataService.ListView) service.readMetadata(
            'ListView', new String[] { 'Lead.MyListView' }
        ).getRecords()[0];
        System.Debug(JSON.serializePretty(listView));
        if(listView.columns!=null)
            for(String column : listView.columns)
                System.Debug('Column ' + column)
        ;
        if(listView.filters!=null)
            for(MetadataService.ListViewFilter filter : listView.filters)
                System.Debug('Filter ' + filter.field + ' ' + filter.operation + ' ' + filter.value)
        ;
    }
    public static MetadataService.MetadataPort createService() {
        MetadataService.MetadataPort            service         = new MetadataService.MetadataPort();
            service.SessionHeader               = new MetadataService.SessionHeader_element();
            service.SessionHeader.sessionId     = UserInfo.getSessionId();
        return service;
    }
}

Now run this via execute anonymous:

SFSEMetadataListView.readListView(); 

In case you saved the ListView "Visible only to me" it happens as you describe. But save it "Visible to all users":

enter image description here

Now the debug output changes to something like this (I reduced the output a bit):

17:38:29.538 (538524558)|USER_DEBUG|[221]|WARN|{
  "label" : "MyListView",
  "fullName" : "Lead.MyListView",
  "filterScope" : "Everything",
  "filters" : [ {
     "value" : "a",
     "operation" : "contains",
     "field_order_type_info" : [ "field", "operation", "value" ],
     "field" : "FULL_NAME",
  } ],
  "field_order_type_info" : [ "fullName", "booleanFilter", "columns", "division", "filterScope", "filters", "label", "language", "queue", "sharedTo" ],
  "division" : null,
  "columns" : [ "FULL_NAME", "LEAD.COMPANY", "LEAD.STATE", "LEAD.EMAIL", "LEAD.STATUS", "LEAD.CREATED_DATE", "CORE.USERS.ALIAS", "LEAD.UNREAD" ],
  "booleanFilter" : null,
}
17:38:29.538 (538681723)|USER_DEBUG|[221]|WARN|"Column FULL_NAME"
17:38:29.538 (538747807)|USER_DEBUG|[221]|WARN|"Column LEAD.COMPANY"
17:38:29.538 (538810820)|USER_DEBUG|[221]|WARN|"Column LEAD.STATE"
17:38:29.538 (538872452)|USER_DEBUG|[221]|WARN|"Column LEAD.EMAIL"
17:38:29.538 (538933417)|USER_DEBUG|[221]|WARN|"Column LEAD.STATUS"
17:38:29.538 (538994636)|USER_DEBUG|[221]|WARN|"Column LEAD.CREATED_DATE"
17:38:29.539 (539055597)|USER_DEBUG|[221]|WARN|"Column CORE.USERS.ALIAS"
17:38:29.539 (539117331)|USER_DEBUG|[221]|WARN|"Column LEAD.UNREAD"
17:38:29.539 (539292384)|USER_DEBUG|[221]|WARN|"Filter FULL_NAME contains a"
17:38:29.541 (541145813)|CODE_UNIT_FINISHED|execute_anonymous_apex
17:38:29.542 (542137493)|EXECUTION_FINISHED

This should come close to what you need.

Still I can't tell exactly, why it's not good enough to get a restricted visible ListView either. I've opened a followup question on this, since it raised my own interest now :-)

7
  • I ran your code on my test lead view which is set as "Visible to all users (Includes partner and customer portal users)" and got the same results as I did initially, all the fields are null.
    – user8246
    Commented Mar 6, 2015 at 22:31
  • Strange, in my test org it's clearly reproducible. So maybe more loose ends. Let's see if someone else might have an idea.
    – Uwe Heim
    Commented Mar 7, 2015 at 1:28
  • Btw, all fields null means the API can't find the view. Same happens with nonexistent names or inaccesible items. For me also list metadata is sensibel to visibility "all users" vs. "only me"
    – Uwe Heim
    Commented Mar 7, 2015 at 8:27
  • Ok interesting, I thought I had tested that but it does seem that putting something like 'Lead.blah' gives the same results as I am getting, the names I am using are what is returned as the FullName of the Lead View from listing the ListView metadata, which returns names of the format Lead.Name_Of_Lead_View, like in my initial example code I used 'Lead.Test_Lead_View', I have tried creating new lead views and none that I have tried have had any results, Is it possible that I have some permissions set wrong for my org? or is there another way to query for the Lead Views? like by Id?
    – user8246
    Commented Mar 7, 2015 at 11:25
  • Just to clarify I am running my code via the execute anonymous window in the developer console logged in as a user with the System Administrator profile on my developer account
    – user8246
    Commented Mar 7, 2015 at 12:22

You must log in to answer this question.

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