String query = 'Select Id,Email,Name FROM Contact Where';
String clause = 'Mobile__c = '+accountCreate1.Custmobile__c;
List<Contact> conList = Database.query(query+clause);

Here account is Account object,but getting - System.QueryException: Variable does not exist: accountCreate1.Custmobile__c

  • 4
    Why are you even using a dynamic query here? – Adrian Larson Oct 3 '17 at 13:06
  • actually this is a small part of my code , i have written a general function for quering objects but i'am getting error while using decimal field in where clause. (Custmobile__c is a number field) – Rohit C Oct 3 '17 at 13:08
  • 3
    We're probably going to need more context here to be able to provide any help. About the only thing that I'm seeing is that you never attach your 'clause' to your query before trying to run it (and Custmobile__c is probably a string, which I think you'd need to wrap in escaped single quotes in your query). Are you certain that accountCreate1 is an Account? Where is it defined? That aside, I agree with Adrian here. Without additional context, the dynamic query here is unnecessary and is only causing you trouble. – Derek F Oct 3 '17 at 13:14
  • Sorry , corrected. Yes accountCreate1 is an account. – Rohit C Oct 3 '17 at 13:15
  • String clause = 'Mobile__c =\' '+accountCreate1.Custmobile__c + '\'' ; – NITHESH K Oct 3 '17 at 13:20

In Salesforce, Long Integer values are passed as a 32 bit integer into the query, unless you explicitly tell it to use a Double. Because of this, the maximum number you can query is limited by the maximum size of a 32 bit integer 2,147,483,647.

Below is the scenario which I tested on my Developer org. Both Contact and light002__Deal_Action__c has custom Mobile field defined as Number (10,0). I'm trying to retrieve light002__Deal_Action__c by building query dynamically and using light002__Mobile__c field on Contact.

Refer below code:

Contact con = [SELECT id,light002__Mobile__c FROM Contact WHERE id='0032800001DxCOj'];
Decimal phoneNumber = con.light002__Mobile__c.setScale(1);
String body = 'Select id, Name FROM light002__Deal_Action__c WHERE ';
String clause = 'light002__Deal_Phone__c = '+phoneNumber;
String finalQuery = body+clause;
List<light002__Deal_Action__c> lst = Database.query(finalQuery);


enter image description here

  • Thanks @Rohit Mourya , that worked , didnt knew that we need to do setScale. – Rohit C Oct 4 '17 at 10:21

So there is a hidden Gem in Dynamic SOQL , you can actually use dynamic variable with it.

That being said you can do something like this.

String mobileNumber = accountCreate1.Custmobile__c;
List<Contact> contacts = Database.query('SELECT Id FROM Contact WHERE Mobile__c =:mobileNumber');

Please note the variable is actually inside the string, and the Database.query method automatically does the proper binding for it.

  • N.B. to @rohitc - the bind variable can not use dot notation, e.g :a.b – cropredy Oct 3 '17 at 21:32
  • @Avidev9 , i had tried that but getting the same exception – Rohit C Oct 4 '17 at 4:31
  • then probably your code is not structured the way it should be, probably accountCreate1 is not accessible by the code. or there is datatype mismatch between Mobile__c and Custmobile__c @RohitC – Avidev9 Oct 4 '17 at 9:30
  • @Avidev9 , accountCreate1 is accessible & datatypes are matching.Thanks , problem is solved now. – Rohit C Oct 4 '17 at 10:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.