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
Steve ThurstonSteve Thurston 

Update Opportunity Owner to Match a New Activity's "Assigned To" User

We are implementing a third party scheduling app that the office staff use to create new work job appointments for specific Salesforce Users, which then gets fed back into Salesforce as a calendar Event, assigned to the appropriate User.

The Opportunity exists prior to the appointment scheduling.  Once the job has been assigned in the third party app and the calendar Event created in Salesforce, the ownership of the Salesforce Opp should be updated to match the User the Event has been assigned to.  The Event is connected to the Opportunity through the 'Related To' field.

I've seen lots of suggestions on how to do the reverse type of flow:  updating a task to match the parent object's ownership, but I'm going in the opposite direction:  Looking at the child (the Event Assigned To field) to update the related parent (the Opportunity Owner field)

I don't think this can be done with either a workflow or with process builder.  So I think I need to resort to Apex.  I don't consider myself an Apex specialist.  Is there a simple trigger that could accomplish this task?

Thanks!
Naval Sharma4Naval Sharma4
Hi Steve,

First of all, you have it backwards. If you can achieve functionality through workflow instead of trigger, you should do so! Clicks not code and all that. There are some exceptions to this but it is a solid general rule.
Unfortunately, workflows are not capable of updating lookups with dynamic values. For instance you could hard code an Id in the workflow field update, but that is clearly how you want.
However, all hope for a configurable solution is not lost. You can use Lightning Process Builder to dynamically populate lookups. You have not provided any relationship names or, more importantly, the relationship direction, so I will touch on both.
If you want to sync from Opportunity to Activity, you will create a Process on Opportunity. You will want your filter criteria to be OwnerId',ischanged, true. Add an Action to Update Records. Select the child relationship (e.g. Activity), select the field you want to populate (e.g. OwnerId), and select the field you want to populate from (e.g. OwnerId from the Opportunity record). The input widget for the value to populate allows you to toggle between a hard coded value and a field value from the source record.

If you want to sync Activity to Opportunity, you should be able to reverse the above steps. :)