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.

way to create script include

demoutah
Tera Contributor

In the Catalog items i have a field name property (ref to sys_properti and list collector )

and another variable --> value

 

using the script inlcude what i select in the property field --> that particular value should be seen in the value field.

but the value of the particular property is not coming. what is wrong with the script?

 

catalog client script:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('getPropertyValue');
ga.addParam('sysparm_name','getRecords');
ga.addParam('sysparm_groupId',newValue); //Parameters
ga.getXML(cb);


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

g_form.setValue('value', answer);

}
}

 

Script include:

 

var getPropertyValue = Class.create();
getPropertyValue.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRecords:function()
{
var groupSysId = this.getParameter('sysparm_groupId');
gs.addInfoMessage(groupSysId);
var obj = {};

var grSysUser = new GlideRecord('sys_properties');
grSysUser.addQuery('name', groupSysId);
grSysUser.query();


while (grSysUser.next()) {

obj[grSysUser.name.value.getDisplayvalue()] = grSysUser.name.toString();

}
gs.addInfoMessage(grSysUser.value);
return JSON.stringify(obj);
},

type: 'getPropertyValue'
});

 

i need the particular property value should be displayed in JSON format

@jaheerhattiwale 

 

1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hello @demoutah 

 

Adjusted your code little bit.Highlighted in Bold and Green

 

Catalog client script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('getPropertyValue');
ga.addParam('sysparm_name','getRecords');
ga.addParam('sysparm_groupId',newValue); //Parameters
ga.getXMLAnswer(cb);


function cb(answer) {
//var answer = response.responseXML.documentElement.getAttribute("answer");

var result = JSON.parse(answer);

g_form.setValue('value', result.value);

}
}

 

Script include:

 

var getPropertyValue = Class.create();
getPropertyValue.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRecords:function()
{
var groupSysId = this.getParameter('sysparm_groupId'); // as this is a list collector
gs.addInfoMessage(groupSysId);
var obj = {

name:'',

value:'',

};

var grSysProp = new GlideRecord('sys_properties');
grSysProp.addEncodedQuery('sys_idIN'+ groupSysId); 
grSysProp.query();


while (grSysProp.next()) {
//obj[grSysUser.name.value.getDisplayvalue()] = grSysUser.name.toString();

obj.name = grSysProp.getDisplayValue().toString(); // optional if you want name of property also

obj.value = grSysProp.getValue('value').toString();


}
//gs.addInfoMessage(grSysUser.value);
return JSON.stringify(obj);
},

type: 'getPropertyValue'
});

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

3 REPLIES 3

jaheerhattiwale
Mega Sage

@demoutah The exact same question has been answered in below thread

 

https://www.servicenow.com/community/developer-forum/using-background-script-issue/m-p/2480479/emcs_...

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Vishal Birajdar
Giga Sage

Hello @demoutah 

 

Adjusted your code little bit.Highlighted in Bold and Green

 

Catalog client script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('getPropertyValue');
ga.addParam('sysparm_name','getRecords');
ga.addParam('sysparm_groupId',newValue); //Parameters
ga.getXMLAnswer(cb);


function cb(answer) {
//var answer = response.responseXML.documentElement.getAttribute("answer");

var result = JSON.parse(answer);

g_form.setValue('value', result.value);

}
}

 

Script include:

 

var getPropertyValue = Class.create();
getPropertyValue.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRecords:function()
{
var groupSysId = this.getParameter('sysparm_groupId'); // as this is a list collector
gs.addInfoMessage(groupSysId);
var obj = {

name:'',

value:'',

};

var grSysProp = new GlideRecord('sys_properties');
grSysProp.addEncodedQuery('sys_idIN'+ groupSysId); 
grSysProp.query();


while (grSysProp.next()) {
//obj[grSysUser.name.value.getDisplayvalue()] = grSysUser.name.toString();

obj.name = grSysProp.getDisplayValue().toString(); // optional if you want name of property also

obj.value = grSysProp.getValue('value').toString();


}
//gs.addInfoMessage(grSysUser.value);
return JSON.stringify(obj);
},

type: 'getPropertyValue'
});

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

@Vishal Birajdar @jaheerhattiwale 

Catalog client script:

function onChange(control, oldValue, newValue, isLoading) {   if (isLoading || newValue == '') {
      return;
   }
var prop =g_form.getValue('property');
var gaINCUsers = new GlideAjax('testingPropertyValue');
gaINCUsers.addParam('sysparm_groupId', prop);
gaINCUsers.addParam('sysparm_name''getprop');
gaINCUsers.getXMLAnswer(callback);
function callback(answer) {
var results = JSON.parse(answer);
g_form.setValue('propertyvalue','value= ' + results.value + ' ' + 'Name = ' + results.name);
}   
}
 
Script Include:
 
var testingPropertyValue = Class.create();
testingPropertyValue.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getprop: function() {
    var users ={
        Name : '' ,
        value : ''
    };
    var groupSysId = this.getParameter('sysparm_groupId');
    var grSysUser = new GlideRecord('sys_properties');
    grSysUser.addEncodedQuery('sys_idIN' + groupSysId );
    grSysUser.query();
    while (grSysUser.next()) {
        users.Name =grSysUser.getValue('name').toString();
    users.value = grSysUser.getValue('value').toString();
    //users.Name =grSysUser.getValue('name').toString();
    return JSON.stringify(users);
    }
    },
    type: 'testingPropertyValue'
});
 
1720a.jpg
 
I am not getting the name of the selected property name, and if i select more property means i need all the property should be come with value in JSON format .how to get that?