0

I'm new to Apex and Code...

A trigger from a managed package is causing my test class to fail. Here's the code for the trigger causing the error:

trigger RAPID_SVMXC_BeforeInsertUpdateDelete_WorkDetails on SVMXC__Service_Order_Line__c (before insert, before update,before delete) {

Activate_Timesheet__c Activate_Timesheet   = new Activate_Timesheet__c();  // name of Custom Setting
Activate_Timesheet = Activate_Timesheet__c.getValues('TimesheetState');  // grab the specific setting

if (Activate_Timesheet.Enable_Timesheet__c==true || RAPID_SVMXC_TimesheetUtils_UT.TimesheetUtilsTest==true)
{
    if (trigger.isDelete) 
    {
        RAPID_SVMXC_TimesheetUtils.removeTimesheetEntriesFromWorkDetails(trigger.oldMap.keySet());
    }
    else
    {
        RAPID_SVMXC_TimesheetUtils.checkForWorkDetailOverlap(trigger.new,trigger.oldMap);
    }
}}

The error message is:

caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.RAPID_SVMXC_BeforeInsertUpdateDelete_WorkDetails: line 7, column 1: []

Line 7 in that trigger is: if (Activate_Timesheet.Enable_Timesheet__c==true || RAPID_SVMXC_TimesheetUtils_UT.TimesheetUtilsTest==true)...

I can't figure out why it's causing this error if "Activate_Timesheet__c" is a Custom Setting, why is a missing lookup field on my record insert causing that?

2

You need to create custom setting in your test class.

Insert new Activate_Timesheet__c (field__c = value);

  • 1
    And the reason for this is that Custom Setting data in an org is not available in unit tests. If you (OP) don't create the data, you'll get null back from Activate_Timesheet__c.getValues('TimesheetState');. That was the cause of the NPE you were getting. – Derek F Nov 8 '17 at 19:08

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.