Dynamically lookup field label and values from a custom table (or data lookup record)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2023 11:00 AM
I am wondering what everyone might recommend for the following scenario:
We are considering customizing demand_task to add a generic question field which depending on the particular demand task, would then have a different question label and values depending on the requirement. This would mean that we could (possibly) - just create some sort of custom table or lookup table with labels and values, where this one field (or possibly more) - could be dynamically set.
This would mean that we would not need to create multiple custom fields on the demand task or the demand itself to cater for requirements.
Has anyone done this or anything like this? Is there a best practice or recommended way to do this? Presuming for now it is say just one question label, and a couple of values changing per requirement..
For example, the question in one scenario might just be "server backup needed" - yes/no, which would then trigger a different task based on the answer. In another situation the field label would be different but might also say yes/no or something else. The question field could be shown or hidden based on a field drop down value from the parent task.
Through a client script (on the demand_task - onload for example) I think it would kind of have to be re-written for each requirement. I was hoping that some sort of custom table or lookup table solution might be a bit more cleaner and easier to maintain.
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2023 10:07 PM
Hi @ghass ,
For a dynamic and more maintainable approach to handle custom fields based on different scenarios, you can indeed consider using a lookup table or a custom table. Here's a general outline of how you might approach this:
Custom Table Approach:
-
Create a Custom Table: Create a new table to store your dynamic question details. This table might have fields like
Label
,Values
,Type
, etc. -
Link Table to Task Table: Create a reference field in your
demand_task
table that references records in your new custom table. This field will determine which set of dynamic questions to load. -
Client Script for Dynamic Field Generation:
- Use a client script on the
demand_task
form. - On form load, query the linked custom table using the value in the reference field to get the dynamic questions.
- Dynamically generate the necessary HTML elements (labels, checkboxes, etc.) based on the retrieved data and inject them into the form.
- Use a client script on the
UI Page/Widget Approach:
-
Create a UI Page or Widget: You can create a UI Page or Widget that queries the custom table based on the task and renders the dynamic fields.
-
Embed in Form: Embed this UI Page or Widget in the form of the
demand_task
record using UI Policies or Client Scripts.
Example Client Script for Dynamic Field Generation:
function onLoad() {
// Assuming you have a reference field 'question_set' linking to your custom table
var questionSet = g_form.getValue('question_set');
// Query the custom table to get the dynamic questions
var gr = new GlideRecord('your_dynamic_questions_table');
gr.addQuery('question_set', questionSet);
gr.query();
while (gr.next()) {
// Depending on the question type (text, checkbox, etc.), generate appropriate HTML
var questionLabel = gr.getValue('label');
var questionValues = gr.getValue('values');
// Inject the HTML into the form
// ...
}
}
Thanks,
Ratnakar