+ Start a Discussion
nicodemnicodem 

Help on error CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, System.NullPointerException

Hello all.

 

I'm working since a few time on apex and I develloped a trigger witch works well but My test class is a problem.

 

This is the error message :

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TR04Objectifcompte: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.TR04Objectifcompte: line 6, column 1: []

 

This is my trigger :

 

 

trigger TR04Objectifcompte on Account (before update, before insert) 
{

    for (Account a : trigger.new)
    {
    List<Objectifs_commerciaux_annuels__c> listobj = new List<Objectifs_commerciaux_annuels__c> ([select Id from Objectifs_commerciaux_annuels__c where Nom_du_VRP__r.Id =: a.OwnerId and recordtype.name = 'Objectifs liés au compte' and debut_periode__c <=: a.createddate.date() and fin_periode__c >=: a.createddate.date()]);
   
        if ( listobj.isEmpty() == false && (a.Nouveau_client__c == true || a.Commercialement_inactif__c == false) )
        {
            Objectifs_commerciaux_annuels__c obj = [select Id from Objectifs_commerciaux_annuels__c where Nom_du_VRP__r.Id =: a.OwnerId and recordtype.name = 'Objectifs liés au compte' and debut_periode__c <=: a.createddate.date() and fin_periode__c >=: a.createddate.date()]; 
            a.Objectifs_commerciaux_annuels__c = obj.id;
        }    
    
    }

}

 

This is my test class :

 

@isTest
public class ObjectifcompteTestClass{
  
  
    static testMethod void validateObjectifcompte() 
    {
    
// Creation of the user
Profile p = [select id from profile where name='Administrateur système'];
User u1 = new User(alias = 'standt2', email='standarduser@test12.com',
emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,firstname='Heather',
timezonesidkey='America/Los_Angeles', username='standarduser@test12.com');
insert u1;  

date mydate = date.today();

 

// Creation of the custom object "Objectifs_commerciaux_annuels__c"
String objRecordTypeId = [Select Id From RecordType Where SobjectType = 'Objectifs_commerciaux_annuels__c' and Name = 'Objectifs liés au compte'].Id;
Objectifs_commerciaux_annuels__c obj = new Objectifs_commerciaux_annuels__c(); 
obj.debut_periode__c = mydate.addDays(-15) ; 
obj.fin_periode__c = mydate.addDays(15);
obj.Nom_du_VRP__c = u1.Id;

obj.RecordTypeId = objRecordTypeId;
insert obj;

 

//Creation of the account witch respects conditions trigger
String strRecordTypeId1 = [Select Id From RecordType Where SobjectType = 'Account' and Name = 'Client'].Id;
Account accnt = new Account();
accnt.Name = 'test account';
accnt.RecordTypeId  = strRecordTypeId1;
accnt.OwnerId = u1.Id;
accnt.Nouveau_client__c = true;
accnt.Commercialement_inactif__c = false;
insert accnt ;

 

// Test       

System.assertEquals(obj.Id, accnt.Objectifs_commerciaux_annuels__c );

}
    
}

 

Defintly I know I didn't follow Apex bestpratcices because I have a Soql in a loop in my Trigger.

However, I spend a long time to try to stay in bestpratices and work my test class but I did not succes, so it would be wonderfull if somebody could help me to finish my development.

 

Best Answer chosen by Admin (Salesforce Developers) 
k_bentsenk_bentsen

I suspect that your query is not returning any results when you run your test class. If you're familiar with using the development console, insert the line line as shown below.

 

Objectifs_commerciaux_annuels__c obj = [select Id from Objectifs_commerciaux_annuels__c where Nom_du_VRP__r.Id =: a.OwnerId and recordtype.name = 'Objectifs liés au compte' and debut_periode__c <=: a.createddate.date() and fin_periode__c >=: a.createddate.date()]; 

System.debug('The obj ID is: ' + obj.Id);
a.Objectifs_commerciaux_annuels__c = obj.id;

 

Then look at your debug log and look for this line. If you see "NULL" then look for the SOQL query that would be just above in the log and try to determine why the query returned no results.

All Answers

k_bentsenk_bentsen

I suspect that your query is not returning any results when you run your test class. If you're familiar with using the development console, insert the line line as shown below.

 

Objectifs_commerciaux_annuels__c obj = [select Id from Objectifs_commerciaux_annuels__c where Nom_du_VRP__r.Id =: a.OwnerId and recordtype.name = 'Objectifs liés au compte' and debut_periode__c <=: a.createddate.date() and fin_periode__c >=: a.createddate.date()]; 

System.debug('The obj ID is: ' + obj.Id);
a.Objectifs_commerciaux_annuels__c = obj.id;

 

Then look at your debug log and look for this line. If you see "NULL" then look for the SOQL query that would be just above in the log and try to determine why the query returned no results.

This was selected as the best answer
nicodemnicodem

Hello k_bentsen,

 

Your suggestion make me think using try and catch method to avoid have an error message if my query returns nothing. So, I corrected my trigger like below.

 

Now, concerning my test class, I arrived to 83 % covered but not 100% because I didn't success to do an assertequals.

This line : System.assertEquals(obj.Id, accnt.Objectifs_commerciaux_annuels__c ); returns an error because it compares an Id with a null value (accnt.Objectifs_commerciaux_annuels__c is null)

 

Have you got an idea ? I thank you for your advices.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

trigger TR04Objectifcompte on Account (before update, before insert) 
{

    for (Account a : trigger.new)
    {
      
                          if ( a.Nouveau_client__c == true || a.Commercialement_inactif__c == false )
                          try
                          {
                           Objectifs_commerciaux_annuels__c obj = [select Id from Objectifs_commerciaux_annuels__c where Nom_du_VRP__r.Id =: a.OwnerId and recordtype.name = 'Objectifs liés au compte' and debut_periode__c <=: a.createddate.date() and fin_periode__c >=: a.createddate.date()]; 
                           System.debug('After the trigger execution, the obj ID is: ' + obj.Id);
                           a.Objectifs_commerciaux_annuels__c = obj.Id;
                           }
           
                          catch (Exception e)
                          {
                          }
   

    }

}

k_bentsenk_bentsen

It's because the localized instance of "accnt" does not get updated by the trigger. You need to recall the record that gets inserted into the database by DML with an SOQL query. Try adding and modifying as shown below:

 

accnt.Commercialement_inactif__c = false;
insert accnt ;

Account updatedAcct = [select Id, Objectifs_commerciaux_annuels__c from Account where Id = :accnt.Id];

 

// Test       

System.assertEquals(obj.Id, updatedAcct.Objectifs_commerciaux_annuels__c );

nicodemnicodem

Thanks you very much k_bentsen, you helped me well and now my trigger works and my test class is on 100% !