I have the error message above, but I am not sure, what else to take care of. My Code is the following and the error is in a test class

My Code:

trigger AddAccEuroAISNo on Task (before insert, before update) {
for (Task a : trigger.new){ 
          if(Task.WhoId != null){
        String WhoIdString = String.valueof(a.WhoId);
            if (WhoIdString.substring(0,3) == '003'){
            Contact parent = [SELECT AccountId, Account.Name, Name, EuroAIS_Customer_Number__c  FROM Contact WHERE ID = :a.WhoId ];
            if(parent.AccountId  != null){
            a.Account__c = parent.Account.Name;
            a.EuroAISCustomerId__c = parent.EuroAIS_Customer_Number__c;
            a.Contact__c = parent.Name;

The Error Message:

Test_MergeCasesController.caseMergeTestMethod(), Details: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AddAccEuroAISNo: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.AddAccEuroAISNo: line 5, column 1: [] Class.MergeCasesController.mergeCases: line 181, column 1 Class.Test_MergeCasesController.caseMergeTestMethod: line 48, column 1

Thanks a lot in advance for any help

  • 1
    How can you confirm a.WhatId is always to be a Case record id. In some cases check will be null and since you are not checking for null your code will break.
    – highfive
    Sep 18 '15 at 9:52

The problem is that you are not setting a value for the WhoId on the Task that you are inserting.

What is essentially happening is this:

Task a = new Task();
String WhoIdString = String.valueof(a.WhoId);
system.debug(WhoIdString.substring(0,3) == '003');

In this line of your code:

if(Task.WhoId != null){    

it looks like you are attempting to check for null values, if you change it to:

if(a.WhoId != null){

you will not get the error message, but your test will also not cover the code inside the if block.

  • but in the test class, there is a WhoId "Task t = testHelper.createTask(con.Id, c3.Id); insert t;" con.Id stands for this "Contact con = testHelper.createContact(acc);" Sep 18 '15 at 9:59
  • @RalfWittenberger it looks like your testHelper class functions aren't inserting the records that they are generating. Make sure you add a insert con; statement after the Contact con = testHelper.createContact(acc);
    – martin
    Sep 18 '15 at 10:02
  • @RalfWittenberger no problem. Definitely leave the a.WhoId part, but if you are coming up short on test coverage, make sure you insert that contact that you are setting as the Who on the task so the test can see the code in the if block.
    – martin
    Sep 18 '15 at 10:09

You cannot insert Activity entity so remove that insertion.

  • Hi Vinod, i do not know, if i understand you right, but from my point, i am not inserting an entity. OR can you explain, what you mean with that? Sep 18 '15 at 9:53
  • @RalfWittenberger in one of your classes, either in MergeCasesController.mergeCases or Test_MergeCasesController.caseMergeTestMethod you are inserting a Task. I assume that the DML operation there had a purpose, and I wouldn't suggest deleting it unless you are sure that you don't want it to serve that function any longer.
    – martin
    Sep 18 '15 at 9:56
  • Means entity cannot be updated which are active or may be used somewhere else
    – Vinod
    Sep 18 '15 at 11:05

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.