0

I have updated my triggers with few lines of code and a custom setting which will allow me to dynamically activate/inactivate the trigger in production. But now the issue is that my test class is failing due to it whenever a trigger fires in the test class. Below is the error message I am receiving.

Error Message   System.DmlException: Update failed. First exception on row 0 with id 5008A000002YUVTQA4; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CaseTrigger: execution of AfterUpdate

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

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

Trigger.CaseStatusHistoryTrigger: line 4, column 1: []

Class.CaseTriggerHelper.CaseAuditMethod: line 106, column 1
Trigger.CaseTrigger: line 65, column 1: []
Stack Trace Class.CaseTriggerHelper_Test.Test1: line 57, column 1

Below is my trigger.

Trigger CaseStatusHistoryTrigger on Case_Status_History__c(After Insert) 
{
    TriggerSetting__c ObjCustomSetting = TriggerSetting__c.getInstance('CaseStatusHistoryTrigger');
    IF(ObjCustomSetting.IsTriggerActive__c == FALSE)  //This is line no 4.
    { 
        Return;
    }

    IF(Trigger.IsAfter)
    {
        IF(Trigger.IsInsert)
        {
            Boolean CallThatMethod = FALSE;
            For(Case_Status_History__c CSH : Trigger.New)
            {
                IF(CSH.New_Status_Value__c == 'Closed')
                {
                    callThatMethod = true;
                }                             
            }
            IF(callThatMethod) 
            {
                CaseStatusHistoryTriggerHelper.UpdateCSHFields(Trigger.New);
            }
        }
    }
}

Below is the test class.

//This test class is written for testing the class "CaseTriggerHelper" and the trigger "CaseTrigger".
@IsTest
Public Class CaseTriggerHelper_Test
{
    Public Static TestMethod Void Test1()
    {
        Test.startTest();

        RecordType parentRT = [select id from RecordType where name = 'Parent Account' and SObjectType = 'Account' limit 1];
        System.assertNotEquals(null, parentRT.id);

        Account Acc = New Account(Name = 'New Test Account' , RecordTypeId = parentRT.id);
        Insert Acc;
        System.assertNotEquals(null, Acc.id);

        Contact Con = New Contact(FirstName = 'New Test' , LastName = 'Contact' , AccountId= Acc.Id , Email = 'abcd@efg.com' , Phone = '9191919191' , Contact_Type__c = 'Customer' , Will_Sample_be_sent_to_customer__c = 'NO');
        Insert Con;
        System.assertNotEquals(null, Con.id);

        RecordType CaseRT = [select id from RecordType where name = 'Delivery Escalation' and SObjectType = 'Case' limit 1];
        System.assertNotEquals(null, CaseRT.id);

        Product2 Produc1 = New Product2(Name = 'Product 1' , PL_product_design_id__c = '12345');
        Insert Produc1;
        System.assertNotEquals(null, Produc1.id);

        Product2 Produc2 = New Product2(Name = 'Product 1' , PL_product_design_id__c = '12345');
        Insert Produc2;
        System.assertNotEquals(null, Produc2.id);

        Case Cas = New Case(AccountId = Acc.Id , ContactId = Con.Id , Origin = 'Phone' , Status = 'New' , Subject = 'New Case' , RecordTypeId = CaseRT.id , Part_Number__c = produc1.id , Product__c = produc2.id , DP_Entity__c = '1234' , Quantity__c = 2 , Revenue_Impact__c = 12 , Type = 'MAD > CRD');
        Insert Cas;
        System.assertNotEquals(null, Cas.id);

        Profile Prof = [select id from Profile where name = 'System Administrator' limit 1];
        System.assertNotEquals(null, Prof.id);

        User U = New User(FirstName = 'Test' , LastName = 'User' , Alias = 'UsTe' , Username = 'TestUsername_1@Test.com' , Email = 'TestUsername@Test.com' , TimeZoneSidKey = 'America/Chicago' , LocaleSidKey = 'en_US' , LanguageLocaleKey = 'en_US' , EmailEncodingKey = 'ISO-8859-1' , ProfileId = prof.id);
        Insert U;
        System.assertNotEquals(null, U.id);

        CaseTeamRole CTR1 = [Select Id from CaseTeamRole where Name = 'BU' limit 1];
        System.assertNotEquals(null, CTR1.id);

        CaseTeamRole CTR2 = [Select Id,Name from CaseTeamRole where Name = 'DA' limit 1];
        System.assertNotEquals(null, CTR2.id);

        CaseTeamMember CTM1 = New CaseTeamMember(ParentId = Cas.Id , MemberId = Con.Id , TeamRoleId = CTR1.Id);
        Insert CTM1;
        System.assertNotEquals(null, CTM1.id);

        CaseTeamMember CTM2 = New CaseTeamMember(ParentId = Cas.Id , MemberId = U.Id , TeamRoleId = CTR2.Id);
        Insert CTM2;
        System.assertNotEquals(null, CTM2.id);

        Cas.Status = 'DA Working';
        Update Cas; // This is line 57. Since here 'CaseStatusHistoryTrigger' fires then the error is thrown.
        System.assertEquals('DA Working',Cas.Status);

        try
        {
        Cas.Status = 'OF Working';
        Update Cas;
        System.assertEquals('CSO Working',Cas.Status);
        }
        catch(Exception e)
        {
        Boolean expectedExceptionThrown =  e.getMessage().contains('No Case Team Member Exist with this Role. Please change the status which has its corresponding Team Member Present') ? true : false;
        //System.AssertEquals(expectedExceptionThrown, true);
        } 

        RecordType CaseRTCSP = [select id from RecordType where name = 'CSP Mix Request' and SObjectType = 'Case' limit 1];
        System.assertNotEquals(null, CaseRT.id);

        Product2 Prod1 = New Product2(Name = 'Product 1' , PL_product_design_id__c = '12345' , PL_PART_NUMBER__c = '6789');
        Insert Prod1;

        Case Cas2 = New Case(AccountId = Acc.Id , ContactId = Con.Id , Origin = 'Phone' , Status = 'New' , Subject = 'New Case' , RecordTypeId = CaseRTCSP.id , Current_MPN__c = Prod1.Id);
        Insert Cas2;
        System.assertNotEquals(null, Cas2.id);     

        CaseTeamMember CTM3 = New CaseTeamMember(ParentId = Cas2.Id , MemberId = U.Id , TeamRoleId = CTR2.Id);
        Insert CTM3;
        System.assertNotEquals(null, CTM3.id); 

        Cas2.Status = 'DA Working';
        Update Cas2;

        Cas2.Status = 'CSO Working';
        Update Cas2;  

        Test.stopTest();
    }
}

The records exist in the custom setting. Below is the snapshot of the same.

enter image description here

Test class is failing for all the 3 triggers whose records I have created in custom setting.

Please guide me in resolving this issue.

Thanks! Ruchika Saxena

  • seems like record with name CaseStatusHistoryTrigger doesn't exist – Ilya Lepesh Apr 26 '16 at 7:24
  • @IlyaLepesh : The records exist in the custom setting. I have added the snapshot of the same. Test class is failing for all the 3 triggers whose records I have created in custom setting. – Ruchi Apr 26 '16 at 7:29
  • I'm afraid I see space between, but you are using TriggerSetting__c, isnt it Trigger_Setting__c ? – Ilya Lepesh Apr 26 '16 at 7:31
  • 2
    You will need to insert the custom setting instance in your test just as you insert other data such as Account. – Keith C Apr 26 '16 at 7:35
  • 1
    You need getInstance('CaseStatusHistoryTrigger') to not return null so it will have to be with the original trigger name. Tests are isolated from list custom settings just as they are isolated from normal SObjects. – Keith C Apr 26 '16 at 8:55
0

According to your error message (System.NullPointerException: Attempt to de-reference a null object) and your code:

TriggerSetting__c ObjCustomSetting = TriggerSetting__c.getInstance('CaseStatusHistoryTrigger');
IF(ObjCustomSetting.IsTriggerActive__c == FALSE)

the reason seems that you do not have this custom setting in your test. Line 4 would only throw this exception if ObjCustomSetting is null, so that means that on line 3:

TriggerSetting__c.getInstance('CaseStatusHistoryTrigger')

this part is returning null.

Make sure that you have this custom setting in your test class and it will fix your problem

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.