+ Start a Discussion
djensendjensen 

apex attempt to de-reference a null object

Hello, 
I'm hoping someone can help me with this trigger. Here's what it's supposed to do. If notes are added or updated to a text field called "Call Notes" on an opportunity, create a task. This trigger should run when an opp is created or updated.

If I remove "after insert", it seems to work just fine. However, when I add "after insert" I get the error "apex attempt to de-reference a null object: oppNotes".

trigger oppNotes on Opportunity (after insert, after update) {
     List<Task> taskList = new List<Task>();
      for (Opportunity opp : Trigger.new){
        Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
         if (oldOpp.Call_Notes__c != opp.Call_Notes__c ) {
          Task t = new Task();
           t.subject = 'Call Notes';
            t.WhatId = opp.ID;
             t.Status = 'Completed';
            t.Priority = 'Normal';
           t.Type = 'Other';
          t.Description = opp.Call_Notes__c;
         taskList.add(t);
        }
       }
      insert taskList;
     }

Any help would be greatly appreciated. 

Best Answer chosen by djensen
Vishnu VaishnavVishnu Vaishnav
Hi ,
Here is modified code , you can try this one :

trigger oppNotes on Opportunity (after insert, after update) {
    List<Task> taskList = new List<Task>();
    if( trigger.isInsert){
        for (Opportunity opp : Trigger.new){
            if ( opp.Call_Notes__c != null && opp.Call_Notes__c !='' ) {
                Task t = new Task();
                t.subject = 'Call Notes';
                t.WhatId = opp.ID;
                t.Status = 'Completed';
                t.Priority = 'Normal';
                t.Type = 'Other';
                t.Description = opp.Call_Notes__c;
                taskList.add(t);
            }
        }
    }
    else if( trigger.isUpdate){
        for (Opportunity opp : Trigger.new){
            Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
            if (oldOpp.Call_Notes__c != opp.Call_Notes__c ) {
                Task t = new Task();
                t.subject = 'Call Notes';
                t.WhatId = opp.ID;
                t.Status = 'Completed';
                t.Priority = 'Normal';
                t.Type = 'Other';
                t.Description = opp.Call_Notes__c;
                taskList.add(t);
            }
        }
    }    
    if( tasklist.size() > 0 )    {
        insert taskList;
    }
}

::
Qusetion Solved ? then mark as best answer to make helpful to others .....

All Answers

Vishnu VaishnavVishnu Vaishnav
Hi ,
Here is modified code , you can try this one :

trigger oppNotes on Opportunity (after insert, after update) {
    List<Task> taskList = new List<Task>();
    if( trigger.isInsert){
        for (Opportunity opp : Trigger.new){
            if ( opp.Call_Notes__c != null && opp.Call_Notes__c !='' ) {
                Task t = new Task();
                t.subject = 'Call Notes';
                t.WhatId = opp.ID;
                t.Status = 'Completed';
                t.Priority = 'Normal';
                t.Type = 'Other';
                t.Description = opp.Call_Notes__c;
                taskList.add(t);
            }
        }
    }
    else if( trigger.isUpdate){
        for (Opportunity opp : Trigger.new){
            Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
            if (oldOpp.Call_Notes__c != opp.Call_Notes__c ) {
                Task t = new Task();
                t.subject = 'Call Notes';
                t.WhatId = opp.ID;
                t.Status = 'Completed';
                t.Priority = 'Normal';
                t.Type = 'Other';
                t.Description = opp.Call_Notes__c;
                taskList.add(t);
            }
        }
    }    
    if( tasklist.size() > 0 )    {
        insert taskList;
    }
}

::
Qusetion Solved ? then mark as best answer to make helpful to others .....
This was selected as the best answer
djensendjensen
That worked perfectly. Thanks so very much! I really apprecaite the help. Again...thanks a ton.