Owner Info in Formulas Workaround

FacebookTwitterGoogleLinkedInEmail this page


Ownership is an important concept in Salesforce. Ownership in conjunction with the Role Hierarchy determine the core of your sharing model (unless you’ve opted for Territories)… and up until criteria-based sharing was released recently (which is freakin killer by the way) sharing rules were all centered around ownership as well. So it follows that using Owner information in your formulas will end up being desired functionality, but alas (at the time I am writing this) you cannot.┬áHere is an illustration to help explain the problem:

This is backed up by the fact that it has been posted to the IdeaExchange not one but many times… right now I count about 7 duplicates. I have been reassured they will be merged soon… so I will only post a link here to the one I think will become the ‘master’ of the merges:

Make “Owner Id” Look up fields available for formulas

So, I tweeted this issue and immediately received support and/or agreement from @vanityforce__c, @timothyainman, @RatherGeeky, @sfdc_nerd, and @aognenoff. We had a surprisingly effective and engaging interaction about different approaches considering we were each limited to 140 characters… minus the ‘mentions’. So I am posting what I ended up implementing after digesting their guidance. Thanks guys!

The basic idea is that while you can’t use cross-object formulas on the Owner of a record… you CAN use a custom lookup relationship to the User object. So we’re going to create our own lookup field and then make sure it is always synchronized with the standard Owner Field.

Ingredients:

1 Lookup Field
1 Apex Trigger
Formula Fields as desired
Validation Rules as desired

Instructions:

First step, make your lookup field. It doesn’t matter what object you’re working with, standard or custom, but in this example I will work with the Lead object. I chose to call my field “Owner (Copy)” with a technical name of “Owner_Copy__c”. You probably don’t want to add it to any page layouts because it will just confuse people.

Next we add the trigger which is going to keep our “Copy” field always in sync’ with the actual Owner. The code is below, note that a check is done on the first three characters of the Owner Id… the reason for this is that Leads (and perhaps the object you are working with) can be owned by Queues. In the event that the record is owned by a Queue we don’t want to try to populate our new lookup field with the ID because we’ll just get an Apex error… and no one wants that.

So now all you have to do is create a Formula Field, or a Validation Rule, or define criteria for workflow via formula, (and maybe some other places…hmm) and when you click the “Insert Field” button you will be able to select “Owner (Copy) >” (or whatever you called your field) and insert fields from the Owner into your formula! Because its a lookup you don’t have to worry about changes to the Owner’s User record… your formulas will reflect the current values always.

Don’t forget to write a healthy test class to make sure this Trigger plays well with your other Triggers. Also, you will need to initialize your Copy field, because while it will handle changes going forward, your existing data will not be automatically populated by creating this Trigger… so export your Owner Id for all records, and pop it into the “Copy” field after you’ve got your Trigger in place. Cheers!

trigger ownerCopy on Lead (before Insert, before Update) {

// handle arbitrary number of opps
for(Lead x : Trigger.New){

// check that owner is a user (not a queue)
if( ((String)x.OwnerId).substring(0,3) == ‘005’ ){
x.Owner_Copy__c = x.OwnerId;
}
else{
// in case of Queue we clear out our copy field
x.Owner_Copy__c = null;
}
}

}

 

Be the first to comment

Leave a Reply


Your email address will not be published.


*