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
XeeshanXeeshan 

exceeding DML Statements error during Test Coverage

I am writing test coverage code for my organization and i am receiving error of exceeding DML Statements in my Test Class.I have already done test coverage for previous classes successfully but when i want to do test coverage for newly created class.It gives me DML exceeding error.

Is there any solution for this problem as i need to use DML statements in my Test Class to cover more code.

 

Thanks

mikecaimikecai
Do you have a Select statement inside of a For loop?  That will often exceed the allowable limit.
lvivaninlvivanin
May be this link will be helpful : APEX CODE BEST PRACTICES
XeeshanXeeshan
No, there is not any For loop in my test class for Code Coverage.Is there any way so that i can cover more code in test class for newly created class without DML Exceeding error.I have to deploy my new class but code coverage is pending due to this problem.
XeeshanXeeshan

public class CompileAndTest { static testMethod void testInsertOpportunity() { Opportunity oppFrance=new Opportunity(name='testFrance',Region__c = 'France',Business_Unit__c = 'Media',StageName='Contract Pending',CloseDate=System.today()); Opportunity opp=new Opportunity(name='test',Region__c = 'U.K.',Invoice_To__c='Agency',StageName='Contract Pending',CloseDate=System.today()); Opportunity oppDeleteContract=new Opportunity(name='test2',Region__c = 'U.S.',StageName='Contract Pending',CloseDate=System.today()); try{ insert oppFrance; oppFrance.Invoice_To__c='Agency'; update oppFrance; oppFrance.Region__c = 'Germany'; update oppFrance; insert opp; opp.Business_Unit__c='Media'; opp.Public_Service__c=True; update opp; opp.No_Agency_Discount__c=False; update opp; insert oppDeleteContract; oppDeleteContract.StageName ='Close Won'; update oppDeleteContract; oppDeleteContract.StageName='Close Lost'; update oppDeleteContract; oppDeleteContract.StageName='Contract Pending'; update oppDeleteContract; GenerateSequenceNo.GenerateCampaignId(opp.Id); opp.name='test'; opp.SeqNo__c =null; opp.Region__c = 'U.K.'; opp.Invoice_To__c='Agency'; opp.Business_Unit__c= 'Media'; opp.Total_Discount__c=0; update opp; GenerateSequenceNo.GenerateCampaignId(opp.Id); opp.SeqNo__c =9; update opp; GenerateSequenceNo.GenerateCampaignId(opp.Id); opp.SeqNo__c =99; update opp; GenerateSequenceNo.GenerateCampaignId(opp.Id); opp.SeqNo__c =999; update opp; GenerateSequenceNo.GenerateCampaignId(opp.Id); opp.Invoice_To__c='Advertiser'; opp.StageName ='Close Won'; update opp; opp.StageName='Close Lost'; update opp; opp.StageName='Contract Pending'; update opp; List <CampaignScheduler__c> SchedDates = new CampaignScheduler__c[]{}; Date CSDate = date.newInstance(2008, 1, 1); Date CEDate = date.newInstance(2008, 1, 10);//valueOf('2008-08-01'); Media_Campaign__c MC = new Media_Campaign__c(name='test',Campaign_ID__c='Test-001',Opportunity__c=opp.Id,Budget__c=2000,Total_Amount__c=1000,Start_Date__c=CSDate,End_Date__c=CEDate); insert MC; Media_Campaign__c MC2 = new Media_Campaign__c(name='test1',Campaign_ID__c='Test-002',Opportunity__c=opp.Id,Budget__c=2000,Total_Amount__c=1000,Start_Date__c=CSDate,End_Date__c=CEDate); System.debug('MC'+MC); CampaignScheduler__c CampaignStatus = new CampaignScheduler__c(); CampaignStatus.Date__c = date.newInstance(2008, 1, 2); CampaignStatus.Status__c = 'BlackOut'; CampaignStatus.Campaign__c = MC.Id; CampaignStatus.Opportunity__c = opp.Id; Integer campSchSize2 = SchedDates.size(); String emptySchDatC='not empty'; if (campSchSize2 == 0) emptySchDatC = 'empty'; String Currency1= 'USD'; //Test generateDailySchedulesOnCreate CampaignSchedule.generateDailySchedulesOnCreate(MC.Start_Date__c,MC.End_Date__c,MC.Budget__c,opp.Id,MC.Id,emptySchDatC,Currency1,SchedDates); SchedDates.add(CampaignStatus); System.debug('Campaign Id' + MC.Id); List <CampaignScheduler__c> SchedDates1 = new CampaignScheduler__c[]{}; SchedDates1.add(CampaignStatus); CampaignScheduler__c CampaignStatus1 = new CampaignScheduler__c(); CampaignStatus1.Date__c = date.newInstance(2008, 1, 5); CampaignStatus1.Status__c = 'Cancel'; CampaignStatus1.Campaign__c = MC.Id; CampaignStatus1.Opportunity__c = opp.Id; SchedDates1.add(CampaignStatus1); CampaignSchedule.generateDailySchedulesOnCreate(MC.Start_Date__c,MC.End_Date__c,MC.Budget__c,opp.Id,MC.Id,emptySchDatC,Currency1,SchedDates1); //End Test generateDailySchedulesOnCreate Date BEffDate = date.newInstance(2008, 1, 5); Date EffDate = date.newInstance(2008, 1, 5); CampaignSchedule.generateDailySchedulesonEdit(MC.Start_Date__c,MC.End_Date__c,MC.Budget__c,opp.Id,MC.Id,Currency1,SchedDates,2); //Start Test of generateDailySchedulesonEditforMOD Integer campSchSize = SchedDates.size(); String emptySchDat='not empty'; if (campSchSize == 0) emptySchDat = 'empty'; CampaignSchedule.generateDailySchedulesonEditforMOD(MC.Start_Date__c,MC.End_Date__c,MC.Budget__c,opp.Id,MC.Id,BEffDate,EffDate,emptySchDat,Currency1,SchedDates,SchedDates,2); CampaignSchedule.generateSchedulesAllCampaigns(opp.Id,MC.Id,MC.Start_Date__c,MC.End_Date__c,MC.Budget__c,Currency1); List <CampaignScheduler__c> SchedDatesMOD = new CampaignScheduler__c[]{}; CampaignScheduler__c CampaignStatus2 = new CampaignScheduler__c(); CampaignStatus2.Date__c = date.newInstance(2008, 1, 4); CampaignStatus2.Status__c = 'Cancel'; CampaignStatus2.Campaign__c = MC.Id; CampaignStatus2.Opportunity__c = opp.Id; SchedDatesMOD.add(CampaignStatus2); CampaignScheduler__c CampaignStatus3 = new CampaignScheduler__c(); CampaignStatus3.Date__c = date.newInstance(2008, 1, 5); CampaignStatus3.Status__c = 'Cancel'; CampaignStatus3.Campaign__c = MC.Id; CampaignStatus3.Opportunity__c = opp.Id; SchedDatesMOD.add(CampaignStatus3); CampaignScheduler__c CampaignStatus4 = new CampaignScheduler__c(); CampaignStatus4.Date__c = date.newInstance(2008, 1, 2); CampaignStatus4.Status__c = 'BlackOut'; CampaignStatus4.Campaign__c = MC.Id; CampaignStatus4.Opportunity__c = opp.Id; SchedDatesMOD.add(CampaignStatus4); Integer campSchSize1 = SchedDatesMOD.size(); if (campSchSize1 == 0) emptySchDat = 'empty'; CampaignSchedule.generateDailySchedulesonEditforMOD(MC.Start_Date__c,MC.End_Date__c,1000,opp.Id,MC.Id,BEffDate,EffDate,emptySchDat,Currency1,SchedDatesMOD,SchedDatesMOD,2); CampaignSchedule.generateDailySchedulesonEditforMOD(MC.Start_Date__c,MC.End_Date__c,500,opp.Id,MC.Id,BEffDate,EffDate,emptySchDat,Currency1,SchedDatesMOD,SchedDatesMOD,2); CampaignScheduler__c CampaignStatus5 = new CampaignScheduler__c(); CampaignStatus5.Date__c = date.newInstance(2008, 1, 2); CampaignStatus5.Status__c = 'Suspended'; CampaignStatus5.Campaign__c = MC.Id; CampaignStatus5.Opportunity__c = opp.Id; SchedDatesMOD.add(CampaignStatus5); CampaignScheduler__c CampaignStatus6 = new CampaignScheduler__c(); CampaignStatus6.Date__c = date.newInstance(2008, 1, 9); CampaignStatus6.Status__c = 'Cancel'; CampaignStatus6.Campaign__c = MC.Id; CampaignStatus6.Opportunity__c = opp.Id; SchedDatesMOD.add(CampaignStatus6); CampaignScheduler__c CampaignStatus7 = new CampaignScheduler__c(); CampaignStatus7.Date__c = date.newInstance(2008, 1, 10); CampaignStatus7.Status__c = 'Cancel'; CampaignStatus7.Campaign__c = MC.Id; CampaignStatus7.Opportunity__c = opp.Id; SchedDatesMOD.add(CampaignStatus7); CampaignSchedule.generateDailySchedulesonEditforMOD(MC.Start_Date__c,MC.End_Date__c,10000,opp.Id,MC.Id,BEffDate,EffDate,emptySchDat,Currency1,SchedDatesMOD,SchedDatesMOD,2); CampaignScheduler__c CampaignStatus8 = new CampaignScheduler__c(); CampaignStatus8.Date__c = date.newInstance(2008, 1, 10); CampaignStatus8.Status__c = 'Active'; CampaignStatus8.Campaign__c = MC.Id; CampaignStatus8.Opportunity__c = opp.Id; SchedDatesMOD.add(CampaignStatus8); CampaignSchedule.generateDailySchedulesonEditforMOD(MC.Start_Date__c,MC.End_Date__c,10000,opp.Id,MC.Id,BEffDate,EffDate,emptySchDat,Currency1,SchedDatesMOD,SchedDatesMOD,2); CampaignScheduler__c CampaignStatus9 = new CampaignScheduler__c(); CampaignStatus9.Date__c = date.newInstance(2008, 1, 8); CampaignStatus9.Status__c = 'Cancel'; CampaignStatus9.Campaign__c = MC.Id; CampaignStatus9.Opportunity__c = opp.Id; SchedDatesMOD.add(CampaignStatus9); CampaignSchedule.generateDailySchedulesonEditforMOD(MC.Start_Date__c,MC.End_Date__c,10000,opp.Id,MC.Id,BEffDate,EffDate,emptySchDat,Currency1,SchedDatesMOD,SchedDatesMOD,2); CampaignScheduler__c CampaignStatus10 = new CampaignScheduler__c(); CampaignStatus10.Date__c = date.newInstance(2008, 1, 8); CampaignStatus10.Status__c = 'Suspended'; CampaignStatus10.Campaign__c = MC.Id; CampaignStatus10.Opportunity__c = opp.Id; SchedDatesMOD.add(CampaignStatus10); String CampaignIds= '\''+MC.Id +'\',\''+MC2.Id +'\''; List<String> ListCampaigns = new List<String>(); ListCampaigns.add(MC.Id+':'+MC.Id); ListCampaigns.add(MC2.Id+':'+MC2.Id); OpportunityCloning.DailySchedules(opp.Id,opp.Id,MC.Id,MC.Id,ListCampaigns); OpportunityCloning.DailySchedulesDates(opp.Id,opp.Id,MC.Id,MC.Id,ListCampaigns); OpportunityCloning.MonthlySchedule(opp.Id,opp.Id,MC.Id,MC.Id,ListCampaigns); Account Acc = new Account(name='hello',Great_Plains_ID__c='222'); insert Acc; Company__c com = new Company__c(name='adv'); Acc.Region__c = 'U.S'; Acc.Great_Plains_ID__c = '333'; Acc.Advertiser__c = com.Id; Acc.Agency__c = com.Id; Acc.Broker__c = com.Id; update Acc; Company__c com2 = new Company__c(name='adv2'); Acc.Region__c = 'U.S'; Acc.name ='Hello'; Acc.Great_Plains_ID__c = '4444'; Acc.Advertiser__c = com2.Id; Acc.Agency__c = com2.Id; update Acc; Company__c com3 = new Company__c(name='adv2'); Acc.Region__c = 'U.S'; Acc.name ='Hello'; Acc.Great_Plains_ID__c = '44'; Acc.Advertiser__c = com2.Id; Acc.Agency__c = com3.Id; Company__c com4 = new Company__c(name='com4'); Acc.Region__c = 'U.S'; Acc.name ='Hel'; Acc.Great_Plains_ID__c = '44'; Acc.Agency__c = com3.Id; Acc.Advertiser__c = com2.Id; Acc.Broker__c = com4.Id; Contract objContract = new Contract(name='test',Region__c = 'U.S.',AccountId=Acc.Id); insert objContract; Contract_Campaign__c objContractCampaign = new Contract_Campaign__c (name='Test2',Campaign_ID__c='Test-001',Contract__c=objContract.Id,Budget__c=1000,End_Date__c=CEDate,Start_Date__c=CSDate); insert objContractCampaign; //insert objContractCampaign1; System.debug('Start check'); System.debug('Opportunity'+ opp); System.debug('Contract'+ objContract); System.debug('Contract Campaign'+ objContractCampaign); System.debug('Media Campaign'+ MC); System.debug('End check'); List<String> ListofMediaCampaigns = new List<String>(); ListofMediaCampaigns.add(MC.Id+':'+objContractCampaign.Id); OpportunityConversion.DailySchedules(opp.Id,objContract.Id,MC.Id,objContractCampaign.Id,ListofMediaCampaigns); OpportunityConversion.updateDailySchedules(opp.Id,objContract.Id,MC.Id,objContractCampaign.Id,ListofMediaCampaigns); UndoMODProcess.DailyOppSchedules(opp.Id,objContract.Id,MC2.Id,objContractCampaign.Id,ListofMediaCampaigns); Lead myLead = new Lead(lastname = 'Fry', company='Fry And Sons',Business_Unit__c='Media',Region__c='Germany'); insert myLead; Database.LeadConvert lc = new database.LeadConvert(); lc.setLeadId(myLead.id); System.debug('Lead Convert' + lc); LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1]; lc.setConvertedStatus(convertStatus.MasterLabel); System.debug('Convert Status' + convertStatus); Database.LeadConvertResult lcr = Database.convertLead(lc); System.assert(lcr.isSuccess()); System.debug('Lead Convert Result' + lcr); //Contact con = new Contact(Name='ss',Contact_Role__c='Economic Decision Maker',Company__c=com4.Id); //insert con; //System.debug('Contact'+ con); Task tsk = new Task( WhoId = lcr.getContactId() , WhatId = lcr.getOpportunityId()); insert tsk; System.debug('Task'+ tsk); Opportunity opp123 = [select Id from Opportunity where Id =: lcr.getOpportunityId() limit 1]; System.debug('opp123----------'+ opp123); /////////////////UK RIghts to Buy Test Coverage starts here////////////// system.debug ('JUST BEFORE EXECUTION OF UKRB TEST COVERAGE'); system.debug ('CREATING CONTROLLER OBJU'); PublisherInventoryPurchaseController testObjCont = new PublisherInventoryPurchaseController (); system.debug ('CALLING TEST METHOD IN CONTROLLER OBJECT'); testObjCont.TestTheUKRBController(); system.debug ('CALLING SAVE INVENTORY METHOD'); testObjCont.saveInventory(); System.debug('END OF CONTROLLER TEST'); } catch (DmlException e) { System.debug(e.getMessage()); } } }

I am posting my Test Class Code here if nay one have a look into it and find the solution of this problem.

lvivaninlvivanin

you have more than 20 DMLs in your test coverage code.

prepare the objects(to be inserted/updated) with in FOR LOOP and do insert/updates outside of the FOR LOOP

Nick1746323Nick1746323

You can probably split all those tests into separate methods, instead of all in one method.

Note that the DML limit is for each test method, so this should help.