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
Siddharth LakhotiaSiddharth Lakhotia 

Field is not writeable: Task.Owner

Hi,

I am getting following error  while writting Test class

User-added image
Deepak Pandey 13Deepak Pandey 13
Hi Sidharth,
     Firstly you just insert user with a profile and use runasuser.

 Profile p = [SELECT Id FROM Profile WHERE Name='ProfileName']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standar123456duser@testorg.com');

            System.runAs(u) 
            {
                Task t = new Task();
                t.OwnerId = u.id;
                t.Subject='Donni';
                t.Status='Not Started';
                t.Priority='Normal';
                insert t; 
}

try this in your method.