0

Struggling a bit here.

I am trying to create a workflow/flow that will automatically update owners of an opportunity without the complexity of creating a trigger.

The relationship goes -

The child object is attached to opportunities (and can be attached to various opportunities), I want a workflow that will automatically change the opportunity owner to the child object owner if the two records do not align already.

In terms of the workflow actaully triggering this could be 'any time the opportunity record is edited/created'

Thanks all :)

4
  • "The child object is attached to opportunities (and can be attached to various opportunities)" Does that mean it is a many-to-many relationship using a junction object? Oct 15, 2014 at 14:38
  • No junction object - simply just one to many relationship on the child object side. The child can be attached to many opportunties yet an opportunity can only be attached to one child object record.
    – TJT
    Oct 15, 2014 at 14:46
  • OK, so do I understand correctly then that there is a lookup field on the Opportunity that points to the other object? Oct 15, 2014 at 15:18
  • Hi Guy - yes that's it
    – TJT
    Oct 15, 2014 at 15:54

1 Answer 1

0

You will need an APEX trigger for this, as it is not possible to update an Opportunity Owner with a variable value (you can only use a predefined user that is fixed in the workflow).

Here's a simple example trigger, assuming that the related object is named 'RelatedRecord'. So you'd need to replace that with the actual API name.

I didn't test the code so let me know if you run into trouble.

trigger UpdateOwnerID on Opportunity (before insert, before update) {

    // first find out if the owner is deviating from the related record owner
    // do this by requerying the opportunity including the owner of the related record
    Map<Id,Id> oppIdToNewOwnerId = new Map<Id,Id>();
    for(Opportunity o : [Select 
                            Id, 
                            OwnerId, 
                            RelatedRecord__r.OwnerId 
                        From 
                            Opportunity 
                        Where 
                            Id in :trigger.NewMap.keySet() 
                        AND 
                            OwnerId != RelatedRecord__r.OwnerId])
    {
        // if the opp owner deviates from the relatedrecord owner, add to the map
        oppIdToNewOwnerId.put(o.Id,o.RelatedRecord__r.OwnerId);
    }

    // now go through the records in the trigger and update where applicable
    for(opportunity opp : trigger.new)
    {
        if(oppIdToNewOwnerId.containsKey(opp.Id)){
            opp.OwnerId = oppIdToNewOwnerId.get(opp.Id);
        }
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .