+ Start a Discussion
BrianWKBrianWK 

Passing filter criteria to query string in different class

I have a batch class that accepts a query string.

 

I"m testing the function and want to pass over a query to the batch for very specific records that I can test. I would like to pass over either a single id or a set.

 

Can I format a query string to include the single quotes necessary when formatting a filter for a single ID? I know I could modify the class to include another variable but I would rather not. I'm only doing this as a test and wouldn't actually use that variable ever again after the test.

 

For example:

 

I want: 
Select MyFied__c from Contract where id = '8003000000021gz';

but as a string literal:

string q = 'select MyField__c from Contract where id =' .... 

 

Best Answer chosen by Admin (Salesforce Developers) 
Platy ITPlaty IT

Use a backslash to escape the quotes like this:

string q = 'select MyField__c from Contract where id =\'8003000000021gz\''

 

All Answers

Platy ITPlaty IT

Use a backslash to escape the quotes like this:

string q = 'select MyField__c from Contract where id =\'8003000000021gz\''

 

This was selected as the best answer
BrianWKBrianWK

Thanks Jessie! You're my hero for the week.

 

I had tried the slashes at one point - I was just using them in the wrong place! Thank you very much!