Using SFX Edinburgh, I have a form with a polymorphic field (WhatId) that references many objects. I would like to limit the objects that are available to reference to 5 specific ones for use on this form as the others don’t make sense for the user. With the custom field renderer feature turned on, I attempted to use some code found in an older community post ( Dynamically Control the WhatId reference field popup - Questions - Skuid Community) However the JS I used is throwing errors in the console (TypeError: skuid.ui.getFieldRenderer is not a function). This makes sense when reading through the docs for the current version. JS is not my strong suit however and was wondering if anyone has a sample snippet attempting to accomplish the same thing? The code I attempted to use below, Thanks!!
var targetObjects = ['Vehicles__c', 'Contact', 'Account', 'House__c', 'Area__c'];
var renderAsPicklist = false;
field = arguments[0];
var value = skuid.utils.decodeHTML(arguments[1]),
metadata = field.metadata,
$ = skuid.$;
var params = arguments[0], model = params.model,
row = params.row,
$ = skuid.$;
var updateFieldData = function(field, refLength) {switch ( refLength) {
case"001": // Account
field.options.returnFields = [
{id: 'Id', showInSearchDialog: false},
{id: 'Name', showInSearchDialog: true},
];
field.options.searchTemplate = "{{Name}}";
break;
case"701": // Contact
field.options.returnFields = [
{id: 'Id', showInSearchDialog: false},
{id: 'Name', showInSearchDialog: true},
];
field.options.searchTemplate = "{{Name}}";
break;
case"006": // Vehicles
field.options.returnFields = [
{id: 'Id', showInSearchDialog: false},
{id: 'Name', showInSearchDialog: true},
];
field.options.searchTemplate = "{{Name}}";
break;
case"a2c": // House
field.options.returnFields = [
{id: 'Id', showInSearchDialog: false},
{id: 'Name', showInSearchDialog: true},
];
field.options.searchTemplate = "{{Name}}";
break;
case"a2m": // Area
field.options.returnFields = [
{id: 'Id', showInSearchDialog: false},
{id: 'Name', showInSearchDialog: true},
];
field.options.searchTemplate = "{{Name}}";
break;
}
};
if (field.mode == 'edit') {
// Limit the set of target objectsvar 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) returnfalse;
}
});
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;
}
}
skuid.ui.getFieldRenderer(field)[field.mode]( field, value );
//skuid.ui.fieldRenderers[metadata.displaytype][field.mode](field,value);var params = arguments[0],
model = params.model,
row = params.row,
$ = skuid.$;
var whatId = row.WhatId;
var refString = String(whatId);
var refLength = refString.substring(0, 3);
if ( refLength === 'und') refLength = "006";//default to Vehicles
$(field.element).find("select").bind("change", function(e) {var val = $( "select option:selected" ).text();
if ( val.startsWith("Vehicles")) {
updateFieldData(field,"006");
} elseif ( val.startsWith("Account")) {
updateFieldData(field,"001");
} elseif ( val.startsWith("Contact")) {
updateFieldData(field,"701");
} elseif ( val.startsWith("House")) {
updateFieldData(field,"a2C");
} elseif ( val.startsWith("Area")) {
updateFieldData(field,"a2m");
}
});
updateFieldData(field, refLength);
Best answer by cstern
Hey Ghesketh, here’s an example of the great work Huyen did above but adjusted slightly so there is no modal. Hopefully this helps!
I know this is not exactly the custom field renderer you’re asking for, but just wanting to provide a declarative alternative, using UI only fields and a modal to edit the polymorphic field. The WhatId field is set to read-only, and user can click on the field to open the Edit modal. The XML for this solution is attached if you want to play with this solution.
Thank you for the reply and solution. That could definitely work; however, I fear that might provide the users with too many clicks during the process they are completing. I will run it by some folks and see if it will work, but I think ideally the customer field renderer will be the preferred option.
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.