You need to sign in to do that
Don't have an account?
Matthew Keefe
Setting AccountId on Task
When inserting a new Task, AccountId cannot be set because it's set by the system. Is there a way to make sure the system sets it? I've tried setting the WhatId and the WhoId, but the AccountId is still not set after inserting the Task.
Here's some sample code:
Thanks!
Matt
Here's some sample code:
System.Savepoint sp = Database.setSavePoint(); User TestOwner = [Select Id from User where Id = :UserInfo.getUserId()]; Account TestAccount = new Account(Name = 'Test Account'); insert TestAccount; Contact TestContact = new Contact(FirstName='TEST First Name' ,LastName='TEST Last Name' ); TestContact.AccountId = TestAccount.Id; insert TestContact; Task NewTask = new Task(Subject='Test Task' ,Status='Completed' ,ActivityDate=Date.today().addDays(-7) ,OwnerId=TestOwner.Id ,WhatId=TestAccount.Id ,WhoId=TestContact.Id //,AccountId=TestAccount.Id ); insert NewTask; system.debug('New Task: ' + NewTask); system.debug('New Task AccountId: ' + NewTask.AccountId); Database.rollback(sp);
Thanks!
Matt
Turns out Salesforce was indeed setting the AccountId, I just didn't have it because Task needs to be queried to get it from the database first.