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

Help with trigger
I am getting error message on hitting the governor limits due to the trigger below. What this trigger does is that whenever a task's description it copies over the data from the task object on to the custom object. Can someone please help so that my code does not hit the governor limits.
Many thanks in advance.
trigger CommentLog on Task (before update) { for(Task t: trigger.old) { CommentLog__c c = new CommentLog__c(); c.Previous_Comment__c=t.Description; c.CommentDate__c=t.LastModifiedDate; task t1= [select owner.name,LastModifiedBy.name from task where id=:t.id]; c.Name=t1.LastModifiedBy.name; c.Task_owner__c=t1.owner.name; c.type__c='Task'; c.Link_to_Task__c='https://na1.salesforce.com/'+t.id; insert c; } }
Try moving the insert into a list and remove the task loop and put it in a map - something like this:e.g.
You should never do a select/DML statement inside the loop...you will hit SF limitation easily.
Hi,
Whenever you are writing a trigger you need make sure that it can handle Bulk Data. You are getting an exception because you have written the Query and the DML operation ( insert ) in FOR loop. Take the following approach when writing a trigger :
First collect the data from TRIGGER.NEW/TRIGGER.OLD in a LIST. Then, iterate through that list in a FOR loop to perform the LOGIC / Assignment. Each time we run through the loop we are creating a new object and assigning our changes to that object and then adding it to a LIST. Once we have iterated though all the records then we do a DML (which is out the FOR Loop).
Please see the code below which will handle the bulk condition:
for(Account acct : Trigger.new){
if((acct.OS_Address_1__c==null && acct.OS_City__c==null &&
acct.OS_State__c==null && acct.D_B_Country__c==null &&
acct.D_B_Postal_Code__c==null) &&
( acct.BillingStreet !=null || acct.BillingCity !=null ||
acct.BillingCountry !=null || acct.BillingPostalCode !=null ||
acct.OS_Address_1__c = acct.BillingStreet;
acct.OS_City__c = acct.BillingCity;
acct.OS_State__c = acct.BillingState;
acct.D_B_Country__c = acct.BillingCountry;
acct.OS_Postal_Code__c = acct.BillingPostalCode;
}
}
}