Created
August 23, 2013 16:11
-
-
Save CodisRedding/6321096 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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