+ Start a Discussion
mojavemojave 

query based on trigger.new

I would normally use a "for" loop to go through trigger-submitted data.  It works, but that kind of code won't pass the test in this case because it will generate too many queries.

 

So I'm trying to dump the results of a query into a list, but system doesn't like this, apparently because the trigger data is a list:

 

accts.add([select ID, custom__c from account where id=:trigger.new.Account__c]);

 

It's probably obvious that I'm missing something about how these mysterious bulk queries work....

 

 

Thanks,

 

Kevin

Best Answer chosen by Admin (Salesforce Developers) 
mojavemojave

Thanks a bunch!  I figured I must be getting close, but not close enough.  :)   Looks like I need to study the differences between sets and lists and stop thinking of everything in terms of generic arrays!

 

Kevin

All Answers

Srinivas_V2Srinivas_V2

Use it in this way

 

Set<id> idSet = new Set<Id>();

for(Integer i= 0 ; i < Trigger.new.size(); i++)

{

           idSet.add(Trigger.new[i].Account__c);

}

 list<Account> lstAccount = [select ID, custom__c from account where id in:idSet];

mojavemojave

Thanks a bunch!  I figured I must be getting close, but not close enough.  :)   Looks like I need to study the differences between sets and lists and stop thinking of everything in terms of generic arrays!

 

Kevin

This was selected as the best answer