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
LuxsaLuxsa 

Trying to retrieve custom fields on the user Profile of the Owner of an opportunity in a formula

I am trying to retrieve custom fields on the user Profile of the Owner of an Opportunity in a formula and can't seem to find a way of getting to it.  How can I achieve this?

 

Thanks

 

Luc

Best Answer chosen by Admin (Salesforce Developers) 
BA_AdminBA_Admin

Follow these steps:

 

1) create lookup relationship to user from Opp lets say User__c

2) Create a formula field on Opp

TEXT( User__r.Line_of_Business__c )

Here Line_of_Business__c is the custom field on user

 

3) cerate a trigger on Opp, it is going to be like this

 

trigger UserLineofBusiness on opportunity (before insert)
{
 List<User> us = new List<User>();
 Id userId;
 for (Opportunity o : Trigger.New)
  {
    userId = o.OwnerId;
  }
     us = [select Id,Name from User where id =:userId];
     for(Opportunity opp : trigger.New)
       {
         for (User u :us)
            {
              opp.User__c = u.Id;
            }
       }

}

 

All Answers

Steve :-/Steve :-/

Unfortunately the Opportunity.Owner field is a Dead End (that is as far as you can get with a formula).  What exactly are you trying to do?

LuxsaLuxsa

I created a custom fiueld on the User object called 'Line of business'.

In the opportunity I am trying to create a formula field that uses that user's line of business as a criteria to define a certain value.

 

Steve :-/Steve :-/

You can do it for CreatedBy, LastModifiedBy, and $User, but not owner (sorry)

LuxsaLuxsa

Could I retrieve the opportunity's owner role or profile somehow (that might be a workaround)?

 

LuxsaLuxsa

Thanks for the quick replies :smileyhappy:

Really appreciated!

BA_AdminBA_Admin

Follow these steps:

 

1) create lookup relationship to user from Opp lets say User__c

2) Create a formula field on Opp

TEXT( User__r.Line_of_Business__c )

Here Line_of_Business__c is the custom field on user

 

3) cerate a trigger on Opp, it is going to be like this

 

trigger UserLineofBusiness on opportunity (before insert)
{
 List<User> us = new List<User>();
 Id userId;
 for (Opportunity o : Trigger.New)
  {
    userId = o.OwnerId;
  }
     us = [select Id,Name from User where id =:userId];
     for(Opportunity opp : trigger.New)
       {
         for (User u :us)
            {
              opp.User__c = u.Id;
            }
       }

}

 

This was selected as the best answer
LuxsaLuxsa

Thank you so much for this idea

Have a great weekend

Luc

Jerun JoseJerun Jose

Hi,

 

A simpler trigger to do the trick would be :

 

trigger populateOwner on Opportunity (before insert, before update){
for( Opportunity opty : trigger.new )
  opty.Owner_field__c = opty.OwnerID;
}

 Of course, this one isn't the most stable one, as the Opportunity owner can be a user or a queue. If your org uses only users as Opportunity owners, then this should be fine.

LuxsaLuxsa

Thanks.  It is the case exactly

LuxsaLuxsa

How would you adapt it to capture a custom field coming from the user profile and populate the custom field for that field in the oppty (i.e. Line_of_business_c)?

Jerun JoseJerun Jose

Whoa..

 

I did not think that the profile object was customizable.. Well .. If it is then you could just extend the formula to pull the field from the profile object.

 

In your formula field, you could just use the formula as :

 

User_lookup__r.Profile.Custom_field__c

 

 

I'm not sure if I addressed your issue. Let me know otherwise.

Jerun JoseJerun Jose

Going over the topic once again. I think what you wanted is to have a custom field in Opportunity to show some field from the User object.

 

You can have a formula field with the formula as:

 

User_lookup__r.Custom_field_on_User__c