Using Script for creating UI Policy

AadityaSahu
Tera Contributor

I have a requirement for a Catalog Item where I need to create a UI Policy that has the following condition:

Condition 1 : If User_Variable_Num >=250

and has the following UI Policy Action : Make User_Variable_Dependent visible and mandatory

 

Where User_Variable_Num is a Catalog Variable of SingleLine Text type, where the user can only input comma separated numerical values. The catch here is that the user may or may not enter comma in the number and the number would still be valid. The even bigger catch is that the comma should be treated as a decimal when testing it in Condition 1. For example if the User enters "249,50" then this should be treated as 249.50 (which is smaller then 250) and not  as 24950 (which is greater than 250)

 

The simple condition builder on the UI Policy Form does not work in this situation.

Therefore I need a Script for the "Execute if True" Script Section to test Condition 1; and if it is True, then make User_Variable_Dependent Visible and Mandatory on Catalog Form, RITM Form and Task Form. And if Condition 1 is False, then make User_Variable_Dependent "Not Visible" in all Forms (User_Variable_Dependent is another Catalog Variable).

 

Feel free to suggest other ways like using Client Scripts as well.

 

1 ACCEPTED SOLUTION

Viraj Hudlikar
Giga Sage

Hello @AadityaSahu 

I believe you have a Ui Policy created then in condition you can enter the field name saying it should not be empty so it will be triggered.

 

In the UI Policy's Actions section, check Execute script if true and use the following script:

 

    var userInput = g_form.getValue('user_variable_num');
    var dependentVar = 'user_variable_dependent'; // Replace with your variable name

    if (userInput) {
        // Replace commas with dots and parse
        userInput = userInput.trim().replace(/,/g, '.');
        var numericValue = parseFloat(userInput);

        // Check if valid number and >= 250
        if (!isNaN(numericValue) && numericValue >= 250) {
            g_form.setVisible(dependentVar, true);
            g_form.setMandatory(dependentVar, true);
        } else {
            g_form.setVisible(dependentVar, false);
            g_form.setMandatory(dependentVar, false);
        }
    } else {
        // Hide if input is empty
        g_form.setVisible(dependentVar, false);
        g_form.setMandatory(dependentVar, false);
    }

 

I tested with test case as you mentioned and it work. 

  • Input: "249,50" → Treated as 249.50 → Dependent field hidden
  • Input: "250" → Treated as 250 → Dependent field visible and mandatory.

  • Input: "abc" NaN → Dependent field hidden.

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

View solution in original post

2 REPLIES 2

Viraj Hudlikar
Giga Sage

Hello @AadityaSahu 

I believe you have a Ui Policy created then in condition you can enter the field name saying it should not be empty so it will be triggered.

 

In the UI Policy's Actions section, check Execute script if true and use the following script:

 

    var userInput = g_form.getValue('user_variable_num');
    var dependentVar = 'user_variable_dependent'; // Replace with your variable name

    if (userInput) {
        // Replace commas with dots and parse
        userInput = userInput.trim().replace(/,/g, '.');
        var numericValue = parseFloat(userInput);

        // Check if valid number and >= 250
        if (!isNaN(numericValue) && numericValue >= 250) {
            g_form.setVisible(dependentVar, true);
            g_form.setMandatory(dependentVar, true);
        } else {
            g_form.setVisible(dependentVar, false);
            g_form.setMandatory(dependentVar, false);
        }
    } else {
        // Hide if input is empty
        g_form.setVisible(dependentVar, false);
        g_form.setMandatory(dependentVar, false);
    }

 

I tested with test case as you mentioned and it work. 

  • Input: "249,50" → Treated as 249.50 → Dependent field hidden
  • Input: "250" → Treated as 250 → Dependent field visible and mandatory.

  • Input: "abc" NaN → Dependent field hidden.

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Thankyou for your response Viraj. The solution you provided works as per requirement.

Warm Regards