clear fields when category changes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2017 03:07 PM
Hi All,
I'm working on Incident and have come across something I didn't think about. There are times when an incident is reported under the incorrect category and it needs to be corrected.
For example, there is an issue with a phone and the incident is reported as a hardware issue but in reality it's something to do with the phone line. In this case we want to change the category to the proper one but there is an issue. It is possible that the creator filled in required fields based on the original category and that is causing the UI Polices and Actions to be triggered making the form a big mess.
Is there a way to clear all fields when a category changes?
I'd need a script example if there is.
Psuedo Script in my head is:
If "Category" changes to anything, Clear all fields that have been populated
Please let me know if you have ideas or input or examples! thank you
Carl

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2017 03:16 PM
Hello Carl,
You can write an OnSubmit client script which will clear all the fields if the category is changed. This will trigger any time category value is modified so you may have to adjust the logic of category as per your req.
Script:
function onSubmit() {
//Type appropriate comment here, and begin script below
var cat = g_form.getControl('category');
if(cat.changed)
{
var gr = new GlideRecord('incident');
gr.get(g_form.getUniqueValue());
for(var cf in gr)
{
g_form.clearValue(cf);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2017 03:20 PM
Thank you for your reply! Will this script clear everything on the form? It wasn't mentioned at first but I'd hope not to lose things in the top section such as Requestor, Location, Short Description and Description.
I have a sectioned called "Categorization" Ideally it would only clear fields that are populated in that section.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2017 08:42 PM
I would look at an onChange client script to replace the onSubmit example above.
You can also selectively clear certain fields with the following:
//Clear the value of the 'location' field
g_form.setValue('location', '');

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2017 08:47 PM
Create an onChange script on category field and put this code in there.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var fieldArr=["caller_id","assignment_group"];//add comma separated field names which you want to clear here
for( var field in fieldArr){
g_form.clearValue(fieldArr[field]);
}
}