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
jfitz1959jfitz1959 

How to create new 'cases' for unit testing

We're creating a unit test for a cases trigger and as part of the process we need to create new cases.

We're using something like this to start but we're stuck...

   ....

   List<case> cases=new List<case>();

   for(integer I=0; I<20; I++)

   {

             string em='joe@gmail.com/'+String.valueof(i);

             cases.add( new case(contact.email=em) ); //<= this throws an Error: Compile Error: Invalid field initializer: contact.email at line 10 column 33

   }

   insert cases;    

   ...

I'm not sure how to fill in the email portion or even if I'll need more info.

Best Answer chosen by Admin (Salesforce Developers) 
Andy BoettcherAndy Boettcher

There isn't a field called "email" in the Case object and you're not accounting for the other required fields for a Case record.

 

You'd be looking at something like this:

 

   List<Case> lstCases = new List<Case>();
   
   // Create Contact
   Contact conNew = new Contact(Lastname='Test Contact');
   insert conNew;
   
// Create Cases for(integer i = 0; i < 20; i++) { lstCases.add(new Case(ContactId=conNew.Id, Status='New', Origin='Phone')); } insert lstCases;

 

All Answers

Andy BoettcherAndy Boettcher

There isn't a field called "email" in the Case object and you're not accounting for the other required fields for a Case record.

 

You'd be looking at something like this:

 

   List<Case> lstCases = new List<Case>();
   
   // Create Contact
   Contact conNew = new Contact(Lastname='Test Contact');
   insert conNew;
   
// Create Cases for(integer i = 0; i < 20; i++) { lstCases.add(new Case(ContactId=conNew.Id, Status='New', Origin='Phone')); } insert lstCases;

 

This was selected as the best answer
jfitz1959jfitz1959

This worked for the most part but I'm curious if I can actually view the records created in contacts and cases?

Andy BoettcherAndy Boettcher

Through a Test Method, the data you instanciate does not get committed to the database.  If you want to see variables and whatnot as the test is firing, you can throw some System.Debug statements in there and view the log.

 

-Andy

jfitz1959jfitz1959

Here is my unit test case

@istest
public class TriggerTest
{
    public static testMethod void myTest()
    {
        List<string> ids = new List<string>();for(integer i=0; i<20; i++)
        {
            string em='joe'+string.valueof(i)+'@gmail.com';
            string last = 'joe'+string.valueof(i);            
            System.Debug('triggerTest:email:'+em);
            Contact cont = new contact(email=em,lastname=last);
            insert cont;
            ids.add(cont.id);
        }
        System.Debug('triggerTest:contacts:'+string.VAlueof(ids.size()));
        List<case> cases=new List<case>();
        for(integer i=0; i<20; i++) {
            cases.add( new case(contactID=ids[i],status='new',origin='web') );
        }
        insert cases;        
    }
}

and here is my trigger:

trigger trig on Case (after insert) {
List<TriggerTest__c> tt = new List<TriggerTest__c>();
for(case c:trigger.new)
{
    System.Debug('trig:origin:'+c.origin);
    System.Debug('trig:status:'+c.status);
    System.Debug('trig:contactId:'+c.contactid);
    System.Debug('trig:name:'+c.contact.lastname);
    System.Debug('trig:email:'+c.contact.email);
    System.Debug('trig:email:'+String.valueof(c.contact.email));
    tt.Add( new TriggerTest__c(EntryTime__c = datetime.now(), Email__c= 'j@hotmail.com'));
}
if(!tt.isempty())
{
    insert tt;
}
}

 

partial ;og trace...no email ????

15:09:43.610 (610320000)|USER_DEBUG|[5]|DEBUG|trig:origin:Web

15:09:43.610 (610364000)|USER_DEBUG|[6]|DEBUG|trig:status:New
15:09:43.610 (610405000)|USER_DEBUG|[7]|DEBUG|trig:contactId:003U0000009g3VTIAY
15:09:43.610 (610430000)|USER_DEBUG|[8]|DEBUG|trig:name:null
15:09:43.610 (610450000)|USER_DEBUG|[9]|DEBUG|trig:email:null
15:09:43.610 (610476000)|USER_DEBUG|[10]|DEBUG|trig:email:null