+ Start a Discussion

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();
task t1= [select owner.name,LastModifiedBy.name from task where id=: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. 


trigger CommentLog on Task (before update) { List<CommentLog__c> newcomments = new List<CommentLog__c> ();
Map<Id, Task> mTask = new Map<Id, Task> ([select Id, owner.name,LastModifiedBy.name from Task where id=:trigger.old]);
for(Task t: trigger.old) { CommentLog__c c = new CommentLog__c(); c.Previous_Comment__c=t.Description; c.CommentDate__c=t.LastModifiedDate; c.Name= mTask.get(t.Id).LastModifiedBy.name; c.Task_owner__c = mTask.get(t.Id).owner.name; c.type__c='Task'; c.Link_to_Task__c='https://na1.salesforce.com/'+t.id; newcomments.add(c); } insert newcomments; }


Chris ZhuangChris Zhuang

You should never do a select/DML statement inside the loop...you will hit SF limitation easily.




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:



trigger CommentLog on Task (before update) 
	List<task> lst_task = [select owner.name , LastModifiedBy.name from task where id IN : trigger.old];
	List<CommentLog__c> lst_com_log = new List<CommentLog__c>();
	for(Integer i=0; i<lst_task.size(); i++)
		CommentLog__c obj_com_log = new CommentLog__c();
	insert lst_com_log;


Trigger  convert billing Country(text field)  to std Country name before copying customer Country using ISO Country mapping
Billing Country is text field and can have any value. We need to convert it to Standard Country name before copying it over to custom country field using the ISO – Country mapping.
          There is a custom object for this mapping called Country. Based on the billing country get the correct country value from Country Object   and update custom country. If billing country is blank look for ISO country code based on that query the custom object and update.  If any time a match is not found for the country from the ISO country code, don’t copy the country value. Leave it blank.
trigger BillingAddress  on Account (before insert, before update) {

        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 || 

BillingCountry !=null || acct.BillingPostalCode !=null ||
           acct.BillingState !=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;