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
Felix Jong Seok ChaeFelix Jong Seok Chae 

Trigger is not firing in Test Method

I created a new case with anonymous window and trigger succesfully set a field of a new case.
However, when I tried to create a new case (w/ same fields' values as one before) in test method, it does not update a field of this case via trigger.
I followed the help from the forum saying that I should fetch a case using soql after insertion, but it did not work.
Can someone please help me on this?
@isTest static void singleCaseInsertion() {
        Case newCase = new Case(...);
        insert newCase;
        test.startTest();
        Case newCasePtr = [Select casenumber, id, similarcases__c from case where id = :newCase.id];
        String newCaseSimCases = newCasePtr.similarCases__c;
        test.stopTest();
        System.assert(newCaseSimCases != null); // I get null value for this field, which is wrong.

    }

 
HARSHIL U PARIKHHARSHIL U PARIKH
On line 2 when you say Insert newCase; it is not inserting any new case at all since you need to provide required field on Case Object.

Have something along following lines..
@isTest 
Public Class ingleCaseInsertion
{
    static TestMethod void singleCaseInsertion() 
    {
        Case newCase = new Case();
        newCase.Status = 'New';
        newCase.Origin  = 'Phone';
        
        insert newCase;
        
        
        test.startTest();
        List<Case> newCasePtr = [Select CaseNumber, id, similarcases__c from case where id = :newCase.id];
        String newCaseSimCases = newCasePtr[0].similarCases__c;
        test.stopTest();
        System.assert(newCaseSimCases != null); // I get null value for this field, which is wrong.

    }
}

Hope it helps!
 
Felix Jong Seok ChaeFelix Jong Seok Chae
Hi Govind,
I intentionally make new Case(...) as I don't want to show all fields in my post, but I did set fields for this new case.
I followed your code, but still have same assertion error. I'm sure my trigger works when I create the case via anonymous window as I can see similarcases__c field is not null after inserting a new case. 
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Felix,

I think you may need to check if the case is inserted or not and what value in coming in similarcases__c

Have below statement After line 10
 
System.debug('newCase Is is' + newCase.Id);

Have below statement before line 15 in above trigger,
 
System.debug('newCasePtr has a similarcases__c = ' + newCasePtr[0].similarCases__c);

Make sure that newCase.Id is not null and similarcases__c is not null
Felix Jong Seok ChaeFelix Jong Seok Chae
I printed them out after erasing @istest from the method and I can see newCasePtr[0].similarCases__c is not null. When I included @istest for the real test, I see newCase.id is not null after insertion, but its similarCases__c after soql query is still null. Is it maybe that trigger was set to be inactive if method is in context of testing (@istest)?
HARSHIL U PARIKHHARSHIL U PARIKH
try using
 
String newCaseSimCases = String.ValueOf(newCasePtr[0].similarCases__c);

 
Felix Jong Seok ChaeFelix Jong Seok Chae
I got the same error.
HARSHIL U PARIKHHARSHIL U PARIKH
right after the

 insert newCase;

add a line

Update newCase;
HARSHIL U PARIKHHARSHIL U PARIKH
Because the trigger is may be running on After Update
Felix Jong Seok ChaeFelix Jong Seok Chae
Thanks, but it still doesn't work. One thing I found out was that when I try with other trigger (about 5 lines), that trigger fires in @istest context. The troubling trigger is about 80 lines plus it calls other methods from other classes as well. There gotta be something different between two triggers that stops firing the first trigger. Can you think of any reason for this?
HARSHIL U PARIKHHARSHIL U PARIKH
Best practive to have a one trigger per object Felix, since we would never know which one is firing first though..
If the trigger calls other methods then we need to make sure that whatever the required record creation is required for other methods, are taken cared as well.

What daya type is similarCases__c?
 
HARSHIL U PARIKHHARSHIL U PARIKH
Also try someting like,
 
@isTest 
Public Class ingleCaseInsertion
{
    static TestMethod void singleCaseInsertion() 
    {
        Case newCase = new Case();
        newCase.Status = 'New';
        newCase.Origin  = 'Phone';
        
        insert newCase;
        
        

        List<Case> newCasePtr = [Select CaseNumber, id, similarcases__c from case where id = :newCase.id];
        String newCaseSimCases = newCasePtr[0].similarCases__c;
        Integer I = newCaseSimCases.length();


       System.assertNotEquals(I, 0);

    }
}

 
Felix Jong Seok ChaeFelix Jong Seok Chae
Data type of similarcases__c is text.
I tried the integer comparison, but didn't work either.
Felix Jong Seok ChaeFelix Jong Seok Chae
Godvin,
I found the solution and it happened because test method was not accessible to sf database. I used "@isTest(SeeAllData=true)" above the test class, and it worked.