Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Using Client script onchange

Genny Rippa
Kilo Explorer

Hi,

 

I need take in relation twoo table cmdb_ci_business_app and cmdb_ci_vmware_instance by a client script creation on change item "IT application owner" on table  cmdb_ci_business_app.

 

everytime change value about Item "IT application owner" I need update the item owner with same value on table cmdb_ci_vmware_instance for each recods with cmdb_ci_business_app.name=mdb_ci_vmware_instance.application

 

I develope this code in first istance but dont work:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
       if (isLoading || newValue === '') {
       return;
}
//Type appropriate comment here, and begin script below
var cat = new GlideRecord("cmdb_ci_business_app");
var vm_rec = new GlideRecord("cmdb_ci_vmware_instance");
vm_rec.query();
cat.query();
while (cat.next()) {
        while (vm_rec.next()) {
            if (vm_rec.u_application == cat.name || cat.it_application_owner==newValue) {

                vm_rec.u_owner=newValue;

    }
    vm_rec.update();

  }
  cat.update();
  }
}

Could you supporto me?

 

thanks a lot

Genny

7 REPLIES 7

Nagaraj Tofin5
Tera Expert

Hi Genny,

To update records from client scripts, please use GlideAjax in client script and call client callable script include.

In that script include perform the update operation.

Please mark reply as Helpful/Correct, if applicable. Thanks!

Regards,
Nagaraj Tofin

Afrith Shariff
Tera Guru

var ga = new GlideAjax('className');

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

ga.addParam('sysparm_user_name',"parameter1");

ga.getXML(HelloWorldParse);

function HelloWorldParse(response) {

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

    alert(answer);

}

this is the syntax of glide ajax. we use glide ajax to call server-side scripting. we can do database operations only on server side.

Genny Rippa
Kilo Explorer

sorry I need update owner items on table cmdb_ci_vmware_instance only when change It_application_owner items on talbe cmdb_ci_business_app

why I can't use clien script onchange? 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (isLoading || newValue === '') {
  return;
}
//Type appropriate comment here, and begin script below
var cat = new GlideRecord("cmdb_ci_business_app");
var vm_rec = new GlideRecord("cmdb_ci_vmware_instance");
vm_rec.query();
cat.query();
while (cat.next()) {
  while (vm_rec.next()){
   if (vm_rec.get(u_application) == cat.get(name) || cat.get(it_application_owner)==newValue ) {

     vm_rec.u_owner=newValue;

  }
   vm_rec.update();

  }
 cat.update();
 }

}

 

thanks a lot for support

 

Genny

GlideRecord in Client script is not an ideal practice. Also, for your case what if user mistakenly selects something as a value if you use GlideRcord in client script it will udpate in database. Now user corrects it again & again it will update. So, unnecessary actions are performed which need not be audited as well.

Why not use Business Rule to make this work?