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
TrautsTrauts 

reference Opportunity Owner (Lookup(User)) in a custom object formula

Opportunity has two fields I want to reference in another custom object, Owner and CloseDate.  For CloseDate I created a formula field returning a date and simply referenced the date field using Opportunity__r.CloseDate.  I tried the same method for Owner.  I created a formula field returning text and referenced the owner with Opportunity__r.Owner.  The former works.  The latter gives a syntax error stating Owner is not a field.

 

When I check the Opportunity object it says the field name is "Owner".  What is wrong?  A possible clue is that Owner is data type Lookup(User).

Shashikant SharmaShashikant Sharma

try with

 

Opportunity__r.OwnerId

 It should work for you.

TrautsTrauts

Closer, but not quite there.  No syntax error but I get the actual owner ID value instead of their name.  Is there a function in which I can use that ID to return the name?

 

Or perhaps I'm approaching this the wrong way by using a formula?  I tried creating a master-detail field but that didn't work either.

Ankit AroraAnkit Arora

If you want to get the name of the Owner then you can do something like this :

 

"Opportunity__r.Owner.Name"

 

And if you want to get the Id of the Owner then you can do like this :

 

"Opportunity__r.OwnerId"

 

It depends upon your approach, what exactly you are trying to do with these. If you just want to display it as text then 

 

"Opportunity__r.Owner.Name" will work for you, but if you want to put it in another lookup then you need Id for sure and name will be fetched out automatically.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

sfdcfoxsfdcfox

Ankit,

 

You know that "Owner" is not traversable in formulas, right? As odd as it seems, it just doesn't work. "Owner.Name" seems perfectly reasonable, yet if you try it, you'll see it won't work. This is an obscure limitation that's been around at least as long as I recall formula fields being around.

 

Trauts,

 

Regrettably, there is no way to display the owner's name directly in a formula. There is a workaround, though. Create a custom user lookup field on opportunities, and have that populated (via a trigger) with the current owner ID. You can then use: "Opportunity__r.Owner__r.Name".

 

The trigger would be as follows:

 

trigger copyOwner on Opportunity (before insert, before update) {
  for(Opportunity o:Trigger.new)
    o.Owner__c = o.OwnerId;
}

Test method you'd need to install along side the trigger for deployment:

 

public class TriggerTests {
  public static void testMethod testOpportunityOwnerCopy() {
    Opportunity o = new Opportunity(Name='test',closedate=system.today(),StageName='Closed Won',Probability=1);
     insert o;
     o = [select id,ownerid,owner__c from opportunity where id = :o.id];
     system.assertequals(o.ownerid,o.owner__c,'Custom owner field should equal system owner field.');
  }
}

 

This is the recommendation I've given to several clients that have all had this sort of request (and, oddly, I think it's always a custom object that links to an opportunity!).

 

Let me know if you have any troubles.

Ron La Porte 7Ron La Porte 7
This post is older, so one would assume that this issue would've been solved by SFDC. Yet, I am getting a similar error when trying to have the Opportunity Owner show up in a formula field ona custom object that is linked to the Opportunity. I would like to this to be done soI can setup Work Flow Email Alerts to the Opportunity Owner when certain criteria is met within the custom object. Can anyone help with this?