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

Hi Genny,

>why I can't use clien script onchange? 

Simply put, there's no .update() method in Client side GlideRecord().

https://developer.servicenow.com/dev.do#!/reference/api/rome/client/c_GlideRecordClientSideAPI?navFi...

Update is used to update a record in a database. Database resides on a server. That's why update must be done on a server-side and not on client-side.

FYI. .update() documentation.

https://developer.servicenow.com/dev.do#!/reference/api/rome/server_legacy/c_GlideRecordAPI#r_GlideR...

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi again,

>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

To satisfy this condition, it's better to create a business rule because business rule is able to execute every time a field value changes on a table. That is, if a user changes value of "IT Applicaiton owner" from the list view, business rule will also execute and update the cmdb_ci_vmware record.

Following steps will create a business rule.

  1. From Application Navigator, go to "System Definition" > "Business Rules".
    find_real_file.png
  2. Click the "New" button
    find_real_file.png
  3. Set "Table" to "Business Application" and check "Insert" and "Update".
    Set Filter Condition to "Name", "changes"
    find_real_file.png
  4. Check "Advanced" checkbox and select "Advanced" tab and enter script to update cmdb_ci_vmware_instance record.
    find_real_file.png
    (function executeRule(current, previous /*null when async*/) {
    
    	var grVm = new GlideRecord("cmdb_ci_vmware_instance");
    	grVm.addActiveQuery();
    	grVm.addQuery('u_application', current.name);
    	grVm.query();
    	if (grVm.next()) {
    		grVm.u_owner = current.it_application_owner;
    		grVm.update();
    	}
    
    })(current, previous);​

Genny Rippa
Kilo Explorer

hi

 

thanks for support I'm checking then what I select as "When" and "Order" default is right?

then I select item IT Application owner bacause I m interested at this change value item as showm belown:

 

find_real_file.png