Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to a Salesforce organisation and currently I am trying to make sense of the organisation's apex code. I came across this query and for the life of me I can't figure out what it does. This is probably a pretty noob question but my experience with queries are pretty limited and it would be a great help if anyone can figure out and explain what the query does.

Query='SELECT Name, Id 
         FROM Form__c 
        WHERE Name > \"+ BName+'\' 
          AND Candidate__c =\"+ candidateID +'\' 
        ORDER BY Name ASC'

For context, all of the variables above are strings (even the ids).

I have googled the syntax for SOQL but in this case the backslashes for the single and double quote characters and random + signs are really throwing me off here. Does anyone know what the query does??

share|improve this question
up vote 3 down vote accepted

This query returns a list of sObjects from the database. Each object will contain only two fields: Name and Id. There will be only objects for a particular candidate with Id equals to candidateID and his/her name need to be greater than BName string.

Finally this list will be sorted by Name field in ascending (ASC) order. The backslashes are used here for escaping purpose. The query is sent as a string to the compiler, so all "injected" charactes need to be escaped.

Example apex code -> generated query:
WHERE Name > \''+ BName+'\' will to WHERE Name > 'Smith'
Read more about ascaping here

You can read more about SOQL: Salesforce SOQL Reference.

share|improve this answer
Hi, thanks for the clarification. Does this mean that the backslashes for the single and double quote characters and concatenations do nothing?? – render Aug 26 '14 at 8:27
@mwh Updated my answer. Backslashes are used for the escaping purpose. – Sergey Utko Aug 26 '14 at 8:34
Thanks so much I understand now. Btw, wouldn't WHERE Name > :BName do the same thing? – render Aug 28 '14 at 13:55
@mwh Yep exactly. It will work. – Sergey Utko Aug 28 '14 at 14:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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