business rule

Jeff W NZAO B
Mega Guru

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

JeffWNZAOB_0-1697120881856.png

 

Thanks for your feedback

 

 

1 ACCEPTED SOLUTION

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);
   }
}​

 

View solution in original post

9 REPLIES 9

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);
   }
}​

 

Thank you very much, this worked. 😀

You are welcome!

@Jeff W NZAO B Could you please answer a couple of questions.

1. On which table you are applying this client script, (it would be great if you could post a screenshot of the client script (capturing the entire form))

2. On which field this client script works, it would be great if you could post a snapshot of the form highlighting the field onChange of which this client script is getting called.

3. What is the name of the field which is having a reference of Application (cmdb_ci_appl) table on the form.

 

Once we have these answers, we will be able to suggest the corrections on your script.

 

 

1. On which table you are applying this client script, (it would be great if you could post a screenshot of the client script (capturing the entire form)) the table is Application (cmdb_ci_appl) 

 

2. On which field this client script works, it would be great if you could post a snapshot of the form highlighting the field onChange of which this client script is getting called.

The script works on the field Alert Application (alert_application)

 

3. What is the name of the field which is having a reference of Application (cmdb_ci_appl) table on the form. The field referenced is Application field (appl_request_access_choice)

 

Let me know if it's enough for you