1

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?

  • As the error says, the AccountId field is not writeable on the Task object. If you want to know how it is calculated you can refer to this documentation. – Martin Lezer Aug 14 '17 at 12:58
4

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();

   }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.