+ Start a Discussion
paddingtonpaddington 

Bulk test issues

Hi everyone,

 

Another schoolboy error to reveal to the forum I'm afraid - thanks in advance for any help.

 

I have the following method, which I thought I'd coded to deal with bulk updates:

 

public static void createIEFfromAccount(List<Account> accs){ //Declare owner ID variable for IEF Assessments queue Id ownerId = [SELECT q.Queue.Id FROM QueueSobject q WHERE q.Queue.Name = 'IEF Assessments' AND q.SobjectType = 'Assessment__c'].Queue.Id; //Create a list of assessments to be inserted List<Assessment__c> assessments = new List<Assessment__c> (); //Call setOfIdFromListOfSObject Utility method to populate a map of account ID to client form ID Map<Id,Id> IEFmap = new Map<Id,Id>(); for (Client_Form__c form : [SELECT ID, Account__c FROM Client_Form__c WHERE RecordTypeId = '01280000000BQlBAAW' AND Account__c in :Utility.setOfIdFromListOfSObject(accs)]) { IEFmap.put(form.Account__c, form.Id); } //For each account, create two IEF assessments for(Account a:accs){ Assessment__c assess = new Assessment__c(); assess.Client_Form__c = IEFmap.get(a.Id); assess.RecordTypeId = '01280000000BR0VAAW'; assess.OwnerId = ownerId; assess.Account__c = a.Id; assessments.add(assess); assessments.add(assess.clone()); } //Insert the assessments insert assessments; }

 

 However, when I run the following test method, I get 804 DML rows. I'm probably being really stupid, but I can't see where I have DML statements in loops. Any ideas?

 

 

static testMethod void TESTcreateIEFfromAccount(){ //Test the creation of two IEF assessments from an account for which //Initial Enquiry Sub-stage has been changed to 'Awaiting two assessments //SINGLE CASE //Create a new account System.debug('SINGLE CASE - creating test account'); Account z = new Account(Name = 'Acme', Initial_Enquiry_Sub_stage__c = null); insert z; //Update the Initial Enquiry Sub-stage System.debug('SINGLE CASE - update Initial Enquiry Sub-stage and fire IEFsubStage trigger'); Account updatez = [SELECT Id, Initial_Enquiry_Sub_stage__c FROM Account WHERE Id = :z.Id]; updatez.Initial_Enquiry_Sub_stage__c = 'Awaiting two assessments'; update updatez; //Updating should fire the IEFsubStage trigger and create two assessments of IEF record type Account testz = [SELECT Id, Initial_Enquiry_Sub_stage__c FROM Account WHERE Id = :z.Id]; Integer num = 0; Integer recType = 0; List<Assessment__c> testAssess = new List<Assessment__c> ([SELECT Id, RecordTypeId FROM Assessment__c WHERE Account__c = :z.Id]); for(Assessment__c assess :testAssess){ num++; if(assess.RecordTypeId == '01280000000BR0VAAW'){ recType++; } } System.assertEquals(2,num,'Incorrect number of assessments have been created'); System.debug(+num+' assessments have been created.'); System.assertEquals(2,recType,'Incorrect number of assessments with IEF record Type'); System.debug(+recType+' assessments of IEF record type have been created.'); Integer DMLlimit = Limits.getDMLStatements(); System.debug('Number of DML Statements is ' +DMLlimit); //BULK CASE //Create 200 accounts System.debug('BULK CASE - inserting 200 accounts'); List<Account> insertAccs = new List<Account>(); for(integer i=0; i<200; i++){ insertAccs.add(new Account(Name = 'Test Account '+i, Initial_Enquiry_Sub_stage__c = null)); } insert insertAccs; //Update the 200 accounts List<Account> updateAccs = new List<Account> ([SELECT Id, Name, Initial_Enquiry_Sub_stage__c FROM Account WHERE Id IN :Utility.setOfIdFromListOfSObject(insertAccs)]); for(Account a: updateAccs){ a.Initial_Enquiry_Sub_stage__c = 'Awaiting two assessments'; } update updateAccs; //IEF subStage Trigger should fire for all 200 records, creating 400 assessments of record type IEF Integer bulkNum = 0; Integer bulkRecType = 0; List<Assessment__c> bulkTestAssess = new List<Assessment__c> ([SELECT Id, RecordTypeId FROM Assessment__c WHERE Account__c IN :Utility.setOfIdFromListOfSObject(updateAccs)]); for(Assessment__c assess :bulkTestAssess){ bulkNum++; if(assess.RecordTypeId == '01280000000BR0VAAW'){ bulkRecType++; } } System.assertEquals(400,bulkNum,'Incorrect number of assessments have been created'); System.debug(+bulkNum+' assessments have been created.'); System.assertEquals(400,bulkRecType,'Incorrect number of assessments with IEF record Type'); System.debug(+bulkRecType+' assessments of IEF record type have been created.'); }

 

 Thanks again...

 

 

Cool_DevloperCool_Devloper

Hi Pads,

 

Would be great if you can post your code in a more readable format :)

 

Not able to make much out of it!

 

Cool_D

paddingtonpaddington
Thanks for taking a look Cool_D, but this one's going to have to wait for a while. I'll edit the post once I've dealt with my multi-select picklist problem.