Can I limit which objects are possible targets of polymorphic fields such as the WhatId and WhoId fields on Tasks and Events?

  • 3
  • Question
  • Updated 5 years ago
  • Answered
When working with the "WhatId" (aka "Opportunity/Account Id" or "Related To") field on the Task and Event objects, Salesforce lets you pick from lots of different possible target objects. On certain Skuid Pages, I would like to limit the set of possible target objects to, say, just Opportunities, or just Accounts and Cases, so that the user doesn't have to scroll through a list of all the possible target objects.

Is this possible with Skuid?

Photo of Zach McElrath

Zach McElrath, Employee

  • 55,056 Points 50k badge 2x thumb

Posted 7 years ago

  • 3
Photo of Zach McElrath

Zach McElrath, Employee

  • 55,056 Points 50k badge 2x thumb
Official Response
Yes, this is possible using a custom field renderer. For our example, we'll limit this to just let the User pick Accounts or Opportunities.

1. Create a custom field renderer

-Snippet Name: something like "relatedToRenderer"
-Type: Inline (Snippet)
-Body: Copy the following, but edit the top part to select your desired target objects: 

// Define the possible target objects 
var targetObjects = ['Account', 'Opportunity']; 
// Render as a Picklist instead of an Autocomplete? 
var renderAsPicklist = false; 
var field = arguments[0], 
    value = skuid.utils.decodeHTML(arguments[1]), 
    metadata = field.metadata, 
    $ = skuid.$; 
if (field.mode == 'edit') { 
    // Limit the set of target objects 
    var targets = [], 
       uniqueTargets = {}; 
       if (($.inArray(r.objectName,targetObjects) != -1) && (!uniqueTargets[r.objectName])) { 
          uniqueTargets[r.objectName] = 1; 
          if (targets.length == targetObjects.length) return false; 
    if (targets.length) { 
       // Make this field render as a picklist? 
       if (renderAsPicklist) field.options.type = 'REFPICK'; 
       // Override the current referenceTo 
       metadata.referenceTo.length = 0; 
       metadata.ref = $.map(targets,function(targ){return targ.objectName;}).join();
       metadata.referenceTo = targets; 

// Run the standard renderer 

2. Assign this custom field renderer to your "WhatId" field

You're done!


If you only want ONE possible target item, this is fine -- Skuid will remove the object-selection picklist entirely! For instance, to limit to just Cases: