loading... Salesforce Cat

Sunday, November 16, 2014

Salesforce Access All Object List Views From Visualforce

Salesforce standard list views are one of the most useful built in features comes with the platform.They serve well for users reporting and filtering needs.Although Salesforce standard list views are great to provide end users access to them you have to either create tab for each of your objects or built custom Visualforce pages with <apex:Listviews> tag for each of your objects.Although above solutions work fine for org with limited number of objects as the number of objects grow you may find it bit unpractical.

In this post I am going to describe you how you can access all the custom object list views via one Visualforce page using Salesforce meta data api.



For this purpose i have created a Visualforce page named  ListViewManager and an Apex controller named
ListviewController.

ListviewController


 public class ListviewController {  
   public List<SelectOPtion> Objectlist {get;set;}  
   public String Selectedview {get;set;}  
   public String objectName {get;set;}  
   Map<String,String> prefixtoName {get;set;}  
   public ListviewController()  
   {  
     Objectlist = new List<SelectOption>();  
     prefixtoName = new Map<String,String>();  
     Objectlist.add(new SelectOption('','Select'));  
     //get all object meta details using meta data api  
     Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();  
     for(String sObj : gd.keySet()){  
       //get details of each object  
       Schema.DescribeSObjectResult r = gd.get(sObj).getDescribe();  
       //get only custom object with key perfix, there are some internal standard objects without key       //prefix. If you want standard objects also considered you must check them by name as all         //standard objects does't have list views  
       if( r.getKeyPrefix() != null && r.getKeyPrefix()!='' && r.getName().contains('__c'))  
       {  
        //Create a list of selectoptins with keyprefix as value and object label as label of selectoption  
        Objectlist.add(new SelectOption( r.getKeyPrefix(),r.getLabel()));  
        //maintain a map of keyprefix to object name  
        prefixtoName.put(r.getKeyPrefix(), r.getLabel());   
       }  
     }  
   }   
  public void callServer()  
  {  
   //get label of selected object  
   objectName = prefixtoName.get(Selectedview);  
  }  
 }  


ListViewManager

 <apex:page controller="ListviewController" >  
  <apex:Form >  
  <apex:SectionHeader title="ListView Manager"/>  
  <apex:pageblock id="ViewBlock" >  
   <apex:pageMessages ></apex:pageMessages>  
   <apex:pageblockSection title="List view Selector" >  
    <apex:pageblockSectionItem >  
    <apex:outputLabel value="Select Object"/>  
    <apex:SelectList value="{!Selectedview}" size="1">  
     <apex:SelectOptions value="{!Objectlist}"/>  
     <apex:actionSupport event="onchange" action="{!CallServer}" rerender="ViewBlock"/>  
    </apex:SelectList>  
    </apex:pageblockSectionItem>  
    <apex:outputLink value="/{!Selectedview}" target="_blank" rendered="{!objectName!= null && objectName!=''}">{!objectName}  List view </apex:outputLink>  
   </apex:pageblockSection>  
   </apex:pageblock>  
  </apex:Form>  
 </apex:page>  
.

  • drop-down lists the all custom object names in your org





  • Select the name of the object you want to access the list view of.



  • Upon selection of the object ,page will provide you with a link to access the standard list view



No comments:

Post a Comment