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
VRKVRK 

System.NullPointerException: Attempt to de-reference a null object issue in Trigger

Hi all,
my scenario is : When record created in custom object (IVR_object) then Task created under Contact object .
i written below code, but when i try to deploy the code getting below errror 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger createTask caused an unexpected exception, contact your administrator: createTask: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.createTask: line 5, column 1

even i  remove line 5 also getting same error , can some one please check below code and pls let me know how to fix this

trigger createTask on IVR_Journey__c (after insert) {
    List<Task> taskToInsertList = new List<Task>();
    List<Contact> contactToUpdateList = new List<Contact>();
   id idRecType = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('IVR_Task_Record').getRecordTypeId();
       for (IVR_Journey__c ivj : trigger.new) {
        Task task = new Task();
        task.Subject = 'IVR Self Service';
        task.WhoId = ivj.Ivr_Journey__c;
        task.Status = 'Completed';
        task.ActivityDate = date.Today();
        task.Authentication_Result__c = ivj.Authentication_Result__c;
        task.Caller_ID__c = ivj.Caller_ID__c;
        task.Contact_Channel_Duration__c = ivj.Contact_Channel_Duration__c;
        task.Contact_Intent__c = ivj.Contact_Intent__c;
        task.Automated_Task_Completed__c = ivj.Automated_Task_Completed__c;
        task.Transfered_To__c = ivj.Transfered_To__c;
        task.Survey_Option__c = ivj.Survey_Option__c;
        task.DNIS__c = ivj.DNIS__c;
        task.Start_Date_Time__c = ivj.Start_Date_Time__c;
        task.End_Date_Time__c = ivj.End_Date_Time__c;
        task.Language_Preference__c = ivj.Language_Preference__c;
        Contact contact = new Contact (Id = ivj.Ivr_Journey__c);
        Task.RecordTypeId = idRecType;
        taskToInsertList.add(task);
        contactToUpdateList.add(contact);
        
    }
    
    if(taskToInsertList.size() > 0)
        insert taskToInsertList;
        
    if(contactToUpdateList.size() > 0)
        update contactToUpdateList;
  
}
please check and let me know why this error coming ............Thanks in advance
Best Answer chosen by VRK
Prashant Pandey07Prashant Pandey07
I see..your record type is on Task but  in code you are getting from Contact..

You need to chage this to this..
 
id idRecType = Schema.SObjectType.Task.getRecordTypeInfosByName().get('IVR Task Record').getRecordTypeId();

let me know..

--
Thanks,
Prashant

 

All Answers

Prashant Pandey07Prashant Pandey07
Hi

You are not giving the right record type label..

I am assuming your record type Name is 
 
id idRecType = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('IVR Task Record').getRecordTypeId();

--
Thanks,
Prashant​
VRKVRK
Hi Prashant,
thanks for response.....
i tried...but no luck, getting same error ...do  you think is there any other reasons for this error ?  Thanks
Nikhil Deshmukh 11Nikhil Deshmukh 11
Hello Sekhar,
have you made record type on contact object?
Because 'idRecType ' not getting an ID on contact object have a name 'IVR_Task_Record'
Please check
Thanks 
Nikhil
Prashant Pandey07Prashant Pandey07
Hi Sekhar,

Send me your record type detail page..then I ca better help you..

There is higer probability that you are not checking the right record type name.

--
Thanks,
Prashant
VRKVRK
Hi Prashant,
thanks for your responsne, please find RecordType details.
Requirement is , when ever record created under 'IVR_Journey__c ' , task created under Contact---> Activity history.


User-added image
Prashant Pandey07Prashant Pandey07
I see..your record type is on Task but  in code you are getting from Contact..

You need to chage this to this..
 
id idRecType = Schema.SObjectType.Task.getRecordTypeInfosByName().get('IVR Task Record').getRecordTypeId();

let me know..

--
Thanks,
Prashant

 
This was selected as the best answer
VRKVRK
Thanks Prashant....just now i also reliazed the mistake.
one more help on this ...could you please let me know if you have any idea on this .

As of now, when record created in custom object(IVR_journey_c) ,  when ever record created under 'IVR_Journey__c ' , tasks created under Contact--->custom IVR Journey   BUT not under Activity History.

my requirement is , when ever record created under 'IVR_Journey__c ' , tasks created under Contact--->Activity History .

i already maintained lookup relation ship between IVR_journy_C and Task Activities.  But the above code si not working
do you have any idea on this ....?
 
Prashant Pandey07Prashant Pandey07
Activity History will show the history of the Task/Eevnt

If you close the task that will show in the ActivityHistory...

--
Thanks,
Prashant
VRKVRK
Thanks prasanth....after check the lookup relation ship between custom object and Activities  and also correction in code suggested by you, will help fixed this issue....thank you very much for your help appreciated