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
JonathanMClarkeJonathanMClarke 

Need Help Bulkifying my Trigger

Currently have the following trigger which works perfectly for one record inside our production organization:

 

trigger createCommissionClosedWon on Opportunity ( before insert, before update) {
    Opportunity[] opps = Trigger.New;
    for (Opportunity o:opps){
    If (o.StageName == 'Closed Won')
       CreateCommissionRecords.createCommissionRecords(opps);
}}

 I'm trying to bulkify this trigger so that it performs well under bulk updates..... The following code creates an error when I try and save:

 

trigger createCommissionClosedWon on Opportunity (before insert, before update) {

    for (Opportunity opps: Trigger.New){
    	If (opps.Stage == 'Closed Won'){
   		CreateCommissionRecords.createCommissionRecords(opps);
}}}

 What am I doing Wrong??? This is the error message that I'm receiveing: Save error: Method does not exist or incorrect signature: CreateCommissionRecords.createCommissionRecords(SOBJECT:Opportunity)  

 

it sounds like a variable / definitional problem.... Thanks so much for helping me.....

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

Hi,

 

Can you please post the CreateCommissionRecords class here?

 

I am not sure what you're trying to do in the CreateCommissionRecords class, but if you want to bulkify, then create a list of type Opportunity, after doing all the changes to the records ,add that record to the list and then update the list outside the forloop. SOmthing like this if I am doing all my processing in my trigger itself:

trigger createCommissionClosedWon on Opportunity (before insert, before update) {
//create a new list to store the records.
list<opportunity> updateOpps = new list<opportunity>();

    for (Opportunity opps: Trigger.New){
    	If (opps.Stage == 'Closed Won'){

		//do all the processing and then add the oppotunity record to the list.
		updateOpps.add(opps);
	}
    }

    //update all the records at once outside the forloop.
    update updateOpps;
}

 Hope this helps!

 

 -sam

All Answers

jungleeejungleee

Hi,

 

Can you please post the CreateCommissionRecords class here?

 

I am not sure what you're trying to do in the CreateCommissionRecords class, but if you want to bulkify, then create a list of type Opportunity, after doing all the changes to the records ,add that record to the list and then update the list outside the forloop. SOmthing like this if I am doing all my processing in my trigger itself:

trigger createCommissionClosedWon on Opportunity (before insert, before update) {
//create a new list to store the records.
list<opportunity> updateOpps = new list<opportunity>();

    for (Opportunity opps: Trigger.New){
    	If (opps.Stage == 'Closed Won'){

		//do all the processing and then add the oppotunity record to the list.
		updateOpps.add(opps);
	}
    }

    //update all the records at once outside the forloop.
    update updateOpps;
}

 Hope this helps!

 

 -sam

This was selected as the best answer
JonathanMClarkeJonathanMClarke
public with sharing class CreateCommissionRecords {

		public static void createCommissionRecords(Opportunity[] opps){
            
      		  String type_annuity = ''; 
      		  String type_ind_savings = '';
      	 	  String type_grp_insurance = ''; 
      	 	  String type_ind_insurance = '';
      		  String type_OHSA = '';
      		  String type_ind_disability = '';
      		  String type_ind_illness = '';
      		  String type_grp_retirement = '';
  		   	  Integer yearspayable;

 RecordType[] rt = [Select Name, Id From RecordType  where SobjectType='Opportunity'];

for(RecordType r :rt){
	
if(r.Name=='Individual Fixed Annuity'){
       type_annuity = r.Id; 
} else if(r.Name=='Individual Retirement Savings and Investments'){ 
       type_ind_savings = r.Id;    
} else if(r.Name=='Corporate Group Insurance'){
       type_grp_insurance = r.Id; 
} else if(r.Name=='Individual Life Insurance')  {
       type_ind_insurance = r.Id;   
} else if(r.Name=='Individual Disability Insurance'){
       type_ind_disability = r.Id;
} else if (r.Name== 'Individual Critical Illness'){
       type_ind_illness = r.Id;      
} else if (r.Name=='Olympia Health Spending Account'){
       type_OHSA = r.Id;
	} else {
       type_grp_retirement = r.Id;
}}	
        	
 List<Commission__c> commissions = New List<Commission__c>{};
        	
 for(Opportunity o:opps)
     {   
     	String oppid = o.id;
     	String ownerid = o.OwnerId;
     	String oppname = o.Name;
     	String recordtypeID = o.RecordTypeId;
     	if(recordtypeID == type_annuity || recordtypeID == type_ind_illness || recordtypeID == type_OHSA || recordtypeID == type_ind_disability || recordtypeId == type_ind_insurance)	
             yearspayable = 1;
     	if(recordtypeID == type_grp_retirement || recordtypeId == type_grp_insurance)	
             yearspayable = 2;
     	if(recordtypeID == type_ind_savings)	
             yearspayable = 3;  
                 for(Integer i = yearspayable; i > 0; i--){
                         Commission__c c = new Commission__c(Name = 'COMM Payable for ' + oppname + ' - ' + i, Close_Date__c = o.CloseDate +((yearspayable - i)*365), Opportunity__c = oppid, BDT1__c = ownerid, Prizm_FYC_est__c = o.Prizm_FYC__c, Prizm_FYC_act__c = o.Prizm_FYC__c );
                         commissions.add(c);
                         }
     				}
       insert commissions;   	
			} 
		}

 Again just recently morphed this code to take the SOQL insert function out of the FOR LOOP, but any suggestions would be very much appreciated. Newby to Java and Apex... Thank you so much junglee for your help!!

HaroldCHaroldC

I'm not entirely sure, but it looks like your createCommissionRecords is expecting an array of opp's and you are trying to pass in a single object instead of an array.

JonathanMClarkeJonathanMClarke

Thank you, did not realize you could make changes directly within a trigger... haha silly me.

This all works now........