Customize choice list based on reference field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 11:09 AM
Hi all,
This might be really simple but I'm not able to figure out. I have a 'Format' choice field on a custom table, and a reference field. I want the format choices to be populated depending on the 'field type' (field on referenced table) of the selected record. This is pretty straight-forward if the dependent field is a choice or any other field. I tried with client script but didn't give me the exact results. it worked fine but failed in some scenarios. I was wondering if there's another way to do it.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 11:11 AM
Hi Veena,
If you can share the client script, there's an army of ServiceNow geeks ready to take a look and see what we can come up with.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 11:16 AM
Haha. Thanks. Here is the client script. I wrote an onChange CS.
It worked fine in general when I'm creating a new record. But when I save a record, I don't see the choices if i load it again. I thought, may be i should write a similar onLoad() too but when I did that, there's extra choices(the value of stored choice) on the field.
Note: The field itself doesn't have any choices. Only through the client script, the choices are being added. i.e. if I open the dictionary, there's no choices for the field. I'm not sure if that is right.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var sourcefield = g_form.getValue('u_source_field_name');
var gr = new GlideRecord('u_source_field_list');
gr.get(sourcefield);
var fieldtype = gr.u_field_type;
if(fieldtype == 'Date/Time'){
// g_form.clearOptions('u_format');
g_form.setVisible('u_format', true);
g_form.addOption('u_format', 'MMddyyyy_hhmmtt', 'MM/dd/yyyy hh:mm tt', 0);
g_form.addOption('u_format', 'MMddyyyy_hhmm', 'MM/dd/yyyy HH:mm', 1);
g_form.addOption('u_format', 'yyyyMMdd_hhmmtt', 'yyyyMMdd hh:mm tt', 2);
g_form.addOption('u_format', 'yyyyMMdd_hhmm', 'yyyyMMdd HH:mm', 3);
g_form.addOption('u_format', 'yyyyMMdd', 'yyyyMMdd', 4);
g_form.addOption('u_format', 'MMddyyyy', 'MM/dd/yyyy', 5);
g_form.addOption('u_format', 'hhmmtt', 'hh:mm tt', 6);
g_form.addOption('u_format', 'hhmm', 'HH:mm', 7);
}
else{
g_form.setVisible('u_format', false);
g_form.clearOptions('u_format');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 11:21 AM
Here's how I would re-engineer this to be easier to maintain. The assumption is that there's only that one choice of "Date/Time" you are interested in...
- Populate the choice list with the formats you suggest
- Use a UI policy to show/hide the choice list. If the value from u_source_field_list.u_format is "Date/Time", show the list and make it mandatory (optional), otherwise hide it.
No scripting involved!
Creating a UI Policy - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 11:24 AM
That's good for now. But this is still in baby stages. But we may need to specify additional Field Types with their own corresponding Format options in the future. We want to be able to configure choices accordingly.