You need to sign in to do that
Don't have an account?
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.
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
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 .....