OnChange Client Script - Is there a more efficient way to script this

Damian Mudge
Tera Expert

My first attempt at scripting, so be gentle!

 

The business scenario is we want to identify the the Operational Teams area based on the "Category" selected. I am testing ths in my PDI and I have created both a new Choice and String field to capture the output. (I was not sure which was the best type of field).

 

I have written the following "OnChange" script to populate either one of these fields with a value based on the Category selected. I have removed a number of Categories, and wondered if there is a more efficient syntax.

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

// If the user changes the value of the "Category" field,
// set the default value for the "Type of Service" field

if (g_form.getValue('category')) {
 
if (newValue == '#####_ba' || newValue == '#####_cd' || newValue == '#####_edr' || newValue == '#####_mdr' || newValue == '#####_sdr' || newValue == '#####_ti' || newValue == '#####_tprm' || newValue == '#####_vi') {
g_form.setValue('u_type_of_service','1','SOC');
} else if (newValue == 'network'|| newValue == 'firewall' || newValue == 'wifi'|| newValue == 'lam') {
g_form.setValue('u_type_of_service','2','NOC');
} else if (newValue == 'database' || newValue == 'hardware'|| newValue == 'software' || newValue == 'other') {
g_form.setValue('u_type_of_service','3','Internal');
} else if (newValue == 'Sentinel' || newValue == 'Sentinel') {
g_form.setValue('u_type_of_service','4','Platform');
}
g_form.setReadOnly('u_type_of_service',true);
}
}
3 ACCEPTED SOLUTIONS

Brad Bowman
Kilo Patron
Kilo Patron

Your script looks fine, and is what needs to be done in scenarios like this.  An alternate approach to consider is using the Switch statement

https://www.w3schools.com/js/js_switch.asp 

One is not necessarily better or more efficient than the other, but you may prefer coding the case/switch format, or reading it when you or someone else comes back to update this script later.

 

Assuming you are running this onChange of Category, you don't need 

if (g_form.getValue('category')) {

since the very first if statement stops the script from running if the field was changed to blank/empty.

View solution in original post

Akshay Gupta2
Kilo Sage

Hi @Damian Mudge ,

 

You are correct with onchange client script.

I have just modified the code for you. This should just work fine for your use case.

You can replace client script with this snippet.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var categoryMapping = {
        '#####_ba': 'SOC',
        '#####_cd': 'SOC',
        '#####_edr': 'SOC',
        '#####_mdr': 'SOC',
        '#####_sdr': 'SOC',
        '#####_ti': 'SOC',
        '#####_tprm': 'SOC',
        '#####_vi': 'SOC',
        'network': 'NOC',
        'firewall': 'NOC',
        'wifi': 'NOC',
        'lam': 'NOC',
        'database': 'Internal',
        'hardware': 'Internal',
        'software': 'Internal',
        'other': 'Internal',
        'Sentinel': 'Platform'
    };

    var typeMapping = {
        'SOC': '1',
        'NOC': '2',
        'Internal': '3',
        'Platform': '4'
    };

    var typeOfService = categoryMapping[newValue];
    if (typeOfService) {
        g_form.setValue('u_type_of_service', typeMapping[typeOfService], typeOfService);
        g_form.setReadOnly('u_type_of_service', true);
    }
}

 

 

Please mark this as solution and helpful.

 

Thanks 

Akshay

View solution in original post

Hi @Damian Mudge - You would typically make the field visible, set its value, then hide it. It is possible to use GlideAjax, however, in most cases that would not be the most appropriate or efficient method.

While GlideAjax can be used to update the value of a hidden field, it involves an additional server call, which can be less efficient and more complex compared to handling the field visibility and value setting directly on the client side. GlideAjax is more appropriate for scenarios where server-side processing is required beyond simple field value setting.

View solution in original post

9 REPLIES 9

Brad Bowman
Kilo Patron
Kilo Patron

Your script looks fine, and is what needs to be done in scenarios like this.  An alternate approach to consider is using the Switch statement

https://www.w3schools.com/js/js_switch.asp 

One is not necessarily better or more efficient than the other, but you may prefer coding the case/switch format, or reading it when you or someone else comes back to update this script later.

 

Assuming you are running this onChange of Category, you don't need 

if (g_form.getValue('category')) {

since the very first if statement stops the script from running if the field was changed to blank/empty.

Thank you Brad for this. It worked perfectly.

 

As I mentioned to  Asjkay further down the thread, the field 'u_type_of_service' field has to be visible on the Incident form in order for this to work. When I add it to the form it works perfectly, but when I remove it, it does not update. Is there something else I should be doing so that the 'u_type_of_service' field is updated behind the scenes so to speak. 

The field has to be added to the form in order for g_form to set the value in a Client Script, but it will still update the value even if the Visible or Display attribute is set to false, and/or Read only is true, so if you don't want the u_type_of_service field to be displayed on the form, you can hide it in an onLoad Client 
Script or UI Policy, and the onChange script should still work fine. 

Akshay Gupta2
Kilo Sage

Hi @Damian Mudge ,

 

You are correct with onchange client script.

I have just modified the code for you. This should just work fine for your use case.

You can replace client script with this snippet.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var categoryMapping = {
        '#####_ba': 'SOC',
        '#####_cd': 'SOC',
        '#####_edr': 'SOC',
        '#####_mdr': 'SOC',
        '#####_sdr': 'SOC',
        '#####_ti': 'SOC',
        '#####_tprm': 'SOC',
        '#####_vi': 'SOC',
        'network': 'NOC',
        'firewall': 'NOC',
        'wifi': 'NOC',
        'lam': 'NOC',
        'database': 'Internal',
        'hardware': 'Internal',
        'software': 'Internal',
        'other': 'Internal',
        'Sentinel': 'Platform'
    };

    var typeMapping = {
        'SOC': '1',
        'NOC': '2',
        'Internal': '3',
        'Platform': '4'
    };

    var typeOfService = categoryMapping[newValue];
    if (typeOfService) {
        g_form.setValue('u_type_of_service', typeMapping[typeOfService], typeOfService);
        g_form.setReadOnly('u_type_of_service', true);
    }
}

 

 

Please mark this as solution and helpful.

 

Thanks 

Akshay