- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 07:28 AM
Hi all,
I have create a BR 'cause I want to controle on my catalog it an application from the table Application has a true statut for its purchase. Purchase which is a column from the table Application
Here is my code, I'm not sure it's the best practice, I've tried Client script and script from UI Policy but found no solution.
(function executeRule(current, previous /*null when async*/) {
// Check the condition: If u_purchase_licence is 'true', set display to true
var application = g_form.getValue('appl_request_access_choice');
if (current.u_purchase_licence === 'false' || application === '') {
//g_form.setDisplay('alert_application', false);
gs.addInfoMessage('Business Rule is running for CI Application: ' + current.name);
} else {
// If the condition is not met, you can hide the field (optional)
//g_form.setDisplay('alert_application', true);
gs.addInfoMessage('Business Rule is running for CI Application: ' + current.name);
}
})(current, previous);
Table Application
Thanks for your feedback
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2023 04:35 AM - edited 10-18-2023 04:42 AM
Your if statement to evaluate the callback answer should refer to a field on the record of the reference table, so maybe more like
if ((caller.u_purchase_licence == 'true') || (list_appl != '')) {
also try without quotes around true. To clarify, is this a table record/form or a Catalog Item? Is u_purchase_licence the name of a reference field or variable on this form/item? This script is running onChange of which field/variable? From your descriptions it sounds like you are selecting an Application, which is a reference field/variable, and want to show/hide the alert_application field/variable based on the value of u_purchase_licence, which is on the table referenced by the Application field/variable. If that's the case, then Application needs to be in the getReference:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setDisplay('alert_application', false);
}
var caller = g_form.getReference('application', doAlert); // name of the application field/variable
}
function doAlert(caller) { // reference is passed into callback as first arguments
var list_appl = g_form.getValue('appl_request_access_choice');
if ((caller.u_purchase_licence == 'true') || (list_appl != '')) {
g_form.setDisplay('alert_application', true);
}
else {
g_form.setDisplay('alert_application', false);
}
}
If appl_request_access_choice is the name of the Application field/variable that is a reference to cmdb_ci_appl, then the script would look more like this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setDisplay('alert_application', false);
}
var caller = g_form.getReference('appl_request_access_choice', doAlert);
}
function doAlert(caller) { // reference is passed into callback as first arguments
if (caller.u_purchase_licence == 'true') {
g_form.setDisplay('alert_application', true);
}
else {
g_form.setDisplay('alert_application', false);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 11:20 AM
You cannot control field visibility with a Business Rule. Use a Client Script or UI Policy. Neither will hide the field on a List view, only the form/record view. If you want to control access on a list view you can try to create a read ACL for this field and table.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 11:22 AM
The following code is a client side code which will not work in a server side business rule script.
g_form.getValue('appl_request_access_choice');
if appl_request_access_choice is a field available on form then it can be accessed in the server side as follows
current.getValue('appl_request_access_choice');
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 12:50 PM
can you try this ?
--------------script include--------------------
getApplicationDetails: function() {
var id = this.getParameter('sysparm_userID');
var grBusapp = new GlideRecord('cmdb_ci_app');
grBusapp.addQuery('sys_id', id);
grBusapp.query();
var AppObj = {};
while (grBusapp.next()) {
AppObj.license = grBusapp.u_purchase_licence.toString();
AppObj.app = grBusapp.application.toString();
}
return JSON.stringify(AppObj);
},
-----------Onload--------------------
function onLoad() {
//Type appropriate comment here, and begin script below
var application = g_form.getValue('appl_request_access_choice');
var ga = new GlideAjax(" script inc Name");
ga.addParam("sysparm_name", "getApplicationDetails");
ga.addParam("sysparm_ID", application);
ga.getXML(userDet);
}
function userDet(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var res = JSON.parse(answer);
var app = res.application;
var license = res.u_purchase_licence;
if (license == 'false' || application == '') {
g_form.setDisplay('alert_application', false);
} else {
// If the condition is not met, you can hide the field (optional)
g_form.setDisplay('alert_application', true);
}
let me know if this was helpful.
thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2023 02:49 AM
Hi to all,
I decided to go with a client script, but I still not get the correct script to really control the display with the application chosen value with u_purchase_licence == true.
Here is my code:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setDisplay('alert_application', false);
}
//Type appropriate comment here, and begin script below
g_form.getReference('u_purchase_licence', doAlert); // doAlert is our callback function not cmdb_ci_app
}
function doAlert(caller) { // reference is passed into callback as first arguments
var list_appl = g_form.getValue('appl_request_access_choice');
if ((caller.getValue() == 'true') || (list_appl != '')) {
g_form.setDisplay('alert_application', true);
}
else {
g_form.setDisplay('alert_application', false);
}
}
If you can help me with this
Thanks