Appendix II: Code that Must Be Added to Custom Test Classes

If you have custom test classes in your org, then, if any of that code can cause Contact records to be inserted or updated, you may see an error when your test class runs, such as:

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

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

In this case, you need to insert the following method (“populateCustomSetting”) into each test class that causes Contact records to be inserted or updated. And then you need to call the populateCustomSetting method in your test method as the first operation to insert a custom setting record as test data.

The Method

Private static void populateCustomSetting(){
    ERx_Events__Event_Registration_Settings__c eventSetObj = new
    ERx_Events__Event_Registration_Settings__c();
    eventSetObj.Name = 'DEFAULTS';
    eventSetObj.ERx_Events__Group_Name__c = 'ERxCampusGroup';
    eventSetObj.ERx_Events__Contact_Email_Address__c = 'eventstest@test.com';
    eventSetObj.ERx_Events__Default_Template_Name__c = 'Test';
    eventSetObj.ERx_Events__Event_Full_Message__c = 'Registration is Full';
    eventSetObj.ERx_Events__Invalid_Date_Message__c = 'Invalid Date';
    eventSetObj.ERx_Events__Is_DatePicker_Required__c = true;
    eventSetObj.ERx_Events__New_Contact_Lead_Source__c = 'Web';
    eventSetObj.ERx_Events__Thank_You_Message__c = 'Thanks';
    eventSetObj.ERx_Events__Is_Send_Email__c = true;
    eventSetObj.ERx_Events__is_Send_QR_Code__c = true;
    eventSetObj.ERx_Events__Campus_Campaign_Recurrence__c = 15;
    eventSetObj.ERx_Events__Time_Zone__c = 'GMT-06 : 00 Central Standard 
    Time (America/Chicago)';
    eventSetObj.ERx_Events__Sender_Email__c = 'developer@test.com';
    eventSetObj.ERx_Events__Deactivate_random_string_for_contact__c = false;
    insert eventSetObj;        
}

Calling the Method

Static testmethod void testContct Trigger(){
    populateCustomSetting();