Client Script with Condition on 'Contains'

Sofija
Giga Expert

Hi All!

I was wondering if someone would be possibly able to help me with the following problem:

Description: I am trying to create a client script onChange on incident table that would show an alert based on specific conditions.

Conditions:

1) When on incident CI contains a business department (u_business_departments on cmdb_ci_appl table; this field is a list) of GTT (I would ideally like to use sys ID just to be accurate in case we modify group's name in the future) and priority changes to anything but critical - alert should appear.

2) When on incident priority is anything but critical and CI changes to one that contains GTT as business department.

It is basically two onChange client scripts. The tricky part I cant get right is calling a function that would look for GTT sys ID in list business departments field on cmdb_ci_appl table.

This is what I have so far for this client script:

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

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

          return;

    }

var incidentGTT = (newValue != 1 && cmdb_ci_appl_u_business_departments [CONTAINS FUNCTION] 'GTT_sys_id');

if (newValue == incidentGTT) {

  alert('alert text');

}

   

}

I would appreciate any ideas and suggestions!

Kind Regards,

Kamile

1 ACCEPTED SOLUTION

Script include Code



-----


var DepartmentAndPrioirty = Class.create();


DepartmentAndPrioirty.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  isEligibleDepartment: function(){



  var ci=this.getParameter('sysparm_ci');


  var kr=new GlideRecord('cmdb_ci_appl');


  if(kr.get(ci)){


  var deparments=kr.u_business_departments.toString();


  if(deparments.indexOf('221f3db5c6112284009f4becd3039cc9')>=0){ // I have hardcoded sys_id. Make sure you change this as you wanted.


  return true;


  }



  }


  return false;


  },





  type: 'DepartmentAndPrioirty'


});



-----



Script Include Screenshot;



Screen Shot 2015-08-19 at 10.04.33 pm.JPG





Onchange Client Script:



------



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


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


          return;


    }


var ci=g_form.getValue('cmdb_ci');




  var ga = new GlideAjax('DepartmentAndPrioirty');


ga.addParam('sysparm_name','isEligibleDepartment');


ga.addParam('sysparm_ci',ci);


ga.getXML(callBackForAlert);


}


function callBackForAlert(response) {


var priority=g_form.getValue('priority');


var answer = response.responseXML.documentElement.getAttribute("answer");


  if(answer=='true' && priority=='1')


    alert("Yes alert finally");


}



----


On change Client Script:



Screen Shot 2015-08-19 at 10.05.47 pm.JPG


View solution in original post

5 REPLIES 5

coryseering
ServiceNow Employee
ServiceNow Employee

Hi kamile,



For some specific "working with GlideAjax" tips, see here:


ServiceNow Scripting 101: Client-Server Communication - Cloud Sherpas


GlideAjax Archives - ServiceNow Guru


https://books.google.com/books?id=6KK_CQAAQBAJ&pg=PA125&lpg=PA125&dq=using+glideajax&source=bl&ots=N...



You'll want to triple-check your spelling and remember:


  • new GlideAjax("scriptIncludeName");
  • it's addParam but the string passed in starts with "sysparm_"
  • getXML() is asynchronous and requires a callback function
  • getXMLWait() is not and does not.
  • You probably want to store the results of the call locally, in case the user changes the field multiple times and you can cut out some GlideAjax calls by checking to see if you've already queried for that value
  • Script Includes for GlideAjax must extend AbstractAjaxProcessor, and have the "client callable" checkbox checked.


You should probably also load the *current* CI's business departments into the scratchpad onLoad of the form (using a Display business rule) so that you can cut out any GlideAjax requests int he case where a user loads the form, it has an existing CI that meets your criteria, and they change the priority. If you have pre-loaded those values, you don't have to do a round-trip to the server to find out if the priority change is OK.



Thanks


Cory