0

I created a trigger that update a custom field when the status of a lead change. However, I get the following error when I run the test class :

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ModifiedStatus_PhotoLead: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.ModifiedStatus_PhotoLead: line 13, column 1: [] Stack Trace: Class.ModifiedStatusLeadTest.ModifiedStatusLeadTest: line 9, column 1

Here is the trigger :

trigger ModifiedStatus_PhotoLead on Lead (before update) {

List<Lead> lead = Trigger.new;
List<User> myUsers = [SELECT Id, PhotoLead_Points__c FROM User WHERE Id=:userinfo.getuserId()];
List<User> updatedUsers = new List<User>();

for (Lead l : lead)
{
    if (l.Status == 'Contacted')
    {
        for (User u : myUsers)
        {
            u.PhotoLead_Points__c += 10;
            updatedUsers.add(u);
        }
    }
    else if (l.Status == 'Future Follow Up')
    {
        for (User u : myUsers)
        {
            u.PhotoLead_Points__c += 25;
            updatedUsers.add(u);
        }
    }
    else if (l.Status == 'Qualified')
    {    
        for (User u : myUsers)
        {
            u.PhotoLead_Points__c += 100;
            updatedUsers.add(u);
        }

        List<ContentDocument> myFeed = [SELECT LatestPublishedVersionId FROM ContentDocument WHERE Id In (SELECT ContentDocumentId FROM ContentVersion WHERE Id =:l.PhotoLead_Image__c)];
        delete myFeed;
    }
}
update updatedUsers;
}

And here's the test class :

@isTest 
public class ModifiedStatusLeadTest { 
static testMethod void ModifiedStatusLeadTest() { 
    Lead newLead = new Lead() ; 
    newLead.FirstName = 'Cole'; 
    newLead.LastName = 'Swain'; 
    newLead.Company = 'BlueWave'; 
    newLead.Status = 'contacted'; 
    insert newLead; 
    update newLead;

    User userToCreate = new User();
    userToCreate.FirstName = 'Titi';
    userToCreate.LastName = 'Toto';
    userToCreate.Email = '[email protected]';
    userToCreate.Username = '[email protected]';
    userToCreate.Alias = 'huhu';
    userToCreate.ProfileId = '01er0010000iTqy';
    userToCreate.PhotoLead_Points__c = 50;
    insert userToCreate;

    userToCreate =[select IsActive from User  limit 1];
    userToCreate.IsActive = false;

    newLead.Status = 'Future Follow Up';
    update newLead;
    userToCreate.PhotoLead_Points__c = 100;
    update userToCreate;

    Decimal points = [SELECT Id, PhotoLead_Points__c FROM User WHERE Id=:userinfo.getuserId()][0].PhotoLead_Points__c;
    newLead.Status = 'Qualified';
    update newLead;
    userToCreate.PhotoLead_Points__c = 200;
    update userToCreate;

    try { 
        Delete newLead; 
    } 
    catch(Exception ee) {}
} 
}

Any suggestions on what could be causing this error ? Thanks !

1 Answer 1

3

In this line,

u.PhotoLead_Points__c += 10;

if the initial value PhotoLead_Points__c is null, it will throw this exception.

you can either set the default value of the field to 0 on the field setup or add a null check in your code and default it to the value you are adding before adding subsequent values.

u.PhotoLead_Points__c = (u.PhotoLead_Points__c == null) ? 10 : u.PhotoLead_Points__c + 10;

same applies to all the 3 places where you are adding different value to this field.

Also, couple of points

  1. there are SOQL and DML in your for loop which is not recommended. try to optimise it and move them out of the loop.

  2. try to consolidate the code and reduce the number of loops and repeated code.

1
  • Thanks, it works fine ! I will work on my code to reduce the loops. Feb 2, 2017 at 12:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .