+ 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.