Call multiple functions in client script from script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2022 05:50 AM
I have two fields in a form. My requirement is, I've to get values selected in Business services (cmdb_ci_service) and environment(cmdb_ci_environment) field as shown below picture. Both the fields belong to different tables.
And show/populate them in another field named 'name of the account' as shown below in string format . For eg. "Business services_Environment" in this format.
For this I tried writing on change client script as below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ba = g_form.getValue('business_services');
var ga = new GlideAjax('GetName');
ga.addParam('sysparm_name', 'getBSName');
ga.addParam('sysparm_bs', ba);
var a = ga.getXML(set_form_values);
function set_form_values(xml) {
var answer = xml.responseXML.documentElement.getAttribute("answer");
g_form.setValue('name_of_the_account', answer);
}
Script include:
var GetName = Class.create();
GetName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getBSName: function() {
var bs = this.getParameter('sysparm_bs');
var bsGR = new GlideRecord('cmdb_ci_service');
bsGR.addQuery('sys_id', bs);
bsGR.query();
if (bsGR.next()) {
var return_string = "SVC_ " + bsGR.name;
return return_string;
}
},
getEnvName: function() {
var env = this.getParameter('sysparm_env');
var envGR = new GlideRecord('cmdb_ci_environment');
envGR.addQuery('sys_id', env);
envGR.query();
if (envGR.next()) {
return envGR.name;
}
}, type: 'GetName'
});
Using above scripts I'm only getting name of business service in "name of the account field". Need help in getting both business service and environment name.
- Labels:
-
Orchestration (ITOM)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2022 05:59 AM
Hi Ashwini,
As I see you have one field dependant based on two referenced field. So, you'll need to use two client script onChange to autopopulate that field based on the others behaviours. Or how your logic in the form should be? Are you going to have always both fields filled at the same time?
If it was helpful, please give positive feedback.
Thanks,
☆ Community Rising Star 22, 23 & 24 ☆
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2022 11:59 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2022 06:01 AM
Hi Ashwini
you can directly use below to get the displayvalues of refernce field if name is display value of the 2 tables.
you can paralelly add logic by creating another client script on sevice field to populate values accordingly.
//onchange on environment field.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(g_form.getValue('business_services') == '')
g_form.addInfoMessage("please enter business service");
if(window == null) //for portal
{
var bs= g_form.getDisplayValue('business_services');
var env = g_form.getDisplayValue('environment');
g_form.setValue(bs +"_"+env);
}
else{ //for non-portal
var bs= g_form.getDisplayBox('business_services').value;
var env = g_form.getDisplayBox('environment').value;
g_form.setValue(bs +"_"+env);
}
//Type appropriate comment here, and begin script below
}
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2022 02:26 AM
I tried this, but getDisplayValue/ getDisplayBox is not working on client script. You can see below. I need to populate the name/value selected in business service and environment field (both are reference type and belongs to different tables) in the "name of the account" field. It should be in "business service_environment" format.