+ Start a Discussion
andremsalgadoandremsalgado 

SOQL structure issues, variables comparison

Hello Folks,

Actually this is more than a doubt than a discussion...

I've been working with Salesforce Development more than 03 years and I never get why to use ":" to do variables comparison, like the following :

Sample 1:
List<AnyObject__c> objC = [Select id FROM AnyObject__c WHERE year__c = '1994'];

Sample 2:
String yearSample = '1994';
List<AnyObject__c> objC = [Select id FROM AnyObject__c WHERE year__c =: yearSample];
Yes, I know the both Samples Work and I'm already accustomed to write this way... But it does really grinds my gears!!

Could anyone explain me why? And Kill my curiosity?

Wish all the best! Thank you in advance!

Best Answer chosen by andremsalgado
Vishant ShahVishant Shah
Sample 1 is a hardcoded filter value in your query 

in Sample 2, you are setting the filter for your SOQL at runtime and by giving a : you tell sfdc to pick value from a variable

Ta
Vish

All Answers

Vishant ShahVishant Shah
Sample 1 is a hardcoded filter value in your query 

in Sample 2, you are setting the filter for your SOQL at runtime and by giving a : you tell sfdc to pick value from a variable

Ta
Vish
This was selected as the best answer
Vishant ShahVishant Shah
If it kills your curiosity mark it a best answer :)
andremsalgadoandremsalgado
Hello Vish, 

Thanks for your response, so in fact I need to use the ":" cuz it's necessary for the platform compile my code?

I never found the documentation for this issue!

Regards!
Vishant ShahVishant Shah
You only need it if you are filtering your SOQL query using a variable not if its hardcoded like sample 1