You need to sign in to do that
Don't have an account?
PaulJS
SOQL query omitting duplicate values in trigger
I have a trigger in which I am assigning IDs to a list when the trigger is fired. When I run a query against this list using WHERE ID IN:, the query does not return any duplicate values, which is causing a list index out of bounds error.
Here is my code for the assignment of the IDs to the initial list.
List<String> whatIDs = new List<String>();
for (Task tk : Trigger.new){
whatIDs.add(tk.whatID);
}
And here is my code querying another object where the ID is found in that list.
List<Object1__c> oblist = [SELECT ID, Name
FROM Object1__c
WHERE ID IN :whatIDs];
During testing, I had a list of 26 records in my initial list (whatIDs), but because my query is not returning duplicate values (there is one), I am only getting back 25 records.
Can anyone offer any suggestions/help?
Thanks.
-Paul
Here is my code for the assignment of the IDs to the initial list.
List<String> whatIDs = new List<String>();
for (Task tk : Trigger.new){
whatIDs.add(tk.whatID);
}
And here is my code querying another object where the ID is found in that list.
List<Object1__c> oblist = [SELECT ID, Name
FROM Object1__c
WHERE ID IN :whatIDs];
During testing, I had a list of 26 records in my initial list (whatIDs), but because my query is not returning duplicate values (there is one), I am only getting back 25 records.
Can anyone offer any suggestions/help?
Thanks.
-Paul
Map<id, List<Task>> obj1toTaskMap = new Map<id, List<Task>>();
for (Task tk : Trigger.new){
List<Task> associatedTask obj1toTaskMap.containsKey(tk.whatID) ? obj1toTaskMap.get(tk.whatID) : new List<Task>();
associatedTask.add(tk);
obj1toTaskMap.put(tk.whatID, associatedTasks);
}
List<Object1__c> oblist = [SELECT ID, Name
FROM Object1__c
WHERE ID IN :obj1toTaskMap.keyset()];
then you run a double loop to process
for (Object1__c obj1 : oblist){
for(Task tsk : obj1toTaskMap.get(obj1.id)) {
//do stuff
}
}
All Answers
Map<id, List<Task>> obj1toTaskMap = new Map<id, List<Task>>();
for (Task tk : Trigger.new){
List<Task> associatedTask obj1toTaskMap.containsKey(tk.whatID) ? obj1toTaskMap.get(tk.whatID) : new List<Task>();
associatedTask.add(tk);
obj1toTaskMap.put(tk.whatID, associatedTasks);
}
List<Object1__c> oblist = [SELECT ID, Name
FROM Object1__c
WHERE ID IN :obj1toTaskMap.keyset()];
then you run a double loop to process
for (Object1__c obj1 : oblist){
for(Task tsk : obj1toTaskMap.get(obj1.id)) {
//do stuff
}
}