You need to sign in to do that
Don't have an account?
Update Task Trigger - Validation Errors
Hi all, I'm very new to Apex and this is my first trigger. Thought it was quite simple but I'm getting the following error:
Validation Errors While Saving Record(s) |
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger Task_AfterInsert caused an unexpected exception, contact your administrator: Task_AfterInsert: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Task_AfterInsert: line 18, column 1".
The code is: trigger Task_AfterInsert on Task (after insert) {
I'm assuming that no record type is being found but checked and there is a task record type called email. I tried to only run t.RecordType.Id = rt.Id if rt.Id was not null - still the same error.
Any assistance would be greatly appreciated. |
Ack, figured it out - it's t.RecordTypeId NOT t.RecordType.Id!!!
All Answers
Can you share the code where you have mentioned record type !=null as I am not able to see in this code.
This issue comes when you try to access the null value..
Oh, sorry - I changed this line:
t.RecordType.Id = rt.Id;
to
if(rt.Id != null){
t.RecordType.Id = rt.Id;
}
Please share the complete code as I am seeing that issue is in line number 18.
We can identify it, if you share the complete code.
sorry, think I'm missing something - complete code is at the top of this thread. Line 18 is the t.RecordType.Id = rt.Id line. The additional code I added was a test to see if only running line 18 if rt.Id was not null would work. It had the same result (except that error was at line 19 as that line of code moved down one line).
There is some disconnect here..
If you have mentioned rt.id!=null and then if its value is null it will not go to next line. There is no chance you getting the error for attempt to dereference a null object in the next line of (rt.id!=null)..
First off, thanks for the time you've already spent on this post. Most appreciated. If I understand correctly, you're saying that the updates to the code that I made (i.e. only running the code if rt.Id is not null) should have fixed the issue of dereferencing a null object. I made the same fix to the code and here it is in its entirety. It's still getting the same error and the debug log is below:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Trigger: Task BeforeInsert
Action: before insert
Description: This trigger will change record type to "Email" if type is "Email".
This ensures that the Email page layout is used and Type and Comments cannot be edited.
Created: Apr 13, 2012 by Gail H
Change Log:
*****************************************************************/
trigger Task_BeforeInsert on Task (before insert) {
// find new tasks
for (Task t : trigger.new) {
if (t.type=='Email') {
// update record type
RecordType rt = [select Id from RecordType where Name = 'Email' and SobjectType = 'Task'];
if (rt.Id != null) {
t.RecordType.Id = rt.Id;
}
} // end if task needs update
} // end loop through tasks
}
And here are the debug logs:
Ack, figured it out - it's t.RecordTypeId NOT t.RecordType.Id!!!