I have a trigger with a trigger handler class on Opportunity. I am writing a batch class to update the stage in Opportunity which should call the trigger to create a record in "Order" object. In the Opportunity trigger, I am looking for recursion and only allowing the trigger to fire once. The issue I am having here is that since the trigger is getting fired multiple times, the handler class is not getting called. If I remove the recursion check, two records get created in "Order" object. I tried the same insert through update of opportunity from Anonymous window and I am having the same issue.
Below is my trigger
trigger OpportunityTrigger on Opportunity (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
system.debug('recursive');
system.debug('checkRecursive');
if(checkRecursive.runOnce()){
system.debug('Not recursive');
OpportunityHandler handler1 = new OpportunityHandler();
if(Trigger.isUpdate && Trigger.isBefore){
system.debug('Entered');
handler1.OnBeforeUpdate(Trigger.old, Trigger.new, Trigger.oldMap, Trigger.newMap);
}
}
}
Here is the handler class:
public class OpportunityHandler {
public void OnBeforeUpdate(Opportunity[] oldOpps, Opportunity[] updatedOpps, Map<ID, Opportunity> oldOppMap, Map<ID, Opportunity> updatedOppMap){
createOrders1(updatedOpps,oldOppMap,updatedOppMap);
}
private void createOrders1(Opportunity[] updatedOpps, Map<ID, Opportunity> oldOppMap, Map<ID, Opportunity> updatedOppMap){
//perform logic
}
Here is the code that I am trying in console and in batch
Opportunity o = new Opportunity();
o.Name = 'Test';
o.Entity__c = 'Invoice';
o.Type__c = 'Invoice';
o.AccountId = '001Z000001gS4Xg';
o.Billing_Contact__c = '003Z000000ZC2A8';
o.CloseDate = Date.newInstance(2030, 06, 01);
o.Category__c = 'Sales';
o.StageName = 'Proposal';
insert o;
List<OpportunityLineItem> oli = new List<OpportunityLineItem>();
OpportunityLineItem olinew=New OpportunityLineItem();
olinew.OpportunityId=o.Id;
olinew.Quantity = 1;
olinew.TotalPrice = 0;
olinew.PricebookEntryId = '01uZ00000090HF9';
olinew.Date__c = Date.newInstance(2030, 06, 01);
olinew.Month__c = 'June';
oli.add(olinew);
OpportunityLineItem olinew1=New OpportunityLineItem();
olinew1.OpportunityId=o.Id;
olinew1.Quantity = 1;
olinew1.TotalPrice = 5850;
olinew1.PricebookEntryId = '01uZ0000008vEjyIAE';
olinew1.Date__c = Date.newInstance(2030, 06, 01);
olinew1.Month__c = 'June';
oli.add(olinew1);
OpportunityLineItem olinew2=New OpportunityLineItem();
olinew2.OpportunityId=o.Id;
olinew2.Quantity = 1;
olinew2.TotalPrice = 2000;
olinew2.PricebookEntryId = '01uZ0000008vEkIIAU';
olinew2.Date__c = Date.newInstance(2030, 06, 01);
olinew2.Month__c = 'June';
oli.add(olinew2);
insert oli;
Opportunitycontactrole ocr = new OpportunityContactrole(ContactId = o.Billing_Contact__c, OpportunityId=o.Id, IsPrimary=true, Role='Business User');
insert ocr;
o.StageName = 'Closed/Signed';
update o;