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
Kr ramKr ram 

limit or bulkify the apex trigger

Hello gurus,

the below apex trigger i guess can hit the governor limits and may be be optimized for bulk updates,
Any changes you would recommend? thank you !
---------------------------------------------------------------------------------------------
trigger abc on abc__c (after insert, after update) {
 
    for(abc__c project : Trigger.New)
    {
               List< Project_Task__c> tasklist = new List< Project_Task__c>    (
               [SELECT id, name, IsComplete__c  FROM Project_Task__c
               WHERE (name LIKE '%big deal Call%'     
                                             AND IsComplete__c = FALSE
                                             AND abc__c = :project.id]
               );
 
               for (Project_Task__c task : tasklist)
               {
                              task. IsComplete__c = true;
               }
               update tasklist;
      
   }
}
----------------------------------------------------------------------------------------------------
Anirudh SinghAnirudh Singh
Hi,

There are a few mistakes in the Trigger.

I have modified and commented to explain what is done.

Please find below the trigger:
trigger abc on abc__c (after insert, after update)
{
	//Take all the project Ids in this set.
	Set<Id> projectIds=new Set<Id>();
    for(abc__c project: Trigger.New)
    {
		projectIds.add(project.Id);
	}
    
	//Fetch all the Project_Task__c records for the projectIds and other criterias(where clause)
	List<Project_Task__c> tasklist=new List< Project_Task__c>(
               [SELECT id, name, IsComplete__c  FROM Project_Task__c
                WHERE (name LIKE '%big deal Call%'     
                AND IsComplete__c = FALSE
                AND abc__c IN :projectIds]
               );
	
	//Update the IsComplete__c field value in the tasks fetched in tasklist
    for(Project_Task__c task: tasklist)
    {
		task.IsComplete__c=true;
    }
	
	//Update tasks in tasklist.
    update tasklist;
}

Please let me know if this helps.
If yes, please mark the Question as Solved.


Thanks and Regards,
Anirudh Singh