Script Include and On change Client script using JS methods browser issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2023 05:21 AM - edited 10-31-2023 05:22 AM
Hi All,
I have a script include that returns the following result exactly as shown below with comma
"Development , Production"
Client Script (not working on Portal)
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setVisible('column1',false);
g_form.setVisible('column2',false);
g_form.setVisible('column3',false);
return;
}
var prod = g_form.getReference('product');
//alert(JSON.stringify(prod));
//alert(prod.u_application_name);
var ga = new GlideAjax('<scriptincludename>');
ga.addParam("sysparm_name", "getCI");
ga.addParam("sysparm_app_name", prod.u_application_name);
ga.addParam("sysparm_sysid", newValue);
//alert(newValue);
//alert(newValue.getDisplayValue());
ga.getXML(getResponse);
function getResponse(response) {
var res = response.responseXML.documentElement.getAttribute("answer");
alert(res.toString());
//var res1 = res.toString();
if(res.includes('Development')){
g_form.setVisible('column1',true);
g_form.setVisible('column2',true);
}
if(res.includes('Production')){
g_form.setVisible('column2',true);
g_form.setVisible('column3',true);
}
}
}
"
Now I need to have a client script to check the matches of the above returned results which is Dev/ Prod. I have the below script which works on Native UI but not on the Portal. It displays an error "There is a JavaScript error in your browser console "
I need help to make it compatible and make it work on the Portal as expected.
@Danish Bhairag2 @Sandeep Rajput @Ankur Bawiskar @Peter Bodelier @Chuck Tomasi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2023 05:58 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2023 05:37 AM
@DPrasna ,
Also g_form.getReference does not work in Service Portal.
Please refer this article on how to use it on portal
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2023 05:37 AM
Ensure UI type is ALL and use getXMLAnswer
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setVisible('column1',false);
g_form.setVisible('column2',false);
g_form.setVisible('column3',false);
return;
}
var prod = g_form.getReference('product');
//alert(JSON.stringify(prod));
//alert(prod.u_application_name);
var ga = new GlideAjax('<scriptincludename>');
ga.addParam("sysparm_name", "getCI");
ga.addParam("sysparm_app_name", prod.u_application_name);
ga.addParam("sysparm_sysid", newValue);
ga.getXMLAnswer(function(answer){
var res = answer;
alert(res.toString());
//var res1 = res.toString();
if(res.includes('Development')){
g_form.setVisible('column1',true);
g_form.setVisible('column2',true);
}
if(res.includes('Production')){
g_form.setVisible('column2',true);
g_form.setVisible('column3',true);
}
});
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2023 05:49 AM
Hi Ankur,
I tried the same using "
getXMLAnswer
However the error remains the same
"

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2023 01:12 AM - edited 11-02-2023 01:17 AM
Hi @DPrasna,
Do you have logging in your script include as well?
Does it complete up until the return?
As mentioned before, getReference doesn't work with a callback but I haven't seen a corrected script here.
Try this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setVisible('column1', false);
g_form.setVisible('column2', false);
g_form.setVisible('column3', false);
return;
}
var prod = g_form.getReference('product', getRef);
function getRef(prod) {
//alert(JSON.stringify(prod));
//alert(prod.u_application_name);
var ga = new GlideAjax('<scriptincludename>');
ga.addParam("sysparm_name", "getCI");
ga.addParam("sysparm_app_name", prod.u_application_name);
ga.addParam("sysparm_sysid", newValue);
//alert(newValue);
//alert(newValue.getDisplayValue());
ga.getXML(getResponse);
}
function getResponse(response) {
var res = response.responseXML.documentElement.getAttribute("answer");
alert(res.toString());
//var res1 = res.toString();
if (res.includes('Development')) {
g_form.setVisible('column1', true);
g_form.setVisible('column2', true);
}
if (res.includes('Production')) {
g_form.setVisible('column2', true);
g_form.setVisible('column3', true);
}
}
}
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.