[SalesForce] Field not writable: Task.AccountId

I'm using the following test method:

static testMethod void testTask() {

Test.startTest();

Account acc = new Account(
        OwnerId = u.Id,
        Name = 'Dreamweaver Co.',
        Status__c = 'Terminated',
        Reason_for_Termination__c = 'Compliance violation',      
        Type__c = 'Merchant',
        Registered_Address__c = 'Fifth Avenue',
        Registration_Number__c = '23132424342423',
        BillingCountry = 'USA',
        Parent_company_legal_name__c = 'Dreamweaver Ltd.',
        Parent_company_registration_number__c = '3213123211',
        Parent_company_Registered_Address__c = 'Fifth Avenue',
        Parent_company_Post_Code_and_City__c = 'Los Angeles CA 61700',
        Type_of_processing__c = 'CNP'
        );

        insert acc;

Task testTask = new Task(
            subject = 'Test Task',
            priority = 'In Progress',
            OwnerId = u.id,
            AccountId = acc.id
        );

        insert testTask;

Test.stopTest();

}

But in

Task testTask = new Task(
subject = 'Test Task',
priority = 'In Progress',
OwnerId = u.id,
AccountId = acc.Id
);

I get

Field is not writeable: Task.AccountId

Could you please advise how to avoid "Field is not writeable: Task.AccountId"
and
use the acc in the testTask?

Best Answer

In Task for Account Field we Need to use WhatId

  static testMethod void testTask() {

    Test.startTest();

     Account acc = new Account(
    OwnerId = u.Id,
    Name = 'Dreamweaver Co.',
    Status__c = 'Terminated',
    Reason_for_Termination__c = 'Compliance violation',      
    Type__c = 'Merchant',
    Registered_Address__c = 'Fifth Avenue',
    Registration_Number__c = '23132424342423',
    BillingCountry = 'USA',
    Parent_company_legal_name__c = 'Dreamweaver Ltd.',
    Parent_company_registration_number__c = '3213123211',
    Parent_company_Registered_Address__c = 'Fifth Avenue',
    Parent_company_Post_Code_and_City__c = 'Los Angeles CA 61700',
    Type_of_processing__c = 'CNP'
    );

    insert acc;

       Task testTask = new Task(
        subject = 'Test Task',
        priority = 'In Progress',
        OwnerId = u.id,
        WhatId = acc.id
    );

    insert testTask;

   Test.stopTest();

   }
Related Topic