+ 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