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!

SharathChandraSharathChandra

Same thing is woking for me with OwnerId

crop1645crop1645
for (Opportunity o: Trigger.new)
  o.ownerId = somevblContainingUserId;

 is all you uneed in a before insert/update trigger.  Note that you should never use hard coded ids in APEX code, rather, do one of the following:

 

  • query the User Sobject for the user.id field you need
  • query some other Sobject that contains a field, like ownerId for the user you need or other ID field that is a User
  • Use Custom settings to store environment-specific parameters such as some special userid for this situation