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 

creating cases and contacts in unit testing?

doing a unit test case on a case trigger and getting results that indicate the new case gets created, but not the new contacts. None of the created contact info gets propagated through but the case info does....not sure whats going on any help?

 

Here is my unit test case

@istest public class TriggerTest

 {

     public static testMethod void myTest()

     {

         List<string> ids = new List<string>(); //more for debugging

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

         {

             //create some contacts first-tried creating contacts with in case creation loop below but that didn't work either...

             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.Valueo​f(ids.size()));//debugging

         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.contac​t.email));

     tt.Add( new TriggerTest__c(EntryTime__c = datetime.now(), Email__c= 'j@email.com'));

 }

 if(!tt.isempty())

 {

    insert tt;

 }

}

 

partial 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:00​3U0000009g3VTIAY

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

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Since you are inserting the record in your test method .So you have to make query on your test record inside the trigger code to get the email , lastname value of contact. Thy the below code as reference:
trigger trig on Case (before 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);
contact cou=[select email,lastname,id from contact where id=:c.contactid];
system.debug('################' +cou.email);

System.Debug('trig:name:'+cou.lastname);
System.Debug('trig:email:'+cou.email);
tt.Add( new TriggerTest__c(EntryTime__c = datetime.now(), Email__c= 'j@email.com'));
}
if(!tt.isempty())
{
insert tt;
}
}

 

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.