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 3 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

  • 49,056 Points 20k badge 2x thumb

Posted 6 years ago

  • 3
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k 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: 

//
// CONFIGURATION 
// 
// 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 = {}; 
    $.each(metadata.referenceTo,function(i,r){ 
       if (($.inArray(r.objectName,targetObjects) != -1) && (!uniqueTargets[r.objectName])) { 
          targets.push(r); 
          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 
skuid.ui.fieldRenderers[metadata.displaytype][field.mode](field,value); 






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







You're done!







BONUS

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:
   


(Edited)