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
ChristineVWChristineVW 

Why does Task t.AccountID return a null value?

My trigger on a Task is failing to do anything because according to the debug log, t.AccountId is null.  The tasks that cause this trigger to fire do have Accounts associated to them.  I have no idea what to do next because otherwise the code shown here and everything in the rest of my Trigger seems to work fine. 

Tasks have a WhatId and an AccountId field, since on a Task there is a Related To dropdown.  If it were related to some other object, it would have a WhatId and, say, a ContactId.  I just noticed this but don't know how it relates to my problem.  How do I grab the ID of the Account that's related to any given task so that I can use it later?

Code:
trigger AssetUpdateServices on Task (before insert) {
for(Task t : trigger.new) {
if(t.Subject != 'Services Session') {
continue;
}
String acid = t.AccountId;
Asset[] assetList = [Select a.Total_Hours__c, a.Hours_Remaining__c, a.Status,
a.Name, a.Issuance_Date__c, a.AccountId From Asset a Where a.AccountId = :acid
Order By Issuance_Date__c];

... etc. etc.

 
a.schaefera.schaefer
actually, only the "WhoId" and the "WhatId" are mandatory fields, AccountID can be "Null" for tasks that are not related to Accounts.

HTH - Andreas

ChristineVWChristineVW

a.schaefer wrote:
actually, only the "WhoId" and the "WhatId" are mandatory fields, AccountID can be "Null" for tasks that are not related to Accounts.




The trigger that is firing will fire on Tasks that DO have an AccountId, so I need to know why it comes back as null.  Any tasks in question are indeed related to an Account.  Anyone have any idea?
TehNrdTehNrd
Add the two lines to your code, watach the debug log and report back.


Code:
trigger AssetUpdateServices on Task (before insert) {
for(Task t : trigger.new) {

//------------------------------------------------
system.debug('Account Id ' + t.accountId);
system.debug('What Id ' + t.whatId);
//------------------------------------------------

if(t.Subject != 'Services Session') {
continue;
}
String acid = t.AccountId;
Asset[] assetList = [Select a.Total_Hours__c, a.Hours_Remaining__c, a.Status,
a.Name, a.Issuance_Date__c, a.AccountId From Asset a Where a.AccountId = :acid
Order By Issuance_Date__c];


 



Message Edited by TehNrd on 05-19-2008 12:24 PM