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
BialBial 

Issue in test code for trigger in eclipse.

Hi.

 
I am writing test code for trigger in case for sending email to contacts of case by updating or inserting any case field. But i am facing problem in it. Please help me to sort out this issue. Actually I wrote the following trigger and test code, also the error is given below.
 
Trigger Code
 
trigger sendemaildemo on Case (after insert, after update) {
 
final String template = 'Test';
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
 
Messaging.SingleEmailMessage message1 = new Messaging.SingleEmailMessage();
 
Messaging.SingleEmailMessage message2 = new Messaging.SingleEmailMessage();
 
Messaging.SingleEmailMessage message3 = new Messaging.SingleEmailMessage();
 
Messaging.SingleEmailMessage message4 = new Messaging.SingleEmailMessage();
 
 
 
message.setTemplateId([select id from EmailTemplate where Name =:template].id);
 
message1.setTemplateId([select id from EmailTemplate where Name =:template].id);
 
message2.setTemplateId([select id from EmailTemplate where Name =:template].id);
 
message3.setTemplateId([select id from EmailTemplate where Name =:template].id);
 
message4.setTemplateId([select id from EmailTemplate where Name =:template].id);
 
 
 
 
 
// Send single mail to contact of each updated case
for (Case uc: Trigger.new) {
 
Contact c = [select Email from Contact where Id =:uc.ContactId and Email!=null];
 
message.setTargetObjectId(c.Id);
message.setWhatId(uc.Id);
 
message.setToAddresses(new String[] {c.Email});
Messaging.sendEmail(new Messaging.Email[] {message});
}
 
for (Case vc: Trigger.new) {
IF (vc.Contactemail__c!=null){
Contact v = [select Email from Contact where Id =:vc.Contact__c and Email != null];
 
message1.setTargetObjectId(v.Id);
message1.setWhatId(vc.Id);
 
message1.setToAddresses(new String[] {v.Email});
Messaging.sendEmail(new Messaging.Email[] {message1});
}
 IF (vc.Contact2email__c!=null) {
Contact b = [select Email from Contact where Id =:vc.Contact2__c and Email != null];
 
message2.setTargetObjectId(b.Id);
message2.setWhatId(vc.Id);
 
message2.setToAddresses(new String[] {b.Email});
Messaging.sendEmail(new Messaging.Email[] {message2});
 
 
}
 IF (vc.Contact3email__c!=null) {
Contact d = [select Email from Contact where Id =:vc.Contact3__c and Email != null];
 
message3.setTargetObjectId(d.Id); 
message3.setWhatId(vc.Id);
 
message3.setToAddresses(new String[] {d.Email});
Messaging.sendEmail(new Messaging.Email[] {message3});
 
}
 
 
IF (vc.Contact4email__c!=null) {
Contact f = [select Email from Contact where Id =:vc.Contact4__c and Email != null];
 
message4.setTargetObjectId(f.Id);
message4.setWhatId(vc.Id);
 
message4.setToAddresses(new String[] {f.Email});
Messaging.sendEmail(new Messaging.Email[] {message4});
}
 
else {
}
 
}
}
 
 
Test Code.
 
@isTest
private class sendemaildemo {
 
    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        Case caset = new Case ();
        caset.Origin = '2F5008000000IEE2g';
        caset.Status = 'New';
        caset.Origin = 'CheseTek'; 
        caset.ContactId = '0038000001Doy5g';
        caset.Contact__c = '0038000001Doy5g';
        caset.Contact2__c = '0038000001Doy5g';
caset.Contact3__c = '0038000001Doy5g';
caset.Contact4__c = '0038000001Doy5g';
 
insert caset;
 
 
 
 
    }
}
 
 
Error

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, sendemaildemo: execution of AfterInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.sendemaildemo: line 16, column 1: []

 
 
Thanks in Advance!
Noam.dganiNoam.dgani

Hi Bial

 

when you run test methods, unless you specify differently - there is no data in your system.

 

so, i;m not sure where line 16 actually is, but you are porbably getting that exception on querying the contact or templates becuase they are not there.

 

you can use:

@isTest(SeeAllData=true)

static void myUnitTest() {

 

}

 

which will cause you unit test to include the data already in your system.

 

Now, a few coding principles:

  1. in unit test - create your data in the unit test. dont rely on your system to have it.
  2. dont query inside a for loop (you query for contact twice and case once) - you will hit governer limits really quick.
  3. what is 0038000001Doy5g in your unit test? a contact from your dev?/Production env - what if its not there? or has a different id in every env (which it does)? or someone deletes it?

 

Hope this puts you on a right track - if it does, kindly mark it as resolved.