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
nmnbawanmnbawa 

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;

}

}

 

BritishBoyinDCBritishBoyinDC

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.

RamitRamit

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:

 

 

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();
		
		obj_com_log.Previous_Comment__c=lst_task[i].Description;
		obj_com_log.CommentDate__c=lst_task[i].LastModifiedDate;
		obj_com_log.Name=lst_task[i].LastModifiedBy.name;
		obj_com_log.Task_owner__c=lst_task[i].owner.name;
		obj_com_log.type__c='Task';
		obj_com_log.Link_to_Task__c='https://na1.salesforce.com/'+lst_task[i].id;
		
		lst_com_log.add(obj_com_log);
	}
	
	insert lst_com_log;
}

 

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

           acct.
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;      

     

        }    

     }    

 }