You need to sign in to do that
Don't have an account?

How to Bulkify a SpecificTrigger?
I am getting an error
System.Exception: Too many SOQL queries: 101
I have the followiing trigger which I am attempting to change to avoid that error message.
I have tried using a keyset with this but it has not worked out yet. The tricky part is that I am updating an opportunity from a task, and there are also triggers on opportunities.
System.Exception: Too many SOQL queries: 101
I have the followiing trigger which I am attempting to change to avoid that error message.
I have tried using a keyset with this but it has not worked out yet. The tricky part is that I am updating an opportunity from a task, and there are also triggers on opportunities.
trigger UpdateLatestDueDatefromTask on Task (after insert, after update) { try{ //the trigger will update the opportunity's latest_open_due_date__c field for (task ta : trigger.new){ id Opportunityid = ta.WhatId; Opportunity oOpportunity = [select id, Latest_Open_Due_Date__c from Opportunity where id = :Opportunityid LIMIT 1]; if (oOpportunity.Latest_Open_Due_Date__c < ta.ActivityDate ) { if (ta.Status != 'Completed' ) { System.debug ('in completed'); oOpportunity.Latest_Open_Due_Date__c = ta.ActivityDate; //update oOpportunity; } } if (oOpportunity.Latest_Open_Due_Date__c == null) { if (ta.Status != 'Completed' ) { oOpportunity.Latest_Open_Due_Date__c = ta.ActivityDate; //update oOpportunity; } } List <Task> ActiveTask = [select id, WhatId from Task where Whatid = :Opportunityid and status != 'Completed']; if (ActiveTask.isEmpty()){ oOpportunity.Latest_Open_Due_Date__c = null; ///update oOpportunity; } update oOpportunity; } } catch (Exception e ){ System.debug('Create trigger exception ' + e.getMessage()); } } THIS IS AMONG THE THINGS I HAVE BEEN TRYING: trigger UpdateLatestDueDatefromTask on Task (after insert, after update) { //the trigger will update the opportunity's latest_open_due_date__c field for (task ta : trigger.new){ try{ List<Opportunity> updateOpportunities = new list<Opportunity>(); List <Tasks> OpportunitiesWithTasks = [select id, ActivityDate, WhatId, (Select id, Latest_Open_due_Date__c from Opportunity) from Task Where Id in :Trigger.newMap.keySet()]; for (Opportunity oOpportunity: [select id, WhatId, ActivityDate from Opportunity where id in :Trigger.NewMap.keySet()] ) { list <Opportunity> parentOpp = [select id, Latest_Open_Due_Date__c From Opportunity where id in :Trigger.NewMap.get (oTask.WhatId)]; if (parentOpp.Latest_Open_Due_Date__c < oTask.WhatId) { parentOpp.Latest_Open_Due_Date__c = oTask.ActivityDate; } } } (The error I get is that it doesn't understand the relationship for opportunity.) THANKS!
All Answers
You have to remove the SOQL from the loop.
Please go through the below mentioned url for your reference:
http://blog.jeffdouglas.com/2009/04/20/writing-bulk-triggers-for-salesforce/
You can refer to Salesforce Best practices to bulkify trigger:
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
Thanks,
Prtaik
Hi LBarton,
+1
The code you posted above, (the code within For loop)
for (task ta : trigger.new){
id Opportunityid = ta.WhatId;
Opportunity oOpportunity = [select id, Latest_Open_Due_Date__c from Opportunity where id = :Opportunityid LIMIT 1];
if (oOpportunity.Latest_Open_Due_Date__c < ta.ActivityDate ) {
if (ta.Status != 'Completed' ) {
System.debug ('in completed');
oOpportunity.Latest_Open_Due_Date__c = ta.ActivityDate;
//update oOpportunity;
}
}
if (oOpportunity.Latest_Open_Due_Date__c == null) {
if (ta.Status != 'Completed' ) {
oOpportunity.Latest_Open_Due_Date__c = ta.ActivityDate;
//update oOpportunity;
}
}
List <Task> ActiveTask = [select id, WhatId from Task where Whatid = :Opportunityid and status != 'Completed'];
if (ActiveTask.isEmpty()){
oOpportunity.Latest_Open_Due_Date__c = null;
///update oOpportunity;
}
update oOpportunity;
}
In this code you should put SOQL outside the For loop which will help to remove the error of SOQL limit.
Thanks,
Pratik
I use what he had posted above and changed it a little and this is now in production as follows:
As it's clearly seen above that it was me who provided the code which is in your production is provided by me not Pratik and you give all credit to Pratik.
I think this is not not fare.