Saturday, 1 November 2014

Hiding Salesforce Junction Objects (and their Ugly Names) from the End User

Creating many-to-many relationships in SalesForce is fairly straightforward: you simply create a (junction) object with two master-detail relationship fields linking the objects that you want related, many-to-many. In my example below, I’ve created a junction object to relate two objects, shirts and colors.
Salesforce Custom FIelds and Relationships
Unfortunately, when you create a junction object, there are 2 related-list quirks that you’ll probably want to change before showing the new objects to your customer(s).

Default Related List Label

For each of the related objects, in this case shirts and colors, the default related list label displays the junction object name. In most cases, this is not what you want your users to see, especially since a junction object is almost always intended to be a background object whose sole purpose is to facilitate a many-to-many relationship. As you can see in the screenshot below, the default related-list label is a bit confusing:
Salesforce Confusing Junction Object
What we need to do to fix this atrocity is change the “Related List Label” property for both of the master-detail fields on our junction object. Navigate to the junction object via “Setup->Create->Objects->[YOUR JUNCTION OBJECT],” then drill into each master-detail field individually to change their respective “Related List Label” fields. We’ll change the “Shirt” custom field:
Salesforce Edit Property
Then, we change the related list label to “Colors:”
Salesforce Master-Detail Options
Now, when we navigate to a Shirt detail screen, we’ll notice that the related list label reflects our change:
Salesforce Hidden Name From User
This is great – now we don’t have to worry about users wondering what they’re looking at. We’re not completely out of the woods yet, however…

Related list “New” Button Label

If you looked closely at the past couple of screenshots, you may have noticed that the default “New” button on the related list also shows the junction object name by default:
Salesforce New Button
Unfortunately, there is not currently a way to change the label of the default “New” button. What we really want is to show the name of the related object. For instance, on the Shirt detail screen, we would like to see the related list “New” button read “New Color” instead of “New ShirtColorJunctionObject,” and “New Shirt” on the Color detail screen’s related list.
To do so, we need to create two new custom buttons, one for the Colors related list and one for the Shirts related list. For the purposes of this post, we’ll just be showing how to create the “New Color” button.

Crafting The “New” Link

In order to replace the existing “New ShirtColorJunctionObject” button with our custom button, we need to figure out the URL to which the default button is pointing. Obtaining that URL is easy – just click the default related list “New” button:
Salesforce New Button
Then, copy the URL – we’ll need it to create the URL for the new custom button:
Salesforce Custom Button URL
Next, we need to replace the various hard-coded URL parameters in the URL with merge fields so that our button will work with ALL Shirts/Colors. Since we’re accessing this button from the Shirt detail screen, we’ll need to supply Shirt merge fields as values for the following URL parameters:
  • CF00Nd0000006mN3E
    • This parameter represents the ID of the object from whose detail screen we’ve clicked the “New” button. So in this case, this represents the Org-Wide Object ID for the “Shirt” object. The expected value is the corresponding “Name” value for the object. We’ll use {!Shirt__c.Name} to make sure the correct name is supplied.
  • CF00Nd0000006mN3E_lkid
    • This parameter represents the ID value that should be used for the originating object’s lookup field on the linking object creation page. We’ll use {!Shirt__c.Id}.
  • saveURL
    • This parameter tells SalesForce which page to redirect to after the new junction object record is saved. Since this button will always be accessed from the Shirt detail screen, we’ll use “%2F{!Shirt__c.Id}.” Be sure to include the “%2F” (the URL-encoded version of a forward-slash), since SalesForce needs this to properly parse the URL.
  • retURL
    • This parameter needs to be set to the same value as the “saveURL” param in order for redirection after saving to work properly. The value we’ll use for this param is “%2F{!Shirt__c.Id}.”
The final URL for our “New Color” button is shown below:
  • /a09/e?CF00Nd0000006mN3E={!Shirt__c.Name}&CF00Nd0000006mN3E_lkid=%2F{!Shirt__c.Id}&saveURL=%2F{!Shirt__c.Id}&retURL=%2F{!Shirt__c.Id}

Creating the Custom Button

Now that we have our URL, we’re ready to create the custom button!
Navigate to the junction object setup screen via “Setup->Create->Objects->[YOUR JUNCTION OBJECT]” and click “New Button or Link.” Make sure the button is a “List Button,” the “Content Source” field is set to “On-Click JavaScript,” and the “Behavior” field is set to “Execute JavaScript.” We’ll also want to put the URL that we created in the previous step in the link text-area, as shown below:
Salesforce Edit Custom Button
The final step is to replace the default “New” button with our new custom button on the “Colors” related list on the Shirt detail page layout. To do so, we navigate to the Shirt detail screen and click the “Edit Layout” link in the upper right-hand corner of the page:
Salesforce Edit Layout Page
Then we click the “Related Lists” button and select the edit button on the “Colors” related list:
Salesforce Edit Related List
Now we need to uncheck the “New” checkbox in the “Standard Buttons” section of the related list edit screen, and add our new custom button (New Color) to the “Selected Buttons” list:
Salesforce Edit Layout Properties
Lastly, save the page layout and admire your work!
Salesforce New Custom Button

Conclusion

It took us a few steps to get there, but by using simple techniques, we were able to completely mask the underlying junction object from the end user. Changing the label on the related list was quick and easy, but replacing the default “New” button with a new custom button just to re-label is a bit of a process. Hopefully Salesforce will someday provide an out-of-the-box solution to this very common problem. If you agree, navigate to THIS link and vote up the idea!

No comments:

Post a Comment