function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AnonymouseAnonymouse 

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

Raj VakatiRaj Vakati
trigger someTask on Task (after insert, after update) {
	if(trigger.isupdate && trigger.isAfter){
        SomeClass.someMethod(Trigger.newMap, Trigger.oldMap);
    }
}


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 = newMap.get(someTask.Id).Id;
        	}
            else { // Do nothing
            }
        }
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
You Can try Map to abvoid SOQL inside the for loop
http://amitsalesforce.blogspot.in/2016/09/collection-in-salesforce-example-using.html
Sample code
public class SomeClass {

    public static void someMethod(Map<id, Task> newMap, Map<id, Task> oldMap) {

		Map<Id,Task> mapTask = new Map<Id,Task>([SELECT id,Owner.Name FROM Task WHERE id in:newMap.keySet() ]);
		
        for (Task someTask : newMap.values()) {
        	if (someTask.customField__c == null) {
                someTask.customField__c = mapTask.get(someTask.id).Id;
        	}
        }
    }
}

Let us know if this will help you
 
Narender Singh(Nads)Narender Singh(Nads)
Hi,
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.
AnonymouseAnonymouse
Hi Raj and Amit,

Thank you.



Hi Narender,

Yes, the custom field is a look up field.



Sincerely,
Jackie