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 using Salesforce API with C#. In the query that I am writing I am not sure how to pass a parameter to the select statement. My query works when it looks like this:

SOQL = "select AuthorId,Name, Description,Type from Document where AuthorId='005G0000003s4a8IAA'";

But I am not sure how to replace the value in AuthorId that is '005G0000003s4a8IAA' to a variable that I am passing to this method, like:

SOQL = "select AuthorId,Name, Description,Type from Document where AuthorId=" + id;

But this is not working. What is the correct format to pass the id variable as a query argument?

share|improve this question
up vote 13 down vote accepted

Since you are just building a string, you need to wrap the Id in single quotes.

SOQL = "select AuthorId,Name, Description,Type from Document where AuthorId='" + id + "'";

As was suggested in a comment, you should also escape any potentially harmful strings found in Id. I'm not familiar with the C# functions, but there should be some sort of URLEscape method.

share|improve this answer
Thank you so much it is working. – Alma Feb 20 '14 at 18:56
@nikoom please accept the answer if it solved your problem. – Phil B Feb 20 '14 at 19:01
Since this is not using Apex's parameter binding, the string should probably be sanitized in your C# code. At the very least, escape ' characters: \'. – Snowman Feb 20 '14 at 19:52

if the id is not an integer, please read about sql injection and parameter binding and/or sanitizing like suggested by John Gaughan.

share|improve this answer

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.