Onchange client script with script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 09:45 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 10:22 PM
what's your requirement?
Is it not working? are you trying to optimize the code?
you can send as many values you want to ajax function using the parameter, json is not required ideally
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 10:40 PM
Can u help me with the code. It is not working
optimize the code as possible.
I want the company value in the client script for service and ci.
And restrict based on the company value and set values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 10:30 PM
Hello @karthik65
You can send 2 parameters using two values not in the array of object.
And as I can see in your Script Include you have written - return JSON.stringify(Value);
just you need to change-
return JSON.stringify(value);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 10:47 PM
Hello @karthik65
Use this-
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var var1 = g_form.getValue('business_service');
var var2 = newValue;
var Value = new GlideAjax('global.Brazil_Script');
Value.addParam('sysparm_name', 'GetService');
Value.addParam('sysparm_service_name', var1);
Value.addParam('sysparm_cmdb_name', var2);
Value.getXMLAnswer(setVal);
function setVal(response){
var res = JSON.parse(response);
var ServiceCompany = res.var1;
var ApplCompany = res.var2;
alert(ServiceCompany);
alert(ApplCompany);
var OPCO;
getMessage('BrazilsysID', function(msg) {
OPCO = JSON.parse(msg);
if (ServiceCompany == OPCO.Company1 || ServiceCompany == OPCO.Company2) {
var sername = ser.name;
var cat = 'BR_' + sername; //concat
g_form.setValue('category', cat);
g_form.setReadOnly('category', true);
}
if (ApplCompany == OPCO.Company3 || ApplCompany == OPCO.Company1) {
var ciname = ci.name;
var subcat = 'BR_' + ciname; //concat
var setTimer = '';
setTimer = setTimeout(setTimerDelay, 2000); //To set the timer for 2 sec
function setTimerDelay() {
g_form.setValue('subcategory', subcat);
g_form.setReadOnly('subcategory', true);
}
}
});
}
}
Script include:
var Brazil_Script = Class.create();
Brazil_Script.prototype = {
GetService: function() {
var value={};
var str1 = this.getParameter('sysparm_service_name');
var str2 = this.getParameter('sysparm_cmdb_name');
var arr = new JSON().decode(str);
var Service = new GlideRecord('cmdb_ci_service');
Service.addQuery("name", str1);
Service.query();
while (Service.next()) {
value.var1 = Service.company.toString();
}
var cmdb = new GlideRecord('cmdb_ci_appl');
cmdb.addQuery("name", str2);
cmdb.query();
while (cmdb.next()) {
value.var2 = cmdb.company.toString();
}
return JSON.stringify(value);
},
type: 'Brazil_Script'
};
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.