Thursday, 12 September 2013

Concept Of StandardSetController For List View Buttons


Why StandardSetController Is Used?
StandardSetController is used when User wants to play with List view records, selected records can be easily called by StandardSetController, just you need to define and declare it.

We can use StandardSetController as below When Using VF List View Button

1:   //Defining standardSetcontroller  
2:      private ApexPages.StandardSetController setController;  
3:      //StandardSetController in Constructor  
4:      public ListViewButtonCreatePDF(ApexPages.StandardSetController setController) {  
5:           this.setController = setController;  
6:      }  
7:      //Method  
8:      public PageReference sobjectSelectedRecords() {  
9:      //List of sObjects  
10:      public List listOfObjects { get; set; }  
11:      //Get selected records  
12:      listOfObjects = (List)standardController.getSelected();   
13:      }  

but if we want to call that page where we are using StandardSetController by a JavaScript List View Button then we cant use this concept of StandardSetController, Because when are using Javascript button then method "getSelected()" stops working.

This is because when we use visualForce button to call our page then there is a direct call between page and the VF button so the above snippet works fine.

So here is the other way to use the StandardSetController for Javascript List View Button

1:  //Defining StandardSetController in Constructor  
2:      public ListViewButtonCreatePDF(ApexPages.StandardSetController setController) {  
3:      listOfObjects = new List();   
4:      //List of Ids  
5:      List ids = new List();  
6:      //Fetch Ids from URL  
7:      String conId = ApexPages.currentPage().getParameters().get('id');   
8:      //Split string     of Ids  
9:      for(String str : conId.split(',')) {  
10:      ids.add(str);  
11:      }  
12:      //Query  
13:      listOfObjects = [Select Id From Contact Where Id =: ids];   
14:      //Paasing List of Contact from StandardSetController  
15:      setController = new ApexPages.StandardSetController(listOfObjects);   
16:      //Set selected records  
17:      setController.setSelected(listOfObjects);   
18:      }  

And then we can use this javascript code for the button when using the above standardSetController concept. In this code we are setting the selected Ids of the records in the URL, and then we can fetch Ids from the URL in the class.

Snippet For Javascript List View Button

1:  //This "records" will have all the Ids of the selected records  
2:      var records = {!GETRECORDIDS($ObjectType.Contact)};  
3:      if (records[0] == null ) {  
4:      alert("Please select at least one record.")  
5:      } else {  
6:      //set "records" to the URL  
7:      window.location = '/apex/YOUR-VF-PAGE?Id=' + records;  
8:      }  

Hope I have helped Some One

Happy Coding..!!!!


1 comment:

  1. Great post. Very helpful for learning more Apex, thanks for posting!

    ReplyDelete