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

2 Answers 2

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. –  nikoo m Feb 20 at 18:56
3  
@nikoom please accept the answer if it solved your problem. –  Phil B Feb 20 at 19:01
7  
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: \'. –  John Gaughan Feb 20 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

 
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.