Limiting Available Object for a Polymorphic field - Edinburgh SFX

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 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;
  }
}

  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");
 } else if ( val.startsWith("Account")) {
   updateFieldData(field,"001");
 } else if ( val.startsWith("Contact")) {
   updateFieldData(field,"701");
 } else if ( val.startsWith("House")) {
    updateFieldData(field,"a2C");
 } else if ( val.startsWith("Area")) {
    updateFieldData(field,"a2m");
 }
 
 
});

updateFieldData(field, refLength);

Hey Ghesketh,

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.
Custom_Polymorphic_Field.xml (6.2 KB)

E7103EB2-9626-4DAB-AEBE-91AFC483F853-95097-0004CE86D122B203

Hi Huyen,

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.

Hey Ghesketh, here’s an example of the great work Huyen did above but adjusted slightly so there is no modal. Hopefully this helps!

<skuid__page unsavedchangeswarning="yes" personalizationmode="server" showsidebar="false" showheader="false">
	<models>
		<model id="TaskModel" limit="1" query="true" datasource="salesforce" sobject="Task" createrowifnonefound="true">
			<fields>
				<field id="RecordTypeId"/>
				<field id="RecordType.Name"/>
				<field id="Id"/>
				<field id="Subject"/>
				<field id="WhatId" overridemetadata="false" ogdisplaytype="REFERENCE" displaytype="REFERENCE" datasource="salesforce"/>
				<field id="What.Name"/>
				<field id="WhatId_Type" uionly="true" displaytype="PICKLIST" length="255" ogdisplaytype="TEXT" picklistsource="manual" returntype="TEXT" defaultvaluetype="fieldvalue" defaultValue="Account">
					<picklistentries>
						<entry value="Account" label="Account"/>
						<entry value="Opportunity" label="Opportunity"/>
					</picklistentries>
				</field>
				<field id="WhatId_Account" uionly="true" displaytype="REFERENCE" length="255" ogdisplaytype="TEXT" datasource="salesforce" label="WhatId_Account" targetobjects="Account" keyfield="Id" defaultvaluetype="fieldvalue">
					<batchfields/>
				</field>
				<field id="WhatId_Opportunity" uionly="true" displaytype="REFERENCE" length="255" ogdisplaytype="TEXT" datasource="salesforce" targetobjects="Opportunity" keyfield="Id">
					<batchfields/>
				</field>
			</fields>
			<conditions>
				<condition type="blank" value="null" field="WhatId" operator="!=" enclosevalueinquotes="false"/>
			</conditions>
			<actions>
				<action>
					<actions>
						<action type="updateRow" fieldmodel="TaskModel" affectedrows="context">
							<updates>
								<update valuesource="modelmerge" field="WhatId" enclosevalueinquotes="true" sourcemodel="TaskModel" sourcefield="WhatId_Account"/>
							</updates>
						</action>
						<action type="updateRow" fieldmodel="UI_Controls" affectedrows="context">
							<updates>
								<update valuesource="fieldvalue" field="editMode" enclosevalueinquotes="false" value="false"/>
							</updates>
						</action>
						<action type="save">
							<models>
								<model>TaskModel</model>
							</models>
						</action>
						<action type="requeryModels" behavior="standard">
							<models>
								<model>TaskModel</model>
							</models>
						</action>
					</actions>
					<events>
						<event>row.updated</event>
					</events>
					<fields>
						<field>WhatId_Account</field>
					</fields>
				</action>
				<action>
					<actions>
						<action type="updateRow" fieldmodel="TaskModel" affectedrows="context">
							<updates>
								<update valuesource="modelmerge" field="WhatId" enclosevalueinquotes="true" sourcemodel="TaskModel" sourcefield="WhatId_Opportunity"/>
							</updates>
						</action>
						<action type="updateRow" fieldmodel="UI_Controls" affectedrows="context">
							<updates>
								<update valuesource="fieldvalue" field="editMode" enclosevalueinquotes="false" value="false"/>
							</updates>
						</action>
						<action type="save">
							<models>
								<model>TaskModel</model>
							</models>
						</action>
						<action type="requeryModels" behavior="standard">
							<models>
								<model>TaskModel</model>
							</models>
						</action>
					</actions>
					<events>
						<event>row.updated</event>
					</events>
					<fields>
						<field>WhatId_Opportunity</field>
					</fields>
				</action>
			</actions>
		</model>
		<model id="UI_Controls" limit="20" query="true" datasource="Ui-Only" createrowifnonefound="true">
			<fields>
				<field id="editMode" displaytype="BOOLEAN" length="255" ogdisplaytype="TEXT" defaultvaluetype="fieldvalue" defaultValue="false"/>
			</fields>
			<conditions/>
			<actions/>
		</model>
	</models>
	<components>
		<skuid__form showErrorsInline="true" model="TaskModel" uniqueid="sk-182Y-34670" mode="read" showSaveCancel="false">
			<columns>
				<column>
					<sections>
						<section title="Edit Task">
							<fields>
								<skuid__field id="Subject" uniqueId="sk-182Y-34672"/>
								<skuid__field type="COMBO" label="Related to" uniqueId="sk-182Y-34673" template="{{What.Name}}" readOnly="true">
									<interactions>
										<interaction type="tap">
											<action type="updateRow" fieldmodel="UI_Controls" affectedrows="context">
												<updates>
													<update valuesource="fieldvalue" field="editMode" enclosevalueinquotes="false" value="true"/>
												</updates>
											</action>
											<action type="action-sequence" action-sequence-id="a07ccdae-9e1a-49c1-80ef-e7ec0058781d" enabled="false"/>
										</interaction>
									</interactions>
									<renderConditions logictype="and" onhidedatabehavior="keep">
										<renderCondition type="fieldvalue" operator="=" enclosevalueinquotes="false" fieldmodel="UI_Controls" sourcetype="fieldvalue" nosourcerowbehavior="deactivate" field="editMode" value="false"/>
									</renderConditions>
									<enableConditions/>
									<styleVariantConditions/>
								</skuid__field>
							</fields>
						</section>
					</sections>
				</column>
			</columns>
			<styles>
				<spacing top="4" right="4" bottom="4" left="4"/>
			</styles>
		</skuid__form>
		<skuid__form showErrorsInline="true" model="TaskModel" uniqueid="sk-182v-38129" mode="edit" showSaveCancel="false">
			<columns>
				<column maxWidth="200px">
					<sections>
						<section title="Edit Task" showHeading="false">
							<fields>
								<skuid__field id="WhatId_Type" uniqueid="sk-182v-38130" label="Related to">
									<interactions/>
								</skuid__field>
							</fields>
							<renderConditions logictype="and">
								<renderCondition type="fieldvalue" operator="=" enclosevalueinquotes="false" fieldmodel="UI_Controls" sourcetype="fieldvalue" nosourcerowbehavior="deactivate" field="editMode" value="true"/>
							</renderConditions>
						</section>
					</sections>
				</column>
				<column behavior="flex" ratio="1" width="200px" verticalAlign="top" minWidth="100px">
					<sections>
						<section title="New Section" showHeading="false">
							<fields>
								<skuid__field id="WhatId_Account" label="Related Account">
									<renderConditions logictype="and" onhidedatabehavior="keep">
										<renderCondition type="fieldvalue" operator="=" enclosevalueinquotes="true" fieldmodel="TaskModel" sourcetype="fieldvalue" field="WhatId_Type" fieldtargetobjects="Task" value="Account"/>
									</renderConditions>
									<enableConditions/>
									<styleVariantConditions/>
								</skuid__field>
								<skuid__field id="WhatId_Opportunity" label="Related Opportunity">
									<renderConditions logictype="and" onhidedatabehavior="keep">
										<renderCondition type="fieldvalue" operator="=" enclosevalueinquotes="true" fieldmodel="TaskModel" sourcetype="fieldvalue" field="WhatId_Type" fieldtargetobjects="Task" value="Opportunity"/>
									</renderConditions>
									<enableConditions/>
									<styleVariantConditions/>
								</skuid__field>
							</fields>
						</section>
					</sections>
				</column>
			</columns>
			<styles>
				<spacing top="4" right="4" bottom="4" left="4"/>
			</styles>
			<renderConditions logictype="and">
				<renderCondition type="fieldvalue" operator="=" enclosevalueinquotes="false" fieldmodel="UI_Controls" sourcetype="fieldvalue" nosourcerowbehavior="deactivate" field="editMode" value="true"/>
			</renderConditions>
			<styleVariantConditions/>
		</skuid__form>
	</components>
	<resources>
		<labels/>
		<javascript/>
		<actionsequences>
			<actionsequence id="a07ccdae-9e1a-49c1-80ef-e7ec0058781d" label="Custom Polymorphic Field Modal" type="reusable">
				<actions>
					<action type="showModal">
						<skuid__modal title="Edit WhatId" width="640px" ariaRole="dialog">
							<components>
								<skuid__form showErrorsInline="true" model="TaskModel" uniqueid="sk-17SD-101261" mode="edit" showSaveCancel="false">
									<columns>
										<column ratio="1" minWidth="100px" behavior="flex">
											<sections>
												<section title="Edit Task" showHeading="false">
													<fields>
														<skuid__field id="WhatId_Type" uniqueid="sk-17SD-101262">
															<interactions/>
														</skuid__field>
														<skuid__field id="WhatId_Account" label="Related Account">
															<renderConditions logictype="and" onhidedatabehavior="keep">
																<renderCondition type="fieldvalue" operator="=" enclosevalueinquotes="true" fieldmodel="TaskModel" sourcetype="fieldvalue" field="WhatId_Type" fieldtargetobjects="Task" value="Account"/>
															</renderConditions>
															<enableConditions/>
															<styleVariantConditions/>
														</skuid__field>
														<skuid__field id="WhatId_Opportunity" label="Related Opportunity">
															<renderConditions logictype="and" onhidedatabehavior="keep">
																<renderCondition type="fieldvalue" operator="=" enclosevalueinquotes="true" fieldmodel="TaskModel" sourcetype="fieldvalue" field="WhatId_Type" fieldtargetobjects="Task" value="Opportunity"/>
															</renderConditions>
															<enableConditions/>
															<styleVariantConditions/>
														</skuid__field>
													</fields>
												</section>
											</sections>
										</column>
									</columns>
									<styles>
										<spacing top="2" right="2" bottom="2" left="2"/>
									</styles>
								</skuid__form>
							</components>
							<buttons>
								<skuid__button label="Save" uniqueId="sk-17SM-107128" styleSettingsVariant="primary">
									<actions>
										<action type="save">
											<models>
												<model>TaskModel</model>
											</models>
										</action>
										<action type="requeryModels" behavior="standard">
											<models>
												<model>TaskModel</model>
											</models>
										</action>
										<action type="closeModals" closebehavior="close"/>
									</actions>
								</skuid__button>
								<skuid__button label="Cancel" uniqueId="sk-17SR-109794">
									<actions>
										<action type="cancel">
											<models>
												<model>TaskModel</model>
											</models>
										</action>
										<action type="closeModals" closebehavior="close"/>
									</actions>
								</skuid__button>
							</buttons>
							<afterCloseActions/>
						</skuid__modal>
					</action>
				</actions>
				<description/>
			</actionsequence>
		</actionsequences>
	</resources>
	<background/>
	<interactions/>
	<surfaces/>
</skuid__page>
1 Like

I think that may work for us! Thanks Chris!

1 Like

Awesome! Glad to hear it!!