1

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

3
  • Just a note, lookups have direction. One object is parent and one is child. So you will help people answer your questions if you are more specific about direction of information flow.
    – Adrian Larson
    Commented Feb 28, 2015 at 14:55
  • Sure, here's the flow i.imgur.com/wjq6QMP.png
    – user477
    Commented Feb 28, 2015 at 15:06
  • Are all of these objects custom objects? And what is the purpose of the owner field? Are you driving sharing rules off of it?
    – Eric Clay
    Commented Feb 28, 2015 at 17:32

1 Answer 1

1

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. :)

0

You must log in to answer this question.