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
MicrobeMicrobe 

Correct way to associate task with opportunity?

I want to create a task within Apex, and want it to be associated with an opportunity. I'm using code as follows (assuming o is a reference to a valid Opportunity):

 

 

Task t = new Task(    
  Subject = 'Subject of task',
  WhatId = o.Id,
  Description = 'This is the description text',
  Status = 'Not Started');

insert t;

This works fine, the task is created. However, if I create an After Insert trigger on the task, the task's What.Type property is null, instead of "Opportunity". The WhatId property correctly references the opportunity's ID.

 

Am I missing a step?

 

 

kprkpr

If you query for it after the database commit, you can see that it is set to the correct type.

Imran MohammedImran Mohammed

If Whatid is assigned then the type will be automatically assigned to type.

USe system.log for the same task and query the what.type field to check if it printing null even there.

MicrobeMicrobe

OK, here's the code as I modified it:

 

 

Task t = new Task(		
  Subject = 'Here is the subject',
  WhatId = o.Id,
  Description = 'This is the description text',
  Status = 'Not Started');

System.debug(Logginglevel.DEBUG, '******* Inserting Task');
			
insert t;

System.debug(Logginglevel.DEBUG, '******* t.What.Type= ' + t.What.Type);

t = [SELECT Id, What.Type FROM Task WHERE Id = :t.Id];
		
System.debug(Logginglevel.DEBUG, '******* After Select t.What.Type= ' + t.What.Type);

 

The debug result is:

 

******* Inserting Task

******* t.What.Type= null

******* After Select t.What.Type= Opportunity

 

So it appears that the What.Type is set after the database insert, which is understandable.

 

What is not understandable is that the trigger that this code is supposed to be testing is not seeing the What.Type. Let's say I set up a trigger on Task:

 

 

trigger Task_TestTrigger on Task (after insert) {

for (Task tsk : trigger.new)
{
System.debug(Logginglevel.DEBUG, '!!!!!!!!! What.Type=' + tsk.What.Type);
System.debug(Logginglevel.DEBUG, '!!!!!!!!! Subject=' + tsk.Subject);
System.debug(Logginglevel.DEBUG, '!!!!!!!!! WhatId=' + tsk.WhatId);
}
}

 

This is an "after insert" - which to me, says that it should execute the task has been inserted, and the What.Type should be assigned. However, what's coming out in the debug log is this:

 

!!!!!!!!! What.Type=null

!!!!!!!!! Subject=Here is the subject

!!!!!!!!! WhatId=006Q0000006L...

 

The WhatId is correct, and it's pointing to an "006" sObject, which is an Opportunity. But for some reason, inside the trigger, it has not used this information to assign the "What.Type" property yet.

 

Should this not have already happened at the point that an After Insert trigger occurs?

 

MicrobeMicrobe

OK, I answered my own question. It appears that the tasks handed into the trigger via trigger.new have not yet had the What.Type property populated. Changing my trigger to the following solved the problem:

 

 

trigger Task_TestTrigger on Task (after insert) {

List<Task> taskList = [SELECT Id, Subject, What.Type FROM Task WHERE Id IN :trigger.new];

for (Task tsk : taskList)
{
System.debug(Logginglevel.DEBUG, '!!!!!!!!! What.Type=' + tsk.What.Type);
System.debug(Logginglevel.DEBUG, '!!!!!!!!! Subject=' + tsk.Subject);
System.debug(Logginglevel.DEBUG, '!!!!!!!!! WhatId=' + tsk.WhatId);
}
}

 

 

This way, a list is populated from a select based upon the contents of trigger.new, and THIS list has the WhatId populated correctly.