+ Start a Discussion
Ryan GreeneRyan Greene 

Error on Deployment CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

I tried deploying this Trigger to my production this morning. It is a simple trigger to convert any names to Propper case, first and last. It seems like another APEX Class is interfeering with the deployment. The error points to a managed package I am not able to edit. Any ideas on how to get around this error? 

Full Error: 
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Proper: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Proper: line 5, column 1: [] 
Stack Trace: Class.dlrs.RollupService.testHandler: line 277, column 1 Class.dlrs_LeadTest.testTrigger: line 11, column 1

APEX Trigger:
trigger Proper on Lead (before insert) {
  for(Lead l : Trigger.new){
    if(l.FirstName != null)
      l.FirstName =  l.FirstName.subString(0 ,1).ToUpperCase() + l.FirstName.subString(1);
      l.LastName =  l.LastName.subString(0 ,1).ToUpperCase() + l.LastName.subString(1);
  }
}

 
Best Answer chosen by Ryan Greene
James LoghryJames Loghry

Hi Ryan,

If you look closely at the exception you posted, there is a null pointer exception thrown at line 5 in your trigger.

It looks like LastName is null in some cases.  You're checking for FirstName being null, which is great.  However, you forgot about LastName.

I also prefer to use String.isEmpty instead of != null in cases like this, as isEmpty checks for nulls AND empty strings.

Try the following trigger instead:

trigger Proper on Lead (before insert) {
  for(Lead l : Trigger.new){
    if(!String.isEmpty(l.FirstName)){
      l.FirstName =  l.FirstName.subString(0 ,1).ToUpperCase() + l.FirstName.subString(1);
    }

    if(!String.isEmpty(l.LastName)){
      l.LastName =  l.LastName.subString(0 ,1).ToUpperCase() + l.LastName.subString(1);
  }
}
 

 

All Answers

James LoghryJames Loghry

Hi Ryan,

If you look closely at the exception you posted, there is a null pointer exception thrown at line 5 in your trigger.

It looks like LastName is null in some cases.  You're checking for FirstName being null, which is great.  However, you forgot about LastName.

I also prefer to use String.isEmpty instead of != null in cases like this, as isEmpty checks for nulls AND empty strings.

Try the following trigger instead:

trigger Proper on Lead (before insert) {
  for(Lead l : Trigger.new){
    if(!String.isEmpty(l.FirstName)){
      l.FirstName =  l.FirstName.subString(0 ,1).ToUpperCase() + l.FirstName.subString(1);
    }

    if(!String.isEmpty(l.LastName)){
      l.LastName =  l.LastName.subString(0 ,1).ToUpperCase() + l.LastName.subString(1);
  }
}
 

 

This was selected as the best answer
Asif Ali MAsif Ali M
Hi Ryan,
Seems like the LastName is not set. Add a not null check on LastName too to fix the issue.

Run this code to see the error:
Lead l = new Lead();
l.LastName =  l.LastName.subString(0 ,1).ToUpperCase() + l.LastName.subString(1);

No Error when LastName is set:
Lead l = new Lead();
L.LastName = 'Greene';
l.LastName =  l.LastName.subString(0 ,1).ToUpperCase() + l.LastName.subString(1);


 
Ryan GreeneRyan Greene
Thanks @James Loghry
Good tip on the isEmpty too. Deployed new code without errors