Set visible or set display false of some variables on SC
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2023 07:30 AM
Hi All,
I have the below "On change" client script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var prod = g_form.getReference('product', myFunc);
//alert(JSON.stringify(prod));
//alert(prod.u_application_name);
function myFunc(prod){
var ga = new GlideAjax('abcCheckDevProdCIenv');
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);
if(newValue != ''){
//var res1 = res.toString();
if(res.includes('Development')){
g_form.setVisible('column1',true);
g_form.setVisible('column2',true);
g_form.setVisible('column3',true);
}
if(res.includes('Production')){
g_form.setVisible('column2',true);
g_form.setVisible('column3',true);
}
}
}
}
}
The script include returns output : "Development , Production"
Script include
var getAppname = this.getParameter('sysparm_app_name');
var getsysid = this.getParameter('sysparm_sysid');
var grgetCI = new GlideRecord('cmdb_ci_service');
grgetCI.addEncodedQuery('u_application_name!=NULL');
grgetCI.addQuery('u_application_name',getAppname);
grgetCI.query();
var CIlist = "";
while(grgetCI.next()){
if(CIlist == ''){
CIlist = grgetCI.used_for.toString();
}
else
{
CIlist = CIlist + ", "+ grgetCI.used_for.toString();
}
}
var answer = CIlist;
//JSON.stringify(answer);
return JSON.stringify(answer);
//return answer;
I have the following issues and need help with the same
1. It works as expected on Native UI but not on Portal
2. The hide/show functionality works however when the field is changed the older variables still appears irrespective of the condition. if the form is refreshed it works as expected. but not without loading
@Jaspal Singh @Sandeep Rajput @Tai Vu @Samaksh Wani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2023 08:07 AM
Hi,
for 1. set the UI Type value to "All". for 2. I don't see any 'g_form.setValue([field], [value]);"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2023 10:08 AM
Thanks for your reply.
I am not setting any value. I am trying to make the field visible accordingly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2023 03:55 PM
Is the field the Client Script is defined on a Reference field to the 'cmdb_ci_service' table? Your script include is not using that sys_id value for newValue provided to the client script, in the table query. Your table query only uses 'u_application_name'. and you're missing the script include function declaration of 'getCI' in your post. The complete script include may help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2023 11:01 AM
Hi @babarat1
Let's check the UI Type in your Client Script. Make sure it set to All.
Also, your Client Script make nested async, which might cause to unexpected behaviors.
Let's try to adjust as sample below.
//Client Callable Script Include
getCI: function() {
var productID = this.getParameter('sysparm_product_id');
var applicationName = this.getAppicationNameByProductID(productID);
var grService = new GlideRecord('cmdb_ci_service');
grService.addEncodedQuery('u_application_name!=NULL');
grService.addQuery('u_application_name', applicationName);
grService.query();
var CIlist = [];
while (grService.next()) {
CIlist.push(grService.used_for.toString());
}
return JSON.stringify(CIlist);
},
getAppicationNameByProductID: function(product_id) {
var grApp = new GlideRecord('<product_table_name>'); //replace your product table
if (grApp.get(product_id)) {
return grApp.getValue('u_application_name');
}
return '';
},
//Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var productID = g_form.getValue('product');
var ga = new GlideAjax('abcCheckDevProdCIenv');
ga.addParam("sysparm_name", "getCI");
ga.addParam("sysparm_product_id", productID);
ga.getXMLAnswer(function(response) {
if (response.indexOf('Development') >= 0) {
g_form.setVisible('column1', true);
g_form.setVisible('column2', true);
g_form.setVisible('column3', true);
}
if (response.indexOf('Production') >= 0) {
g_form.setVisible('column2', true);
g_form.setVisible('column3', true);
}
});
}
Double check this point as well. You're triggering OnChange on a variable that you're not using it.
=> ga.addParam("sysparm_sysid", newValue);
Cheers,
Tai Vu