2

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??

1 Answer 1

3

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .