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
bouscalbouscal 

Why won't this create child cases for testing?

Not sure what's going on, the parent cases get insert but the child cases do not.
 
List <Case> lCase = new List<Case>();
        List <Case> nCase = new List<Case>();
        for(integer i=0; i<20; i++){
        	Case cas1 = new Case(
                recordtypeid=rsc,
                accountid=acc1.id,
                contactid=con1.id,
                Case_Name__c='John Doe',
                case_phone_number__c='(800) 555-1212', 
                subject='Test Recycling Case',
                origin = 'Phone',
                status='Escalated to Management',
                Escalated_To__c='Return to Liaison',
                description='This is a parent Case');
           if(math.mod(i,2)==0){
                cas1.Case_Phone_Number__c='(800) 555-1212';
            }
			lCase.add(cas1);
        
            Case cas2 = new Case(
                recordtypeid=pdc,
                parentid=cas1.id,
                accountid=acc1.id,
                contactid=con1.id,
                status='Closed',
                Case_Name__c='John Doe',
                case_phone_number__c='(800) 555-1212', 
                subject='Escalation: Test Recycling Case',
                description='This is an escalated Case');
                nCase.add(cas2);
        }

I've also tried adding the children cases to the same list as the parents and inserting them all at once, which works, but doesn't hold the link to the parent.  They're all standalone cases.
 
Best Answer chosen by bouscal
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

First you have to insert the parent because you don't have its Ids. After you perform the operation, you will have to get the parent's Id and then insert the children.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

First you have to insert the parent because you don't have its Ids. After you perform the operation, you will have to get the parent's Id and then insert the children.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
This was selected as the best answer
James LoghryJames Loghry
List<Case> lCase = new List<Case>();
List <Case> nCase = new List<Case>();
for(Integer i=0; i<20; i++){
    lCase.add(
        new Case(
            recordtypeid=rsc,
            accountid=acc1.id,
            contactid=con1.id,
            Case_Name__c='John Doe',
            case_phone_number__c='(800) 555-1212',
            subject='Test Recycling Case',
            origin = 'Phone',
            status='Escalated to Management',
            Escalated_To__c='Return to Liaison',
            description='This is a parent Case'
        )
   );
}
insert lCase;

for(Integer i=0; i < 20; i++){
    nCase.add(
        new Case(
            recordtypeid=pdc,
            parentid=lCase.get(i).id,
            accountid=acc1.id,
            contactid=con1.id,
            status='Closed',
            Case_Name__c='John Doe',
            case_phone_number__c='(800) 555-1212',
            subject='Escalation: Test Recycling Case',
            description='This is an escalated Case'
        )
    );
}
insert nCase;

Adding to Zuinglio's comment, here's what your test setup might look like.
bouscalbouscal
Thanks James, seems I have a further issue in that my trigger is firing before my test data is in the system.  Inserting the lCase records causes the trigger (After Update) to fire which sets the flag blocking recursion so inserting the nCase list, and subsequent tasks between my starttest and stoptest statements, are not running. 
bouscalbouscal
How do I keep the trigger from firing on the first insert?  
I need the child cases to be tested and if the first insert has fired the trigger the 2nd insert won't because the trigger is executing and I've coded to block the recursion.
bouscalbouscal
Each method can only have a single DML statement apparently.  Breaking my test code down into a method to insert the parent cases and a separate method to create the child cases, and then separate methods for each test has resolved my issue.