3

I have the following piece of code that I am trying to run as part of a unit test (test not shown here). I am creating an Account and then creating a Task associated to that Account via the WhatId

    1. String recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('General').getRecordTypeId();

    2. User someUser = [Select username from User where username like 'tristan%'][0];
    3. Account testAccount = new Account(
                                        RecordTypeId=recordTypeId,
                                        OwnerId=someUser.Id,                                            
                                        Name = 'Test Consulting',
                                        Line_of_Business__c = 'Sport'
                                        );
    4. insert testAccount;


    5. Task testTask = new Task(
                            WhatId= testAccount.Id,     
                            OwnerId=someUser.Id,
                            Subject='Call',
                            ActivityDate=System.today(),
                            Priority='Normal'
                            );

    6. insert testTask;

    7. System.debug(LoggingLevel.INFO, 'user debug: xxxxxxxxxxx # Tasks account: ' + testTask.AccountId);

The line above (Line 7) prints out a null, however when I query the task again and print the same field back like so (Line 10):

    8. Task someTask = [Select Id, AccountId FROM Task where id = :testTask.Id][0];
    9. String accId = someTask.AccountId;

    10. System.debug(LoggingLevel.INFO, 'user debug: xxxxxxxxxxx # Tasks account ID NOW: ' + accId);

I get the AccountId displayed.

Why does this happen? Why can't I just access the Task.AccountId before querying the Task again? The other Task fields like 'Status' etc. are accessible.

2 Answers 2

1

In task AccountId is filled from salesforce end

So when we insert the task

Task testTask = new Task(
                            WhatId= testAccount.Id,     
                            OwnerId=someUser.Id,
                            Subject='Call',
                            ActivityDate=System.today(),
                            Priority='Normal'
                            );

insert testTask;

testTask this will contain only those field which above mentioned and Id after DML operation.

Since task AccountId is filled from salesforce end. You need to query inorder to get this value

1
  • 1
    Thanks Ratan, I sort of suspected something like that, but it's nice to get confirmation :) Commented Jun 8, 2016 at 5:54
3

The ID field is available to the caller after doing an insert. All other derived fields, including createDate, createdById, ... , auto-number fields, formula fields, and, yes, Task.accountId, must be queried.

The specific documentation on Task.accountId is here in the Object reference; always a place to look first about any field. Because Task.accountId is not creatable, by definition, it is derived.

Represents the ID of the related Account. The AccountId is determined as follows.

If the value of WhatId is any of the following objects, then Salesforce uses that object’s AccountId.

Account
Opportunity
Contract
Custom object that is a child of Account

If the value of the WhatIdfield is any other object, and the value of the WhoId field is a Contact object, then Salesforce uses that contact’s AccountId. (If your organization uses Shared Activities, then Salesforce uses the AccountId of the primary contact.)

Otherwise, Salesforce sets the value of the AccountId field to null.

1
  • 1
    Thank you Cropredy, always a pleasure to read such a detailed reply! Much obliged! Commented Jun 8, 2016 at 7:06

You must log in to answer this question.

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