How to remove dependent choice list options with g_form.removeOptions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2020 01:57 PM
Hi all,
I've written an onLoad client script which does quite a lot of things. One of those things is using g_form.removeOptions on a choice list when g_form.isNewRecord() evaluates to true.
It's working fine to remove options from 'hardware_status' but it will not remove options from 'hardware_substatus'.
I've disabled all UI actions and reviewed the onLoad script to see if there would be anything interfering but have come up empty. I'm wondering if this is happening.. or rather, not happening, because the 'hardware_substatus' choices are dependent values of the 'hardware_status' choice list. It's a simple script... I'm not nearly skilled enough to be causing trouble with code gone wrong.
function onLoad() {
//Performs the following actions if the record is "new"
if(g_form.isNewRecord()){
//Hide form sections with read-only information which is populated by integration
g_form.setSectionDisplay('network_information', false);
g_form.setSectionDisplay('operating_systeminformation', false);
g_form.setSectionDisplay('hardware_information', false);
//Hide fields which are read only
g_form.setDisplay('u_ad_description', false);
g_form.setDisplay('manufacturer', false);
g_form.setDisplay('name', false);
g_form.setDisplay('serial_number', false);
g_form.setDisplay('chassis_type', false);
g_form.setDisplay('sys_updated_on', false);
g_form.setDisplay('sys_updated_by', false);
g_form.setDisplay('support_group', false);
g_form.setDisplay('asset', false);
g_form.setDisplay('model_number', false);
//Set default value of 'hardware_status' to "In Stock"
g_form.setValue('hardware_status', 'in_stock');
//Remove options from 'hardware_status' so only "In Stock" is available. Since CMDB will be used to create new assets they will always be "in stock" and exist prior to being set to any other state
g_form.removeOption('hardware_status', 'start_retirement');
g_form.removeOption('hardware_status', 'retired');
g_form.removeOption('hardware_status', 'in_use');
g_form.removeOption('hardware_substatus', 'pending_repair');
}
Any help would be appreciated.
Thanks,
Daniel

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 06:01 PM
Looks like you are looking at CMDB or computer table so try checking alm_hardware_state_mapping.list
Refer to docs on how it's setup - https://docs.servicenow.com/bundle/istanbul-it-service-management/page/product/asset-management/task...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 06:59 PM
Thanks Mike! Very helpful as always 🙂
I am executing that client script on the cmdb_ci_pc_hardware table
The hardware status and substatus fields are synched back to the appropriate asset fields and are working as expected. Could you help me understand how those mappings may be affecting the removal of an option from a choice list?
I just tested the behavior in my PDI and the option is removed as expected with a simple
g_form.removeOption('hardware_substatus', 'in_use');
different option but same concept. Guess I just have to keep experimenting until I figure out what I have done that is conflicting with this removal.
Daniel

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 07:08 PM
Asset-CI Hardware Status Mapping is used to map status or substate from asset to cmdb or hardware table. OOTB there is only one which is in_use and you might not be able to delete it since it's used by system.
I thought you were trying to remove pending_repair not in_use.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 12:35 AM
Mike,
In my production environment I am trying to remove 'pending_repair'.
When I tested in my PDI I was able to remove 'in_use', in spite of it being the only option. g_form.removeOption() doesn't delete the choice it simply "hides" it from the list, at least that's my understanding.
At this point I'm fairly certain I have something else interfering with my desired behavior. It doesn't appear to have anything to do with this being a dependent field based on my test in the PDI. I'm going to disable all my UI policies and comment out every statement on the client script until I find the culprit.
Appreciate all your thoughts on this.
Daniel

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 02:50 AM
It would seem the platform didn't like
g_form.setValue('hardware_status', 'in_stock');
in conjunction with
g_form.removeOption('hardware_status', 'start_retirement');
g_form.removeOption('hardware_status', 'retired');
g_form.removeOption('hardware_status', 'in_use');
g_form.removeOption('hardware_substatus', 'pending_repair');
I removed the setValue statement and 'pending_repair disappeared as expected. 'in_stock' remains as the default because it's the only option left for 'hardware_status' so I didn't need the setValue statement anyhow. But now I'm wondering if the 'pending_repair' function would have worked if 'in_stock' hadn't been the only value left.
Anyway, on to the next problem 🙂
Thanks again for all your thoughts on this (and all my other questions) Mike!
Daniel