0

Can anyone help me in changing this query.

 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);
            }
        }
    }
}

}

5
  • 3
    If taskParentMap is null then you would get that error.
    – BarCotter
    Nov 11, 2014 at 14:58
  • CAn you help me to modify the code.
    – ken_sfdc
    Nov 11, 2014 at 15:19
  • You have not provided enough code to see what the issue is.
    – BarCotter
    Nov 11, 2014 at 15:25
  • It says too longs can I have your email to send my code.Ken 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); } }This is the trigger calling the class but I cant past the class as it is long.
    – ken_sfdc
    Nov 11, 2014 at 15:57
  • 1
    @ken_sfdc please click the "edit" link below your question and from there you can add the additional code so that we can assist you further.
    – Mark Pond
    Nov 11, 2014 at 16:07

2 Answers 2

1

You need to put in system.debugs and check to the logs to see where the null value is coming in. BarCotter was right when he said taskParentMap, but there are also other ones .

if((userProfileMap.get(taskOwnerMap.get(loopTask.Id)).toLowerCase()).contains('nam'))  

if((userProfileMap.get(taskOwnerMap.get(loopTask.Id)).toLowerCase()).contains('lsm'))

loopTask.Veeva_Service_Request_LookUp__c = loopTask.whatId;
            loopTask.whatId = vsrToSR.get(loopTask.whatId);

Pretty much wherever there is a .get can cause that..

Are you sure there is something wrong with your query? Put your query into force.com explorer or developer console and see if it returns anything.

4
  • see the debuglogs Yes I think query is wrong 10:29:34.183 (183364602)|SOQL_EXECUTE_BEGIN|[37]|Aggregations:0|select Id, Service_Request_Id__c from Veeva_Service_Request__c 10:29:34.204 (204071837)|SOQL_EXECUTE_END|[37]|Rows:0
    – ken_sfdc
    Nov 11, 2014 at 18:34
  • So then it goes back to what @BarCotter said.. taskParentMap could be null
    – EricSSH
    Nov 11, 2014 at 19:23
  • So how to change the query to only execute when it is not null.
    – ken_sfdc
    Nov 11, 2014 at 19:49
  • First get the value from UserProfileMap in an object. String userProfile = userProfileMap.get(taskOwnerMap.get(loopTask.Id)); Then check if userProfile is not null. if(userProfile != null){ userProfile = userProfile.toLowerCase(); if(userProfile.contains(...);} etc.
    – Aayush K
    Dec 22, 2014 at 17:11
0

Try to check the size >0 and then assign if(loopVSR.size() >0){}

2
  • Where can I add this if(loopVSR.size() >0){} which lineplease?
    – ken_sfdc
    Nov 11, 2014 at 17:47
  • Hi Ken, In each for loop you need to check for the Null values.
    – Tamil
    Nov 12, 2014 at 19:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .