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

AccountContactRole.AccountId field is not writable error



I have a trigger which executes when an Opportunity is marked as 'Closed Won'. It creates/updates Contact Roles in Account based on the Contact Roles in the associated Opportunity. If a Contact Role exists in Account, it updates its Role to the Role of the Contact in the associated Opportunity.

If Account Contact Role does not exist it creates it and sets the Role to that defined in the corresponding OpportunityContactRole.

Now, if there are AccountContactRoles in the Account before the trigger executes, the trigger works fine. But, if there are no AccountContactRoles, i get the following error:



Field is not writeable: AccountContactRole.AccountId

 My code is as follows :



AccountContactRole acr = new AccountContactRole();
acr.AccountId = accountId;
acr.ContactId = o.ContactId;
acr.Role = o.Role;
insert acr;



Please help

Thank You



AccountId for AccountContactRole is only creatable. You can not update it. but you can update ContactId


Thanks for the reply sforce. In my code im creating a new AccountContactRole, and setting its AccountId and ContactId fields. Im not updating. Is there anything wrong with my code ?


Please help.

Thank You.


It worked for me very fine. By any chance is your before Insert trigger is on AccountContactRole? can u post the code. so that I can investigate whats wrong


Looking at your code, aren't you missing a reference? 



acr.AccountId = o.accountId;