Skip to content

Instantly share code, notes, and snippets.

@CodisRedding
Created August 23, 2013 16:11
Show Gist options
  • Save CodisRedding/6321096 to your computer and use it in GitHub Desktop.
Save CodisRedding/6321096 to your computer and use it in GitHub Desktop.
global class autoCompleteController {
@RemoteAction
global static SObject[] findSObjects(string obj, string qry, string addFields) {
List<String> fieldList;
if (addFields != null) fieldList = addFields.split(',');
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Schema.SObjectType sot = gd.get(obj);
if (sot == null) {
return null;
}
String filter = ' like \'%' + String.escapeSingleQuotes(qry) + '%\'';
String soql = 'select id, Name';
if (fieldList != null) {
for (String s : fieldList) {
soql += ', ' + s;
}
}
soql += ' from ' + obj + ' where name' + filter;
if (fieldList != null) {
for (String s : fieldList) {
soql += ' or ' + s + filter;
}
}
soql += ' order by Name limit 20';
List<sObject> vals = new List<sObject>();
try {
vals = Database.query(soql);
}
catch (QueryException e) {
return null;
}
return vals;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment