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
dev_sfdc1dev_sfdc1 

Getting Error in Test Class

Hi 
I'm getting Error in Test Class like below :
System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Auto_generated: execution of AfterInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Test Class:

@isTest 
private class Emailtocase_TestClass
{
     static TestMethod void Emailtocase_TestClass()
    {
        
        Test.StartTest();  
        
        Case c= new Case();
        c.Subject = 'Testing';
        //insert c;
       
        
       Contact con = new Contact();
       con.LastName = 'Testing Contact';
       con.Email = 'test1@gmail.com'; 
       insert con;  
       Contact c1 = [Select id,LastName,Email from Contact where Email = 'test1@gmail.com'];
        
        Case ca = new Case();
        ca.Id = c.Id;
        ca.Subject='Welcome';
        ca.Status = 'email';
        ca.Origin='New';
        ca.ContactId = c1.Id;
        ca.SuppliedEmail= 'test1@gmail.com';
        ca.Task_Taken__c=false;
        ca.OwnerId = '00G90000002Gb76';
        upsert ca;
        
        Account a = new Account();
        a.Domain_Name__c = 'gmail.com';        
        a.Name='Testing Account';
        insert a;
        Account a1 = [Select id,Domain_Name__c,Name,Phone from Account where Domain_Name__c = 'gmail.com'];
        Case cas1= new Case();
        cas1.Subject = 'Testing';
        cas1.SuppliedEmail= 'test@gmail.com';
        
        List<contact> lstcon =new List<contact>();
        contact con1=new contact(lastname='sas',Email = cas1.SuppliedEmail,AccountId=a1.Id);
        lstcon.add(con1);
        insert lstcon;
        
       
        
       Case ca1 = new Case();
        ca1.Id = cas1.Id;        
        ca1.Subject='Welcome';
        ca1.Status = 'email';
        ca1.Origin='New';       
        ca1.AccountId=a1.Id;
        ca1.Task_Taken__c=false;
        ca1.OwnerId = '00G90000002Gb76';       
        ca.ContactId = lstcon[0].Id;
        Upsert ca1;// Here I'm getting error line is -Class.Emailtocase_TestClass.Emailtocase_TestClass: line 57, column 1
      
        Test.StopTest();
        
        
        
    }
}
The above covers 54% so how to solve this error.

Thanks
Steven NsubugaSteven Nsubuga
Could it because you commented out the line that was inserting the Case you created? I am referring to the line: // insert c;
Rohit SharmaGRohit SharmaG
Hi Change
Line  ca.ContactId = lstcon[0].Id; to ca1.ContactId = lstcon[0].Id; and try.

Thanks,
Rohit Sharma
Swati GSwati G
You have not inserted cas1
dev_sfdc1dev_sfdc1

Hi,
Thanks for the reply.I followed the given suggestions here.
My test class improved 64% but still getting error failure message
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Auto_generated: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

@isTest 
private class Emailtocase_TestClass
{
     static TestMethod void Emailtocase_TestClass()
    {
        
        Test.StartTest();  
        
        Case c= new Case();
        c.Subject = 'Testing';
        
       
        
       Contact con = new Contact();
       con.LastName = 'Testing Contact';
       con.Email = 'test1@gmail.com'; 
       insert con;  
       Contact c1 = [Select id,LastName,Email from Contact where Email = 'test1@gmail.com'];
        
        Case ca = new Case();
        ca.Id = c.Id;
        ca.Subject='Welcome';
        ca.Status = 'email';
        ca.Origin='New';
        ca.ContactId = c1.Id;
        ca.SuppliedEmail= 'test1@gmail.com';
        ca.Task_Taken__c=false;
        ca.OwnerId = '00G90000002Gb76';
        upsert ca;
        
        Account a = new Account();
        a.Domain_Name__c = 'gmail.com';
        a.Phone='9898989999';
        a.Name='Testing Account';
        insert a;
        Account a1 = [Select id,Domain_Name__c,Name,Phone from Account where Domain_Name__c = 'gmail.com'];
        Case cas1= new Case();
        cas1.Subject = 'Testing';
        cas1.SuppliedEmail= 'test@gmail.com';
        insert cas1;// If I enable this line to insert means getting insert failed error message - Please correct me if I'm inserting wrong.

        List<contact> lstcon =new List<contact>();
        contact con1=new contact(lastname='sas',Email = cas1.SuppliedEmail,AccountId=a1.Id);
        lstcon.add(con1);
        insert lstcon;
        
       
        
       Case ca1 = new Case();
        ca1.Id = cas1.Id;        
        ca1.Subject='Welcome';
        ca1.Status = 'email';
        ca1.Origin='New';       
        ca1.AccountId=a1.Id;
        ca1.Task_Taken__c=false;
        ca1.OwnerId = '00G90000002Gb76';       
        ca1.ContactId = lstcon[0].Id;
        Upsert ca1;
      
        Test.StopTest();
        
        
        
    }
}

Thank you
Swati GSwati G
Origin is required field on case. Please add all the requried fields of Case. Do you have any trigger on case insert?