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
Esther OnemaEsther Onema 

Null Pointer Exception Help

Hello, 
I am attempting to create a trigger that populates an empty Country Code field (on an Event) given that the Assigned to Country Code (the event is assigned to someone) field is populated. I receive the following error after running a test on my Trigger. 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SVMXC.Event_Trigger1: execution of BeforeInsert

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

Trigger.SVMXC.Event_Trigger1: line 104, column 1: []


Here is my trigger
public class SVMX_PopulateActivityCountryCode extends TriggerAction
{
    public Id eventRecordTypeId = Schema.SObjectType.Event.getRecordTypeInfosByName().get('PS WP CS').getRecordTypeId();
    List<ID> eventIDList = new List<ID>();
    public Event[] eventsToUpdate = new Event[]{};

    
    public SVMX_PopulateActivityCountryCode() 
    {
        //Call super to set batch variables and context 
        super();
    }
    
    public override Boolean shouldRun(){
        if(this.isInsert()&&!this.hasRun())
        {
            for(Event e: (List<Event>)Trigger.new)
            { 
                if(e.RecordTypeId == eventRecordTypeId && e.SXWP_Country_Code__c==null&& e.OwnerId!=null&&e.SXWP_Assigned_To_Country_Code__c!=null) 
                {
                    eventIDList.add(e.Id);
                }
            }
        }
        return !eventIDList.isEmpty();
    }
    
    
    public override void doAction()
    {
        this.markRun();
        
        for(Event existingEvent:[SELECT id FROM event WHERE id IN:eventIDList])
        {
            existingEvent.SXWP_Country_Code__c = existingEvent.SXWP_Assigned_To_Country_Code__c;
            eventsToUpdate.add(existingEvent);
        }
    
        update eventsToUpdate;
    }
    
    
}

and Here is the test class 
@isTest 
public class SVMX_PopulateActivityCCTest 
{
    private static Id techRecordTypeId = Schema.SObjectType.SVMXC__Service_Group_Members__c.getRecordTypeInfosByName().get('Technician').getRecordTypeId();
    private static Id teamRecordTypeId = Schema.SObjectType.SVMXC__Service_Group__c.getRecordTypeInfosByName().get('Technician').getRecordTypeId();
    private static Id eventRecordTypeId = Schema.SObjectType.Event.getRecordTypeInfosByName().get('PS WP CS').getRecordTypeId();
	private static Event currentEvent = new Event();
    private static User u = new User();
    private static SVMXC__Service_Group__c team = new SVMXC__Service_Group__c();
    private static SVMXC__Service_Group_Members__c technician = new SVMXC__Service_Group_Members__c();



    
    private static void setUpData()
    {
        team.RecordTypeId = teamRecordTypeId;
        team.Name = 'Carnival';
        insert team;
        
        u= TestUtil.createUser('00e120000019I9K');
        u.GID__c = 'Z002SH0P';
        insert u;
        
       	technician.RecordTypeId = techRecordTypeId;
        technician.SVMXC__Service_Group__c = team.Id;
        technician.Name = 'Victor Hugo';
        technician.SVMXC__Enable_Scheduling__c = True;
        technician.SVMXC__Active__c = True; 
        //The meat of the trigger lies in the following line
        technician.SXWP_Country_Code__c = 'CA';
        technician.SXWP_GID__c ='Z002SH0P';
        insert technician;
        
       id userId = [Select id from User where GID__c ='Z002SH0P'].get(0).Id;
      String techCC = [SELECT SXWP_Country_Code__c from SVMXC__Service_Group_Members__c WHERE SXWP_GID__c ='Z002SH0P' limit 1][0].SXWP_Country_Code__c;
       currentEvent.SXWP_Country_Code__c= NULL;
       currentEvent.Record_Id__c = eventRecordTypeId;
       currentEvent.OwnerId = userId;
       currentEvent.Subject = 'Event Test';
       currentEvent.Location = 'Location';
       currentEvent.Description='description';
       currentEvent.IsAllDayEvent=false;
       currentEvent.Start_Date__c= date.newInstance(1990, 11, 21);
        currentEvent.EndDateTime = date.newInstance(1990, 11, 22);
       insert currentEvent;
        

    }
    
    
     Static testmethod void updateEvent()
     {
         Test.startTest();
         setUpData();
         
         List<SVMXC__Service_Group_Members__c > sgm = [Select SXWP_Country_Code__c from SVMXC__Service_Group_Members__c Where Id = :technician.Id LIMIT 1];
         system.assertEquals(1,[Select Count() from SVMXC__Service_Group_Members__c  Where Id = :technician.Id]);
         
         List<Event> e = [Select SXWP_Assigned_To_Country_Code__c,SXWP_Country_Code__c from Event Where Id = :currentEvent.Id LIMIT 1];
         system.assertEquals(1,[Select Count() from Event Where Id = :currentEvent.Id]);
         
         system.assertNotEquals(NULL,e[0].SXWP_Country_Code__c);
         system.assertEquals(e[0].SXWP_Assigned_To_Country_Code__c,e[0].SXWP_Country_Code__c);
         Test.stopTest();
     }

    
}

 
Best Answer chosen by Esther Onema
Dushyant SonwarDushyant Sonwar

Did you install this app from appexchange ? If yes, then you can ask the publisher to fix these issue if it is from manage package.
You can report to them about null pointer exception.

All Answers

Dushyant SonwarDushyant Sonwar

Esther,

Event_Trigger1 has issue on line 104 System.NullPointerException: . You are accessing a element that does not been initialised or not in memory.

If possible, please add trigger code also so others can review your Event_Trigger1  code.

Esther OnemaEsther Onema
Hello,
Unfortunately Event_Trigger1 is a managed package from the ServiceMax application--I am unable to view the code as it is hidden from me.
Dushyant SonwarDushyant Sonwar

Did you install this app from appexchange ? If yes, then you can ask the publisher to fix these issue if it is from manage package.
You can report to them about null pointer exception.

This was selected as the best answer
Esther OnemaEsther Onema
I contacted the publisher and was told the issue occured because I never included currentEvent.StartDateTime. Apparently this is a mandatory field on the the object that I failed to recognize. Thank you for the help!
Dushyant SonwarDushyant Sonwar

I think they have made field required using pagelayouts not made required through database  , that's why it didn't give any error when creating the testdata in testclass.But i think they should make their code more robust  to avoid errors like null pointer exception. 

That's good to know that your issue has been resolved.

Thank you Esther for letting us know :)