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

No comments:

Post a comment