Showing posts with label List View Button. Show all posts
Showing posts with label List View Button. Show all posts

Monday, 23 March 2015

Calling Apex method from List View button having Parameters

Recently I was trying to call an Apex method in List view method and want to pass the parameters from the method from the List View button whatever the records have been selected.

You can say I am dump, but when I selected a single record in the List view it works perfect, but when I selected more then one, it started showing error. I tried putting alerts and tried to check why its not taking the multiple record Ids, this is the code I wrote

1:  //Fetch Id of the selected Record  
2:  var records = {!GETRECORDIDS($ObjectType.Contact)};   
3:  //Check for selected record size  
4:  if(records.length <= 4) {   
5:       //Calling Apex method  
6:       sforce.apex.execute("MyApexClass","myApexMethod", {ids:records });   
7:  } else {   
8:       alert('You can validate only 4 records at a time');   
9:  }  

Above code was not working with the multiple records, it was showing error


So finally I got the thing that RECORDS was returning a list of Ids of the selected records, then the code needs manipulation, I end up doing this below code and this was working like charm

1:  //Fetch Id of the selected Record   
2:  var records = {!GETRECORDIDS($ObjectType.Contact)};   
3:  if(records.length <= 4) {   
4:       var res = "";   
5:       var i;   
6:       //Loop through the records Id selected by the user  
7:       for (i = 0; i < records.length; i++) {   
8:            res += records[i] + ";";   
9:       }   
10:       sforce.apex.execute("MyApexClass","myApexMethod", {ids:res });   
11:  } else {   
12:       alert('You can validate only 4 records at a time');   
13:  }  

Happy Coding!!

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..!!!!