Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

1 Answer 1

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?? –  mwh Aug 26 at 8:27
    
@mwh Updated my answer. Backslashes are used for the escaping purpose. –  mast0r Aug 26 at 8:34
    
Thanks so much I understand now. Btw, wouldn't WHERE Name > :BName do the same thing? –  mwh Aug 28 at 13:55
    
@mwh Yep exactly. It will work. –  mast0r Aug 28 at 14:09

Your Answer

 
discard

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.