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
Lakshmi SLakshmi S 

How to Bulkify the Trigger ?

Hi Team,

How can we bulkify below trigger.
trigger CreateOpportunity on Task (after insert) {
    
    if(Trigger.isInsert && Trigger.isAfter){
        Set<Id> taskid = new Set<Id>();
        Map<Id, Task> taskmap = new Map<Id, Task>();
        
        List<Opportunity> oppList = new List<Opportunity>();
        Opportunity opp = Null;
        for(Task t : Trigger.New){
            if(t.Sales_Outreach__c == 'Converted to New Opportunity' && t.WhatId == Null){
                System.debug('----'+t.Subject+'---'+t.Id);
                taskid.add(t.Id);
                opp = new Opportunity();
                opp.Name = t.Subject;
                opp.Lead_Country__c = 'India';
                opp.Region__c = 'South';
                opp.CloseDate = Date.newInstance(2018, 10, 20);
                opp.ForecastCategoryName = 'Best Case';
                opp.StageName = 'Intro Meeting / Discovery';
                opp.Category__c = 'Cultivation Targets';
               //oppList.add(opp);
            } 
        }
        
        if(opp != Null){
            
            insert opp;
        }
        
        if(taskid.size() > 0){
            Task t = [Select id from Task where id in :taskid limit 1];
            t.WhatId = opp.Id;
            update t;
        }
    
        
       // t.WhatId = opp.Id;
       // update t;
        
    }

}
Please let me konw any one !!!

Thanks,
Regards.
 
Best Answer chosen by Lakshmi S
Steven NsubugaSteven Nsubuga
Try this
trigger CreateOpportunity on Task (after insert) {
    
    if(Trigger.isInsert && Trigger.isAfter){
        Set<Id> taskid = new Set<Id>();
        Map<Id, Task> taskmap = new Map<Id, Task>();
        
        List<Opportunity> oppList = new List<Opportunity>();
        Opportunity opp = Null;
        for(Task t : Trigger.New){
            if(t.Sales_Outreach__c == 'Converted to New Opportunity' && t.WhatId == Null){
                System.debug('----'+t.Subject+'---'+t.Id);
                taskid.add(t.Id);
                opp = new Opportunity();
                opp.Name = t.Subject;
                opp.Lead_Country__c = 'India';
                opp.Region__c = 'South';
                opp.CloseDate = Date.newInstance(2018, 10, 20);
                opp.ForecastCategoryName = 'Best Case';
                opp.StageName = 'Intro Meeting / Discovery';
                opp.Category__c = 'Cultivation Targets';
                oppList.add(opp);
            } 
        }
        
        if(oppList.size() > 0){
            
            insert oppList;
        
        
			if(taskid.size() > 0){
				List<Task> tasks = [Select id, WhatId from Task where id in :taskid];
				for(Integer i = 0; i < tasks.size(); i++){
					tasks[i].WhatId = oppList[i].Id;
				}
				update tasks;
			}
		}
    }

}