[SalesForce] Updating the owner of records connected via lookup relationship

I was given a task to write a trigger to update the owner of records connected via lookup relationship.

Consider i've a custom object which is having a lookup relationship to another custom object. What i have to achieve is to be able to update the owner field of the other object, through trigger.

I believe, this could be achieved through Workflow rules. But then, until i'm sure that trigger for this functionality is really a bad idea, i don't want to give up on the trigger.

The owner is definitely a contact record, so i gotta change that owner field to point to a different contact.

Please suggest on this, i'd been trying to get my head around this for last two day, but so far i'm stuck.

Another thing i noticed is, the owner field data type is Lookup(User,Queue). I'm not much sure, what that means.

Best Regards,
Sangram

Best Answer

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 Parent__c to Child__c, you will create a Process on Parent__c. You will want your filter criteria to be OwnerId',ischanged, true. Add an Action to Update Records. Select the child relationship (e.g. Children__r), select the field you want to populate (e.g. OwnerId), and select the field you want to populate from (e.g. OwnerId from the Parent__c 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 Child__c to Parent__c, you should be able to reverse the above steps. :)

Related Topic