+ Start a Discussion
LudivineLudivine 

Is it possible to Update the Name of the Contract Owner?

Hi,

When the Owner of an account has been changed , I would like to change as well the owner on the related Contract.
I wrote this trigger but it gives me an error, Does someone see where is the issue?
Many thanks for your time.

[Code]
trigger ChangeAccountOwnerOnContract on Account(after insert,After update){

for(Account Acc :Trigger.new){
List<Contract> C = [select Owner.Id from Contract where Account.Id = :Acc.Id];

for (Contract Ctrct : C)
{
Ctrct.Owner.Id = Acc.Owner.Id ;
}
update C;
}
}
[/code]

Error: Apex trigger ChangeAccountOwnerOnContract caused an unexpected exception, contact your administrator: ChangeAccountOwnerOnContract: execution of AfterUpdate

caused by: System.DmlException: Update failed. First exception on row 0 with id 80020000000vULAAA2; first error: INVALID_FIELD, Cannot specify both an external ID reference Owner and a salesforce id, OwnerId: []: Trigger.ChangeAccountOwnerOnContract: line 14, column 1
Best Answer chosen by Ludivine
nbknbk
Can you change the Owner to OwnerId. Hope this will work.

contact con =[select id,ownerid from contact where id='003F000000pdYZy'];
con.ownerid='005F0000002D0t3';
update con;


All Answers

Sonam_SFDCSonam_SFDC
Follow the code structure given in the following thread to get rid of the error message:
https://developer.salesforce.com/forums?id=906F00000008rCsIAI
LudivineLudivine
Hi Sonam,
thanks for your reply.
I changée owner.id by Owner. I changed the account Owner on Account,The error Is not showing anymore but the Contract Owner has not been updated on the related Contract :( Any ideas?
nbknbk
Can you change the Owner to OwnerId. Hope this will work.

contact con =[select id,ownerid from contact where id='003F000000pdYZy'];
con.ownerid='005F0000002D0t3';
update con;


This was selected as the best answer
LudivineLudivine
Hi nbk,

I have changed Owner by OwnerId and it is working now!
Many thanks for your great support!
nbknbk
Thanks..Can you mark the answer as 'Best Answer' so that it will help to others.