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 !