You need to sign in to do that
Don't have an account?
Anonymouse
How to bulkify query inside of another query efficiently
Hello.
How may I bulkify a query inside of another query efficiently? I realize that I should not query inside of a loop, but I'm afraid I don't know how to approach this. Any help would be greatly appreciated.
Trigger:
trigger someTask on Task (after insert, after update) { if(trigger.isupdate && trigger.isAfter){ SomeClass.someMethod(Trigger.newMap, Trigger.oldMap); } }
Class:
public class SomeClass { public static void someMethod(Map<id, Task> newMap, Map<id, Task> oldMap) { // Iterate through all of the new map values for (Task someTask : newMap.values()) { // Check if custom field is null if (someTask.customField__c == null) { // Populate next follow up by with contact name someTask.customField__c = [SELECT Owner.Name FROM Task WHERE id =: someTask.Id LIMIT 1].get(0).Id; } else { // Do nothing } } } }
Sincerely,
Jackie
http://amitsalesforce.blogspot.in/2016/09/collection-in-salesforce-example-using.html
Sample code
Let us know if this will help you
I am wondering, what if the customfield__c is of Text type and not a look up field, in that case the ID value will be populated instead of name.
Thank you.
Hi Narender,
Yes, the custom field is a look up field.
Sincerely,
Jackie