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
pooja chauchanpooja chauchan 

How to Bulkify a SpecificTrigger?

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;
    }
Best Answer chosen by pooja chauchan
Gaurav NirwalGaurav Nirwal
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;
    }

All Answers

Gaurav NirwalGaurav Nirwal
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;
    }

This was selected as the best answer
pooja chauchanpooja chauchan
thanks for quick reply