Help with client script for incident template

sharonjanisch
Tera Contributor

We want to have a client script that runs when our techs use a template to create an incident that will display a message when the CI field changes to a specific value.  

If I select Configuration Item as the field name and use this script, it does not work:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isTemplate || newValue == '') {
          return;
    }

    //Type appropriate comment here, and begin script below
    if (newValue == 'CI_Name')
  g_form.showFieldMsg('short_description','IMPORTANT:___','info');
}

If I use select SubCategory as the field name with this script, it does work:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isTemplate || newValue == '') {
          return;
    }

    //Type appropriate comment here, and begin script below
    if (newValue == 'SubCategory_Name')
  g_form.showFieldMsg('short_description','IMPORTANT:___','info');
}

Can someone tell me why this doesn't work with the CI field?

9 REPLIES 9

Chuck Tomasi
Tera Patron

Hi Sharon,



newValue represents a sys_id on the CI field. You are comparing against a string.



if (newValue == 'CI_Name')



should be more like



if (newValue == 'bc14b51f7f0000016c0893ec88be62da')



Also, if they are using a template, you may want to change:



if (isTemplate || newValue == '') {


          return;


    }



to



if (newValue == '') {


          return;


    }



The first one says "If you're using a template, abort and return". I thought you wanted this to display if you WERE using a template - or did I misunderstand.


Oops, forgot the obligatory reference: Client Scripts - ServiceNow Wiki


sharonjanisch
Tera Contributor

You're correct, we want the message to display only when the use the Incident template.  


I've changed the newValue to the sys_id of the CI but the message still does not appear.  
I've tried both scripts below.  



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


    //Type appropriate comment here, and begin script below
    if (newValue == 'd73fa4be0fdb164013ef46ace1050e65')
  g_form.showFieldMsg('short_description','IMPORTANT:___','info');
}



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


    //Type appropriate comment here, and begin script below
    if (newValue == 'd73fa4be0fdb164013ef46ace1050e65')
  g_form.showFieldMsg('short_description','IMPORTANT:___','info');
}


Hi Sharon,



How about adding a few more g_form.addInfoMessage() calls to ensure the script is at least running?



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


        g_form.addInfoMessage('Started script: isTemplate=' + isTemplate);
  if (!isTemplate || newValue == '') { // Changed to bail out if NOT using a template
          return;
    }


        g_form.addInfoMessage('newValue=' + newValue);


    //Type appropriate comment here, and begin script below
    if (newValue == 'd73fa4be0fdb164013ef46ace1050e65')
  g_form.showFieldMsg('short_description','IMPORTANT:___','info');
}