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
AlecSAlecS 

Assign Opportunity Owner to a different user

I have an Opportunity trigger. I want to assign the opportunity ownership to another person 

 

I have tried:

 

 opp.Owner.Name = 'Jack Smith';

and

opp.OwnerId = '00550000001ZszE'

 

None of these work.

 

How can I assign opportunity to another user?

 

Thanks!

Cloud CredenceCloud Credence

Hi,

 

First make sure you are are writing the trigger in After Insert, Before Update  events.

 

You cannot update the owner id before the record is created.

 

Secondly please make sure the user id you are specifying is the right one. Its best practice to avoid hardcoding any ids in the code. You may query the user object with reference to user name and use that variable.

 

Updating owner id in a record is allowed.

 

Please refer to Apex documentation on creation of triggers.

 

Please also note the below points from Apex Documentation

--------------------------------------------------------------------------------------

Fields Not Updateable in Before Triggers
Some field values are set during the system save operation, which occurs after before triggers have fired. As a result, these
fields cannot be modified or accurately detected in before insert or before update triggers. Some examples include:
• Task.isClosed
• Opportunity.amount*
• Opportunity.ForecastCategory
• Opportunity.isWon
• Opportunity.isClosed
• Contract.activatedDate
• Contract.activatedById
• Case.isClosed
• Solution.isReviewed
• Id (for all records)**
• createdDate (for all records)**
• lastUpdated (for all records)
* When Opportunity has no lineitems, Amount can be modified by a before trigger.
** Id and createdDate can be detected in before update triggers, but cannot be modified.

 

Thanks