Using Client script onchange
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2022 03:57 AM
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
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2022 04:11 AM
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!
Nagaraj Tofin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2022 04:20 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2022 04:32 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2022 05:04 AM
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?
