You need to sign in to do that
Don't have an account?

Trigger - Too Many DML statements 151
I need a fresh set of eyes on this trigger. Can some one take am moment and perhaps point me in the right direction?
Trigger:
trigger Opportunity on Opportunity (before insert, before update) { System.debug('Alerting on Opportunity Trigger --------------------> Starting '); for(Opportunity o:trigger.new){ Boolean boolStage; Boolean booRec; Boolean boolClose; if(trigger.isUpdate){ Opportunity oldOpp = Trigger.oldMap.get(o.id); if(oldOpp.StageName != o.StageName){ boolStage = true; } if(oldOpp.CloseDate != o.CloseDate){ boolClose = true; } } // for(integer i=0;i<rec.size();i++){ if(boolStage == true ){ //StageName = 1-Qualification Complete/Research, 0-Prospecting if(o.StageName == '0-Prospecting' || o.StageName == '1-Qualification Complete/Research'){ o.Forecast_Identifier__c = 'Lead'; //StageName = 2-Assessment, 3-Justification, 4-Internal Estimate }else if(o.StageName == '2-Assessment' || o.stageName == '3-Justification' || o.StageName == '4-Internal Estimate'){ o.Forecast_Identifier__c = 'Open'; //StageName = 5-Final Proposal }else if(o.StageName == '5-Final Proposal'){ o.Forecast_Identifier__c = 'Upside'; //StageName = 6-Vendor Selection, 7-Contract }else if(o.StageName == '6-Vendor Selection' || o.StageName == '7-Contract'){ o.Forecast_Identifier__c = 'Committed'; //StageName = 8-Closed }else if(o.StageName == '8-Closed'){ o.Forecast_Identifier__c = 'Won'; } }else if(!trigger.isUpdate){ //StageName = 1-Qualification Complete/Research, 0-Prospecting if(o.StageName == '0-Prospecting' || o.StageName == '1-Qualification Complete/Research'){ o.Forecast_Identifier__c = 'Lead'; //StageName = 2-Assessment, 3-Justification, 4-Internal Estimate }else if(o.StageName == '2-Assessment' || o.stageName == '3-Justification' || o.StageName == '4-Internal Estimate'){ o.Forecast_Identifier__c = 'Open'; //StageName = 5-Final Proposal }else if(o.StageName == '5-Final Proposal'){ o.Forecast_Identifier__c = 'Upside'; //StageName = 6-Vendor Selection, 7-Contract }else if(o.StageName == '6-Vendor Selection' || o.StageName == '7-Contract'){ o.Forecast_Identifier__c = 'Committed'; //StageName = 8-Closed }else if(o.StageName == '8-Closed'){ o.Forecast_Identifier__c = 'Won'; } } if(boolClose == true){ o.Target_Complete_Date__c = o.closeDate+90; o.General_Availability_Date__c = o.closeDate+90; } // } } }
TestCase:
@isTest(seeAlldata=true) private class TestOpportunityTrigger { static testMethod void myUnitTest() { System.debug('Alerting on TestOpportunityTrigger --------------------> Starting'); List<RecordType> rec = [Select id, name from RecordType where sobjecttype = 'Opportunity' and name = 'Business Development']; Account acc = new Account(); acc.Name = 'Test'; acc.ShippingStreet = '123 Est Street'; acc.ShippingCity = 'Home'; acc.ShippingState = 'AL'; acc.ShippingPostalCode = '36105'; insert acc; System.debug('Alerting on --------------------> acc Details'+acc); List<String> stName = new List<String>{'0-Prospecting','1-Qualification Complete/Research','2-Assessment','3-Justifiction','4-Internal Estimate','5-Final Proposal','6-Vendor Selection','8-Closed'}; for(integer p=0;p<200;p++){ for(Integer i=0;i<stName.size();i++){ Opportunity opp = new Opportunity(); opp.RecordTypeId = rec[0].id; opp.StageName = stName[i]; opp.AccountId = acc.id; opp.Name = 'TestOpp'; opp.Type = 'Availity Connect'; opp.CloseDate = date.today(); opp.Opportunity_States__c = 'AL'; opp.Description = 'Testing'; System.debug('Alerting on --------------------> opp Details'+opp); try{ insert opp; }catch (Dmlexception e){ System.debug('Insert Failed '+e); } opp.StageName = '0-Prospecting'; opp.CloseDate = opp.CloseDate+4; try{ update opp; }catch (DmlException e) { System.debug('Update Failed '+e); } } } } }
Possibly rather than looping trigger.new (one dml per loop), try
List<Opportunity> optList = Trigger.new;
for ( Opportunity o : optList ) {