• PeteyBird
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 4
    Replies

Hello all,

 

My first foray into writing a trigger (which I think is what I need to do based on the details below).  I've found a ton of info online which I think has gotten me close, but I'm at a roadblock.


What I'm trying to do is increment a field Number_of_Injuries on a custom object Course by 1 every time a Case is saved where Case.Incident_Type (a custom field on case) is "Injury."

 

The code below returns a result: Error:Apex trigger updateInjuryCount caused an unexpected exception, contact your administrator: updateInjuryCount: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateInjuryCount:

 

I would truly appreciate any guidance experts may have on this.

 

trigger updateInjuryCount on Case (after update, after insert)
{
    
    Integer i = 0;
    
    Set<ID> courseIDs = new Set<ID>(); //variable to store the course id that is used in the case lookup field
    
    Map<ID, integer> id2Injury = new Map<ID, integer>();
    
    for (Case o : Trigger.new) //define case as variable o, when the trigger is first fired
    {
        if ((Trigger.isInsert && o.Incident_Type__c == 'Injury') ||
            Trigger.old[i].Incident_Type__c != o.Incident_Type__c)
        {
            courseIDs.add(o.Course__c); //get the course id from the lookup field on case object
            
            id2Injury.put(o.Course__c, 1); //creates array with course id and integer to increment
        }     
        i++;    
    }
    
    List<Course__c> courses = [select id, Number_of_Injuries__c from Course__c where id in :courseIDs]; //finds the relevant course record
    
    for (Course__c c : courses)
    {
        c.Number_of_Injuries__c++;
    }
    
    update courses;
}

Hello all,

 

My first foray into writing a trigger (which I think is what I need to do based on the details below).  I've found a ton of info online which I think has gotten me close, but I'm at a roadblock.


What I'm trying to do is increment a field Number_of_Injuries on a custom object Course by 1 every time a Case is saved where Case.Incident_Type (a custom field on case) is "Injury."

 

The code below returns a result: Error:Apex trigger updateInjuryCount caused an unexpected exception, contact your administrator: updateInjuryCount: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateInjuryCount:

 

I would truly appreciate any guidance experts may have on this.

 

trigger updateInjuryCount on Case (after update, after insert)
{
    
    Integer i = 0;
    
    Set<ID> courseIDs = new Set<ID>(); //variable to store the course id that is used in the case lookup field
    
    Map<ID, integer> id2Injury = new Map<ID, integer>();
    
    for (Case o : Trigger.new) //define case as variable o, when the trigger is first fired
    {
        if ((Trigger.isInsert && o.Incident_Type__c == 'Injury') ||
            Trigger.old[i].Incident_Type__c != o.Incident_Type__c)
        {
            courseIDs.add(o.Course__c); //get the course id from the lookup field on case object
            
            id2Injury.put(o.Course__c, 1); //creates array with course id and integer to increment
        }     
        i++;    
    }
    
    List<Course__c> courses = [select id, Number_of_Injuries__c from Course__c where id in :courseIDs]; //finds the relevant course record
    
    for (Course__c c : courses)
    {
        c.Number_of_Injuries__c++;
    }
    
    update courses;
}