function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gailhgailh 

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) {
   
    // 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' limit 1];
            t.RecordType.Id = rt.Id;
        } // end if task needs update
    } // end loop through tasks
}

 

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.

Best Answer chosen by Admin (Salesforce Developers) 
gailhgailh

Ack, figured it out - it's t.RecordTypeId NOT t.RecordType.Id!!!

All Answers

JBabuJBabu

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..

gailhgailh

Oh, sorry - I changed this line:

 

  t.RecordType.Id = rt.Id;

 

to 

 

if(rt.Id != null){

t.RecordType.Id = rt.Id;

}

JBabuJBabu

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.

gailhgailh

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). 

JBabuJBabu

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)..

gailhgailh

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:

1
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:

24.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
10:52:06.108 (108094000)|EXECUTION_STARTED
10:52:06.108 (108137000)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
10:52:06.108 (108169000)|CODE_UNIT_STARTED|[EXTERNAL]|01qc0000000Cb49|Task_BeforeInsert on Task trigger event BeforeInsert for [new]
10:52:06.109 (109401000)|SYSTEM_METHOD_ENTRY|[14]|LIST.iterator()
10:52:06.110 (110514000)|SYSTEM_METHOD_EXIT|[14]|LIST.iterator()
10:52:06.110 (110815000)|SYSTEM_METHOD_ENTRY|[14]|system.ListIterator.hasNext()
10:52:06.110 (110850000)|SYSTEM_METHOD_EXIT|[14]|system.ListIterator.hasNext()
10:52:06.110 (110875000)|SYSTEM_METHOD_ENTRY|[14]|system.ListIterator.next()
10:52:06.110 (110897000)|SYSTEM_METHOD_EXIT|[14]|system.ListIterator.next()
10:52:06.111 (111341000)|SOQL_EXECUTE_BEGIN|[17]|Aggregations:0|select Id from RecordType where (Name = 'Email' and SobjectType = 'Task')
10:52:06.117 (117177000)|SOQL_EXECUTE_END|[17]|Rows:1
10:52:06.117 (117559000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Trigger.Task_BeforeInsert: line 19, column 1
10:52:06.117 (117579000)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Trigger.Task_BeforeInsert: line 19, column 1
10:52:06.454 (117624000)|CUMULATIVE_LIMIT_USAGE
10:52:06.454|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 2 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

10:52:06.454|CUMULATIVE_LIMIT_USAGE_END

10:52:06.117 (117702000)|CODE_UNIT_FINISHED|Task_BeforeInsert on Task trigger event BeforeInsert for [new]
gailhgailh

Ack, figured it out - it's t.RecordTypeId NOT t.RecordType.Id!!!

This was selected as the best answer