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
srikanth11srikanth11 

bulkify trigger issue urgent

hi all i have written this trigger and it is working for one insert and update of a record at a time but when i am bulk inserting records 50 or 60 at a time its not working some one plz check my code and help me how to change my trigger to bulkify it. this is my trigger

trigger updatefundtotal on opportunity(after insert, after update)
 {
   Decimal id2num =0.0;

 for(opportunity o:trigger.new)
 {
 fund__c f=[select id ,Current_AUM__c from fund__c where id=:o.fund__c];
 opportunity[] opp=[select ID,Name,amount,Fund__r.Current_AUM__c from opportunity where fund__c=:f.ID  and stagename='booked'];
for(opportunity opp1:opp)
          {
         
         id2num  =opp1.amount + id2num;
          f.Current_AUM__c = id2num ;        
                  
        } update f;
          
          }   

 }

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

you can use your bulkfy triiger below here

 

trigger updatefundtotal on opportunity(after insert, after update)
{
	set<ID> fundid= new set<ID>();
	Decimal id2num =0.0;
	map<ID,fund__c> fundmap= new map<ID,fund__c>();
	list<fund__c> f2 = new list<fund__c>();
	for(opportunity o:trigger.new)
	{
		fundid.add(o.fund__c);
	}
	list<fund__c> f = [select id ,Current_AUM__c from fund__c where id IN :fundid];
	for(fund__c fff : f)
	{
		fundmap.put(fff.id,fff);
	}
	opportunity[] opp=[select ID,Name,amount,Fund__r.Current_AUM__c from opportunity where fund__c IN :fundid  and stagename='booked'];
	for(opportunity opp1:opp)
	{

		id2num  =opp1.amount + id2num;
		fundmap.get(opp1.fund__c).Current_AUM__c = id2num;      
		f2.add(fundmap.get(opp1.fund__c));              
	}
	update f2;
}

 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

you can use your bulkfy triiger below here

 

trigger updatefundtotal on opportunity(after insert, after update)
{
	set<ID> fundid= new set<ID>();
	Decimal id2num =0.0;
	map<ID,fund__c> fundmap= new map<ID,fund__c>();
	list<fund__c> f2 = new list<fund__c>();
	for(opportunity o:trigger.new)
	{
		fundid.add(o.fund__c);
	}
	list<fund__c> f = [select id ,Current_AUM__c from fund__c where id IN :fundid];
	for(fund__c fff : f)
	{
		fundmap.put(fff.id,fff);
	}
	opportunity[] opp=[select ID,Name,amount,Fund__r.Current_AUM__c from opportunity where fund__c IN :fundid  and stagename='booked'];
	for(opportunity opp1:opp)
	{

		id2num  =opp1.amount + id2num;
		fundmap.get(opp1.fund__c).Current_AUM__c = id2num;      
		f2.add(fundmap.get(opp1.fund__c));              
	}
	update f2;
}

 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
srikanth11srikanth11

thats a great help jain anf i have changed update f2 to f as its throwing an error but the functionality is working but i could not exactly understand whats happening and how exactly it got bulkified hope u give me some points on it so i can use it for my furthur triggers