Skip to content

Instantly share code, notes, and snippets.

@kiran-machhewar
Created April 2, 2014 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiran-machhewar/9938941 to your computer and use it in GitHub Desktop.
Save kiran-machhewar/9938941 to your computer and use it in GitHub Desktop.
/**
* ApexClass : PicklistController
* Description : Controller to the piklist component.
*/
global class PicklistController{
public String uniqueId {get;set;} //Uniquie id so that every components javscript code should be separate
/**
* CONSTRUCTOR
*/
public PicklistController(){
//Generating unique code
uniqueId = ''+Integer.valueOf(Math.random()*99999999);
}
/**
* @description : Method to populate picklist values
* @param : objectName SObject name from which options needs to be prepared.
* @param : valueField Decides which value from records goes to value field of option.
* @param : labelField Decides which value from records goes to label field of option.
* @param : fileter fields to be applied.
* @param : values for the fielter fields.
* @return : List of Data wrapper which holds value and label for options to be shown.
*/
@RemoteAction
global static List<Data> getOptions(String objectName, String valueField, String labelField, String filterFields, String filterValues) {
List<Data> options = new List<Data>();
options.add(new Data('','-None-'));
String query = 'SELECT '+valueField+', '+labelField+' FROM '+objectName ;
List<String> filterClauses = new List<String>();
if(filterFields.trim()!=''){
if(filterValues.trim()=='') return options;
List<String> fields = filterFields.split(',');
List<String> values = filterValues.split(',');
if(fields.size() == values.size()){
for(Integer index = 0; index<fields.size();index++){
if(values[index].trim()=='') return options;
filterClauses.add(' '+fields[index]+' = \''+values[index]+'\' ');
}
}else{
return options;
}
}
if(filterClauses.size()>0){
query +=' WHERE '+String.join(filterClauses,' AND ');
}
query +=' ORDER BY '+labelField;
for(SObject sobj : Database.query(query)){
options.add(new Data(sobj.get(valueField),sobj.get(labelField)));
}
return options;
}
/**
* ApexClass : It is the inner wrapper class
* Description : This holds value and label to be populated as options at client side.
*/
global class Data{
String value;
String label;
public Data(Object value, Object label){
this.value = String.valueOf(value);
this.label = String.valueOf(label);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment