client script on Parent table should not run on few extending classes

Servicenow use1
Tera Contributor

 Hello,

we have a requirement where we need to populate fields on a table (CI class) based on one reference field (u_model) that refers to CMDB Model table.

Client script: (Written on Parent table 'Line' that extends cmdb_ci)

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var model = g_form.getValue('u_model');
var ga = new GlideAjax('modeldetails');
ga.addParam('sysparm_name','requestor_info');
ga.addParam('sysparm_user_name',model);
ga.getXML(autopopulate);


function autopopulate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var ans = answer.evalJSON();
for (var k = 0; k < ans.length; k++) {
g_form.setValue('manufacturer',ans[k].manuf);
g_form.setValue('u_base_option',ans[k].baseopt);
g_form.setValue('u_short_name',ans[k].shtname);
g_form.setValue('u_license_name',ans[k].licname);
g_form.setValue('u_license_number',ans[k].licnum);
g_form.setValue('u_name',ans[k].name);
g_form.setValue('key',ans[k].key);
g_form.setValue('u_licensable',ans[k].lic);
}
}
}

Script include:

var modeldetails = Class.create();
modeldetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

requestor_info: function() {
var arr=[];
var output='';
var json = new JSON();
var details=this.getParameter('sysparm_user_name');
var model= new GlideRecord('cmdb_model');
model.addQuery('sys_id',details);
model.query();
while(model.next())
{
var obj = {};
obj.manuf=model.manufacturer.toString();
obj.baseopt=model.u_base_option.toString();
obj.shtname=model.u_short_name.getDisplayValue();
obj.licname=model.u_license_name.getDisplayValue();
obj.licnum=model.u_license_number.getDisplayValue();
obj.name=model.u_name.getDisplayValue();
obj.key=model.u_r_d_key.getDisplayValue();
obj.lic=model.u_licensable.getDisplayValue();

arr.push(obj);
}
gs.log('value is :'+ json.encode(arr));
return (json.encode(arr));
},
type: 'modeldetails'
});

It is working fine but my questions are

1. how to restrict it to only certain extending classes. for example, It should work for 'group' (u_group) and 'component' (u_component) classes that extend 'Line' , but not for other 'Line' extending class namely 'u_version'. ie, on u_version, fields should not be populated based on u_model.

 2.When model is selected, and after saving the form, few fields should become read-only while others are      editable on CIs.

Could anyone help me on this please?

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

You can only apply for all the child/extended tables such thing. Sure you can add "allowed tables" list in there and use gs.getTableName() to check if you are in allowed table but it's not what you asked for i believe)

So it would look like this:

var allowed = ['u_cmdb_one', 'u_cmdb_ci_second', <etc...>];

var au = new global.ArrayUtil();

if(!au.contains(allowed, current.getTableName()){

    return;

}

 

find_real_file.png

View solution in original post

2 REPLIES 2

Community Alums
Not applicable

You can only apply for all the child/extended tables such thing. Sure you can add "allowed tables" list in there and use gs.getTableName() to check if you are in allowed table but it's not what you asked for i believe)

So it would look like this:

var allowed = ['u_cmdb_one', 'u_cmdb_ci_second', <etc...>];

var au = new global.ArrayUtil();

if(!au.contains(allowed, current.getTableName()){

    return;

}

 

find_real_file.png

Thanks Joro,

It helped. 

It worked when the following code is used

var table = g_form.getTableName();
if (table == 'u_line' || table == 'u_group'|| table == 'u_cmdb_ci_product') in the onchange client script.