onChange client script with templates and dependent choice lists

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2014 02:26 PM
I have a couple of onChange client scripts that run when the category and subcategory of an incident change.
What I am needing to do is exclude these from running when a template changes either one of these fields.
It looks as if an onChange client script set to a dependent choice list will fire twice, once when the parent choice is made (I assume by the setting of the choices) and once when the field itself is changed.
Because of this, I am only able to stop one 'cycle' of the script by using the isTemplate check.
How can I stop the client script from running the second time on the subcategory field when a template is used?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2014 01:47 PM
The check for isTemplate was added to both in the beginning. Still fires the subcategory script once.
If you create a simple onChange script only on the subcat field, and leave out the isTemplate, it actually runs twice when trying to set both category and subcategory. My theory is that it triggers whenever the choice list it is dependent upon (in this case category) sets the choices, even if the value does not change (stays at --None--).
Just trying to find away around that.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2014 06:35 AM
Here is my testing script I have used in a demo site with the same result.
Type is onChange
Table is Incident
Field name is Subcategory
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '' || isTemplate) {
return;
}
alert("subcat fired");
}
If I then set a template that sets the category and subcategory, I receive the alert.
If I only set the subcategory in the template, it works fine (I do not receive the alert).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2014 01:59 PM
Checked with support, and this is what they said...
This is expected behavior, as the change handler for the subcategory field will fire due to the change in category. You might want to work around this by changing the onChange script written for the subcategory field.
No indication of how I might work around the issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2014 04:58 AM
There is one way but it isn't pretty.
Have a client script onChange(category), or use one existing to set a global parameter
// If a template is applied save it on the scratchpad and abord running this client script
if(isTemplate){
g_scratchpad.isTemplate=true;
return;
}
Then use the following on your client script onChange(subcategory)
// Do not run if a template is applied
if (isTemplate || g_scratchpad.isTemplate){
g_scratchpad.isTemplate=false;
return;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2015 09:43 PM
here is a script that another used when dealing with templates:
For anyone else who may need it the below client script will apply a template to an incident if a value is selected, but will not reapply if changed.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') { //if loading or new value is blank, do nothing
return;
}
if (oldValue == 6 || oldValue == 7 || oldValue == 😎 {
return;
}
if (newValue == 6 || newValue == 7 || newValue == 😎 //if new value is 6,7 or 8
if (confirm('Set the Priority as a Major Incident?')){
g_form.setValue('u_major_incident',true);
var grTemplate = new GlideRecord('sys_template');
grTemplate.addQuery('name','* Major Incident Template');
grTemplate.query(templateResponse);
function templateResponse(template) {
if (template.next()) {
applyTemplate(template.sys_id);
}
}
}
}