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
Ken sfdc1Ken sfdc1 

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

Can anyone help me in changing this query.

for(Veeva_Service_Request__c loopVSR : [SELECT Id, Service_Request_Id__c 
                                                FROM Veeva_Service_Request__c
                                                WHERE Id IN :taskParentMap.values()
                                                    OR Service_Request_Id__c IN :taskParentMap.values()]){
 
lakslaks
Hi Ken,

Has the map taskParentMap been initialized ? 
Looks like it hasn't been, hence invoking .values() on it might be giving you the Null PointerException.

Regards,
Lakshmi.
Ken sfdc1Ken sfdc1
    public void ReAssignTask(List<Task> taskList){
        //Initialize variables
        String tempContainer;
        Set<Id> taskOwners = new Set<Id>();
        Map<Id,Id> taskOwnerMap = new Map<Id,Id>();
        Map<Id,Id> taskParentMap = new Map<Id,Id>();
        Map<Id,String> userProfileMap = new Map<Id,String>();
        Map<Id,Id> vsrToSR = new Map<Id,Id>();
        Map<Id,Id> srToVSR = new Map<Id,Id>();
        
        //Get owners of the tasks
        for(Task loopTask : taskList){
            taskOwnerMap.put(loopTask.Id, loopTask.OwnerId);
            taskParentMap.put(loopTask.Id, loopTask.WhatId);
        }
        
        //Group tasks depending on the profile of the owners of the tasks
        for(User loopUser : [SELECT id, profile.name FROM User WHERE Id IN :taskOwnerMap.values()]){
            userProfileMap.put(loopUser.Id, loopUser.profile.name);
        }
        
        for(Veeva_Service_Request__c loopVSR : [SELECT Id, Service_Request_Id__c 
                                                FROM Veeva_Service_Request__c
                                                WHERE Id IN :taskParentMap.values()
                                                    OR Service_Request_Id__c IN :taskParentMap.values()]){
            //Get Service Request for Veeva Service Request
            srToVSR.put(loopVSR.Service_Request_Id__c, loopVSR.Id);
            //Get Veeva Service Request for Service Requests
            vsrToSR.put(loopVSR.Id, loopVSR.Service_Request_Id__c);
        }
        
        //Re-assign service request tasks to its veeva service request counterpart
        for(Task loopTask : taskList){
            if(loopTask.WhatId.getSobjectType().getDescribe().getName() != 'Case' && 
               loopTask.WhatId.getSobjectType().getDescribe().getName() != 'Veeva_Service_Request__c' &&
               (userProfileMap.get(loopTask.OwnerId).contains('NAM') || 
                userProfileMap.get(loopTask.OwnerId).contains('LSM'))){
                loopTask.addError('A task cannot be assigned to LSM/NAM users if it is not related to a Case.');
            }
            if((userProfileMap.get(taskOwnerMap.get(loopTask.Id)).toLowerCase()).contains('nam')){
                //Assign task from service request to veeva service request
                if(srToVSR.containsKey(loopTask.whatId)){
                    loopTask.Service_Request_LookUp__c = loopTask.whatId;
                    loopTask.whatId = srToVSR.get(loopTask.whatId);
                }
            }else if((userProfileMap.get(taskOwnerMap.get(loopTask.Id)).toLowerCase()).contains('lsm')){
                //Assign task from service request to veeva service request
                if(srToVSR.containsKey(loopTask.whatId)){
                    loopTask.Service_Request_LookUp__c = loopTask.whatId;
                    loopTask.whatId = srToVSR.get(loopTask.whatId);
                }
            }else{
                //Assign task from veeva service request to service request
                if(vsrToSR.containsKey(loopTask.whatId)){
                    loopTask.Veeva_Service_Request_LookUp__c = loopTask.whatId;
                    loopTask.whatId = vsrToSR.get(loopTask.whatId);
                }
            }
        }
    }
}

Please check it has been initialized.
Navee RahulNavee Rahul
The error mention that your taskParentMap.values() dosent have any values in it,and you were trying to refer it in sql conditions.

Thanks
D Naveen rahul.
Ken sfdc1Ken sfdc1
Then I have to use 
 taskParentMap.values().not empty something like this?
Ken sfdc1Ken sfdc1
trigger Task_Trigger on Task (before insert) {
    Task_TriggerHandler handler = new Task_TriggerHandler();
    //Runs on before insert only
    if(Trigger.isBefore && Trigger.isInsert){
        //Re-assigns tasks
        handler.ReAssignTask(Trigger.new);
    }
}

Here is the trigger calling the class.
Navee RahulNavee Rahul
yer you need to be sure that the values are available before you refer it.

Thanks
D Naveen rahul.
lakslaks
It will be a good idea to check the same for taskParentMap and taskOwnerMap as well, since that is also used in an SOQL query.

Regards,
Lakshmi.