You need to sign in to do that
Don't have an account?
Brooks Johnson 6
Help with Bulk Patterns
Hi Friends,
I have a method that updates a lookup field on the task object. The task field is updated based on the value in a lookup field on the Contact. I know it is inefficient and it fails my bulk test class. But I am not sure how to optimize the SOQL query to get my code under the 100 SOQL query limit. Any tips or pointers would be great. The tasks are getting passed into the Method through trigger.new in a trigger handler. I am sure that putting the WhoIds into a list and doing a query to get the contacts and add them to a Map is pretty sub-optimal.
I have a method that updates a lookup field on the task object. The task field is updated based on the value in a lookup field on the Contact. I know it is inefficient and it fails my bulk test class. But I am not sure how to optimize the SOQL query to get my code under the 100 SOQL query limit. Any tips or pointers would be great. The tasks are getting passed into the Method through trigger.new in a trigger handler. I am sure that putting the WhoIds into a list and doing a query to get the contacts and add them to a Map is pretty sub-optimal.
public static void assignTasksToRelationshipOwnersInsert(List<Task> taskList) { //assign contacts owner to task look up field Set<Id> contactIds = new Set<Id>(); for (Task t : taskList) { if (t.WhoId != null && t.Subject.contains('Pardot List Email')) { contactIds.add(t.WhoId); } } //create a map of contacts and regions //Fails SOQL limit here List<Contact> contactList = [ SELECT Id, New_Relationship_owner1__c FROM CONTACT WHERE Id IN:contactIds AND New_Relationship_owner1__c != Null ]; Map<Id, ID> contactMap = new Map<Id, Id>(); for (Contact c : contactList) { contactMap.put(c.Id, c.New_Relationship_owner1__c); } for (Task updatedTask : taskList) { if (contactMap.containsKey(updatedTask.WhoId) && contactMap.size() > 0) { updatedTask.Relationship_Owner__c = contactMap.get(updatedTask.WhoId); } } }
Integer numContacts = 200; 200 single insert for one synchronous transaction and the maximum is 150.
Total number of DML statements issued:150 max.
Calls to the following methods count against the number of DML statements issued in a request.
- Approval.process
- Database.convertLead
- Database.emptyRecycleBin
- Database.rollback
- Database.setSavePoint
- delete and Database.delete
- insert and Database.insert
- merge and Database.merge
- undelete and Database.undelete
- update and Database.update
- upsert and Database.upsert
- EventBus.publish
- System.runAs
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htmAll your code is bulkified excepted this insert in a loop probably.
A simple check when you overcome the governor limits is to look at all the DML statements in loops.
All Answers
This code is correct (even if incomplete: updatedTask not used) but it is the caller of the class that must be wrong and you don't have posted it.
I never thought the issue could be from the handler. Here is that part of the code.
can u give us the complete code for updateTaskLookup class ..
So for the only optimization, i can see rewrite the updateTaskLookup handler class and from the code, you shared so for noting is wrong except firing multiple methods on before insert
The Class
The Trigger Handler
And the test class
Thank you for taking the time to look at this.
Integer numContacts = 200; 200 single insert for one synchronous transaction and the maximum is 150.
Total number of DML statements issued:150 max.
Calls to the following methods count against the number of DML statements issued in a request.
- Approval.process
- Database.convertLead
- Database.emptyRecycleBin
- Database.rollback
- Database.setSavePoint
- delete and Database.delete
- insert and Database.insert
- merge and Database.merge
- undelete and Database.undelete
- update and Database.update
- upsert and Database.upsert
- EventBus.publish
- System.runAs
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htmAll your code is bulkified excepted this insert in a loop probably.
A simple check when you overcome the governor limits is to look at all the DML statements in loops.