Catalog Variable Set issue with onChange Client Script(s)

bvlo
Tera Expert

I am attempting to utilize a variable set for several different workflows and I'm running into an issue with getting the variables to display when I want them to on the form. The crux of my issue is as follows.

I have one select box field (called Type) containing three different options. When the user selects one of the choices, I want some of the variables from the variable set to be displayed, and then if the user changes this choice, I want the old variables to be cleared and no longer visible and the new variables associated with the updated choice to be displayed on the form.

My approach to these requirements was to create 4 onChange Catalog Client scripts that fire when the user changes the Type field :

Clear (order of 5)

Add (order of 10000)

Change (order of 11000)

Delete (order of 12000)

I only have these 4 scripts, one variable set (order values fall between 5 and 10000) and one variable (Type) active at this point.  

 

The Clear is only doing the following:

function onChange(control, oldValue, newValue, isLoading) {

   if (isLoading || newValue == '') {

      return;

   }

 g_form.setVisible('my_variable_set_name', false);

            console.log('Clear');

}

 

The Add/Change/Delete scripts are very straightforward:

 

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

    if (g_form.getValue('gl_request_type') == 'Extend GL Account') {

        g_form.setVisible('company_codes', true);

        g_form.setVisible('account', true);

        g_form.setVisible('chart_of_accounts', true);

        g_form.setVisible('reason_for_request', true);

 

                        console.log('Extend');

        }

    }

 My train of thought here is every time the user changes the Type field, hide all the variables in the variables set (clear script) -> then based on what the new selection is in the Type field (Add/Change/Delete) run the corresponding script.

 Everything works as I want it the first time the page is loaded, if I select either Add/Change/Delete in the Type field I see in my console log the Clear script and corresponding Add/Change/Delete script run and I get the desired variables on the page. The issue is when I update Add/Change/Delete again, all the fields are hidden on the page (console logs shows me the clear script ran) but the new fields are not displayed despite the log showing me the appropriate script ran. I know the appropriate script works because it works the first time around. 

I am really at a loss at what to look at or troubleshoot at this point. In anticipation of this being asked, I don’t see how I could utilize a UI policy to accomplish this given logic of when I need these variables displayed or cleared.  Thank you in advance.

1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron

Yeah, UI and Data policies have been implemented kinda' backwards. Those imply one should create multiple UI Policies for one field each matching different scenarios. I find it is simpler and more robust to create one UI Policy per field per UI Policy action* and mark it "Reverse if false". In this case the UI Policy statement/condition for "Account" would be something like:

if "Type" is one of "Change" or "Delete" apply UI Policy action set visible to true.

Now if someone selects either of the two types, account will become visible, otherwise it will not be visible.

Having a single point of configuration (a single UI Policy) for a field makes it easier to manage too: avoiding conflict between policies or fine-tuning the sequence of the policies will become non-issues.

Do make sure if for the same field you have both visibility and mandatory policies, make mandatory ones have a smaller order no. than visibility ones. The reason being mandatory policies trounce visibility so if the not-mandatory policy is applied to a mandatory field after the visibility one, the visibility one will make no effect.

*)  I mean if you want to control both visibility, read-only- and mandatory states, I create a separate UI Policy for each action.

View solution in original post

8 REPLIES 8

Michael Jones -
Giga Sage

I could be missing something but, do you really need 4 separate scripts for this? You should be able to accomplish the same results using a switch statement within a single script, which might resolve your issue. 

Something like: 

function onChange(control, oldValue, newValue, isLoading) {
   
        //Hide the variable set by default
	g_form.setVisible('my_variable_set', false);

    switch (newValue) {
        case 'add': //if add is selected
            g_form.setVisible('field_1', true);
            break;
        case 'change': //if change is selected
            g_form.setVisible('field_2', true);
            break;
        case 'delete': //if delete is selected
            g_form.setVisible('field_3', true);
            break;
            //etc...
    }

}

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

I started out going this approach Michael and got the same behavior as what I posted. That is to say when the page loads and the first time a user selects the choice - it works. The moment they change the option - the variables are cleared from the page but the new/correct ones are not displayed. I thought by breaking out into four different scripts might get me a different result. I don't understand why what you have posted above or what I originally posted does not work as we expect it to. I do really appreciate you taking the time to reply and post your code. 

Upender Kumar
Mega Sage

Clear script should not run when type field changes.

Is there any UI Policy is created for Clear field?

-O-
Kilo Patron
Kilo Patron

Why would it not be possible to use UI Policies? Unless the conditions for the UI Policy cannot be expressed using the condition builder only than UI Policies cannot be used.

As for clearing the values when Type change, I'm not sure that is the best strategy. I would do it either in an onSubmit script or in the WF or Flow. I mean maybe someone enters 3 hours worth of information (I'm exaggerating, of course), then changes the Type by accident (your script clears the data), the user switches back to the original option, only to find the entered data is gone.