You need to sign in to do that
Don't have an account?

database.query - how to execute?
Trying to do a simple swap to replace an SOQL statement in my Apex with something that is generated from a string.
Here is what I'm looking at:
Here is what I'm looking at:
String AccountSOQLString = 'Select ownerid from account where ownerid=:search.ownerid' // List<Account> ownerAccs = Database.query(AccountSOQLString); The above does not work. List<Account> ownerAccs = [Select ownerid from account where ownerid=:search.ownerid]; This does work. The aim is to eventually have the string built up with a series of IF statements (which in itself is another sticking point it seems). I'll start with the current sticking point though, any help?
String f1 = search.ownerid;
List<Account> ownerAccs = Database.query('Select ownerid from account where ownerid=' + f1);
Only variable references are allowed in dynamic SOQL/SOSL
List<Account> ownerAccs = Database.query('Select ownerid from account where ownerid = :f1');
also see the debug of the constructed soql before it got queried...
Can you pls post your full code.?
then only I will know where you getting search.ownerid etc
Thanks.
The owner ID is from here:
It's fine until I try to use dynamic SOQL.
Dynamic debug looks like this:
Select ownerid from account where ownerid=:005a0000007i3yCAAQ
Static looks like this:
select ownerid from account where ownerid = :tmpVar1
I think you should remove ':' from your string query.
I got same error message , the problem are missing ' ' for the id
Just do like this:
String query = 'SELECT Name FROM '+ObjectType+' WHERE ID =\'' + RecordID + '\' ';
Enjoyy!!