You need to sign in to do that
Don't have an account?
aKallNV
SOQL query only finding one record when it should be finding several
Select ID, Name, Strategy__c, ActionItem__c, ActionItem__r.Name, ItemNumber__c from nvpsStrategyActionItem__c where Strategy__c IN :stIDs
I am using this query in a soql for loop to create a map. Through testing in the sandbox interface, I am expecting this to return 6 records but it only finds one, which is one of the records it is supposed to find.
Strategy__c is a Lookup field, and stIDs is a list of ids for the object it is looking up to. The query is basically trying to pull all of the child records of a parent record. Can anybody spot why this isn't working correctly?
thanks!
In this section of code:
for(nvpsStrategyActionItem__c aiTemplate : [select ID, Name, Strategy__c, ItemNumber__c, ActionItem__r.Name from nvpsStrategyActionItem__c where Strategy__c IN :stratyTempIDs]) { List<nvpsStrategyActionItem__c> aitemps = new List<nvpsStrategyActionItem__c>(); aitemps.add(aiTemplate); aiTemplates.put(aiTemplate.Strategy__c, aitemps); }
You are still overwriting the value for the key, just with a single element list rather then the single element. You'll need to check if there is an existing value in the map for the key and append to that.
Here's some revised code that should do the trick:
All Answers
The query itself looks fine. Can you post any more of the code? Its a little difficult to suggest anything without knowing how you have generated the stIDs list.
One more thing - as you are generating a map, what are you using the key the entries in the map? If the key is the same for each returned record, they will just overwrite each other.
I think your last post is the solution, but I'm not quite sure yet what to do about it. I'm going to think about it a little and post back here if I struggle.
Thanks!
Cool. One tip in this scenario is to make the values in your map a list rather than a single instance, then you can have multiple values associated with a single key.
Ok. So I got a chance to work on this tonight, and thought I had worked out based on your advice, but it does the same **bleep** thing. So here is all of the code.
Trigger:
Class:
In this section of code:
for(nvpsStrategyActionItem__c aiTemplate : [select ID, Name, Strategy__c, ItemNumber__c, ActionItem__r.Name from nvpsStrategyActionItem__c where Strategy__c IN :stratyTempIDs]) { List<nvpsStrategyActionItem__c> aitemps = new List<nvpsStrategyActionItem__c>(); aitemps.add(aiTemplate); aiTemplates.put(aiTemplate.Strategy__c, aitemps); }
You are still overwriting the value for the key, just with a single element list rather then the single element. You'll need to check if there is an existing value in the map for the key and append to that.
Here's some revised code that should do the trick:
Yes! Thank you Bob.