+ Start a Discussion
a contratora contrator 

apex variable in SOQL

Hi Friends,

 

I am wondering why the following code works:

 

List<account> actlist = [ select id, name from account where name like 'xxx %'];

List<contact> contlist = [select id from contact where accountId = :actlist];

system.debug(contlist.size());

 

Actually I noticed the second line will be redered as "...where accountId IN :actlist".

 

Does anybody find the official reference regrading the syntax of "..where id = :list "  for me ?

 

Thanks !

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Go through this link... it will list where to use IN and where to use =

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_variables.htm


a contratora contrator

Hi Sam.  Thanks for reply.   I know this link and I know well how to use IN.  My point is why we can use "where id = : list"   instead of using "...where id IN :list".   There is no mention there.

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Its implicit. when you are using "=" meaning that you wanted to compare with a specific value in your query... and you will have to use "IN" in a value exists in a groupd/list of values. 

 

There is a point in the link given above... try to understand this statement...

 

"The value of the IN or NOT IN operator in WHERE clauses, allowing filtering on a dynamic set of values. Note that this is of particular use with a list of IDs or Strings, though it works with lists of any type."

a contratora contrator

Sorry I still think that statement is only about how the "IN" is working.   It only says in soql like "..where x IN :y",  x can only be ID while y can be any type of list.

 

It makes sense that " where accountId = :actlist[0].id"    but generally speaking id < > a list

:) :) :) :) :):) :) :) :) :)

Thanks to force.com. You are referencing y =: xList but you list has not a one value it has multiple values. So force.com automatically understand what you wanted to do with it. It automatically predict that you actually wanted y IN : xList but by mistake you have written y=:xList.

a contratora contrator

I don't know it is a bug or feature in Force.com.  You may need to thank me as well if this is a feature in fact  as Salesforce never mention it  :)

 

However, I don't agree this is a "good" feature because the code makes confusion and Force.com does not point it out.