Created
June 19, 2012 17:26
-
-
Save capeterson/2955412 to your computer and use it in GitHub Desktop.
Dynamic SOQL + var binding
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
Set<String> names = new Set<String>{'Me','Myself','I'}; | |
String query = 'SELECT id, name FROM User WHERE LastName IN :names'; | |
List<sObject> objs = Database.query(query); | |
System.debug(objs); |
A coworker wrote it and I was reviewing his code. My conclusion is that this is some form of black magic.
Or a more serious answer: Database.query must use some reflection-like magic under the hood to find any variable mentioned that's in scope at the time of the query and load it in.
Voodoo if you ask me ;) Technically it's an interpreted language right? No idea how it works with byte code though.
Actually, the platform now complies to Java bytecode. This is a fairly recent change.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How is this possible? That set variable is private and the query variable is just a String. I believe you, but this makes no sense.