You need to sign in to do that
Don't have an account?
QD93
Null Exception: Attempt to De-reference a Null Object
Hi,
I am getting a Null Exception, on line 12-18. The idea here is that if that I am trying to only have my Task's related to ID field = Opportunity. So when I relate the Task to an Account the trigger shouldn't do anything. However, I am getting a null exception in my loop b/c the loop is returning null values.
I am getting a Null Exception, on line 12-18. The idea here is that if that I am trying to only have my Task's related to ID field = Opportunity. So when I relate the Task to an Account the trigger shouldn't do anything. However, I am getting a null exception in my loop b/c the loop is returning null values.
public class LFGEmailMessageClass { public static void InsertTask (list<Task> NewTasks) { List<Task> tasks = new List<Task>(); List<Task> Updatetasks = new List<Task>(); Set<Id> opportunityIds = new Set<Id>(); Set<Id> taskIds = new Set<Id>(); String Y = '006'; for (Task task: newtasks){ if (Task != Null && !((string)task.WhatId).startsWith(Y)){ opportunityids = Null; } Else if (Task != Null && ((string)task.WhatId).startsWith(Y)){ opportunityIds.add(task.WhatId); taskIds.add(task.Id); }} Map<Id, Id> opportunities = new Map<Id, Id> (); if(opportunityids != Null){ for (Opportunity opportunity: [SELECT Id, Name, accountid, company__r.Id FROM Opportunity WHERE Id IN :opportunityIds AND accountid = null]) { opportunities.put(opportunity.id, opportunity.company__r.Id); } } Map<Id, Id> TasksMap = new Map <Id, Id> (); For (Task taskss: [SELECT Id, WhatId FROM Task WHERE Id IN :taskIds]){ TasksMap.put(taskss.Id,Taskss.WhatId);} for (Task task: newTasks) { if (task.TaskSubType == 'Email' && task.Status == 'Completed' && task.WhoId != Null && task.WhatId != Null && ((string)task.WhatId).startsWith(Y)) { Task updateTask = new Task(); updateTask.ID = task.Id; updateTask.WhatId = task.WhatId; tasks.add(updateTask); } Else IF (task.TaskSubType == 'Email' && task.Status == 'Completed' && task.WhoId != Null && task.WhatId != Null && !((string)task.WhatId).startsWith(Y)) { Task updateTask = new Task(); updateTask.ID = task.Id; updateTask.WhatId = task.WhatId; UpdateTasks.add(updateTask); } } {if(tasks.size()>0){ update tasks; } Else IF (updateTasks.size()>0){ update updateTasks;}} System.debug( Tasks); }
Replace your code 12-22 with following code:
I hope you will not get the exception after that.
Mark solved if it does help you.
Unfortunately, I was hit with this error message -->
Compile Error: Method does not exist or incorrect signature: void startsWith(String) from the type Id
Now I am getting the error message: Compile Error: Method does not exist or incorrect signature: void add(Schema.SObjectField) from the type Set<Id>
for the line --> opportunity.ids.add(task.whatid);
Please copy paste below code and test if it works fine:
Mark solved if does help you.