Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Client Script not triggered when using "Close Task" UI Action

linoyv691904179
Tera Contributor

Hi,

 

I’m facing an issue with a Catalog Task setup.

 

I created a Catalog Client Script (onLoad) that defines when a specific variable should be displayed based on certain conditions.
In addition, I created a Client Script (onChange) that should make this variable mandatory when those same conditions are met and the task is being closed (state changes to Closed Complete).

However, this logic only works when I manually change the state field, and does not trigger when clicking the "Close Task" UI Action buttonIn that case, the task is closed first, and only afterward does the variable become mandatory.

 

Has anyone encountered this behavior before or found a good approach to make the variable mandatory when closing via a UI Action?

Thanks in advance!

10 REPLIES 10

So you can write in onSubmit client script and check state value as closed and use g_form.getActionName().


You can make the variable mandatory and do a return false if it is empty. This is what I have done in past.


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023
LinkedIn

It is not working, maybe bc i have getXMLAnswer in my script

Ok so for async glide ajax follow below article:

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579 


Raghav
MVP 2023
LinkedIn

@linoyv691904179 Were to able to get this working?


Raghav
MVP 2023
LinkedIn

ChallaR
Giga Guru

Hi @linoyv691904179 ,

 

you can give try below mentioned solutions -

Option 1: Use a Catalog UI Policy Instead

Create a Catalog UI Policy that:

  • Runs when state == Closed Complete
  • Checks the same conditions (via a Catalog UI Policy Condition or Script)
  • Sets license_required as mandatory

UI Policies are better suited for enforcing field behavior based on values, regardless of how those values are set.

Option 2: Trigger Logic in the UI Action Script

If you control the UI Action, you can manually trigger the same logic in the script:

var individual = g_form.getValue('individual_or_floating');
var ga = new GlideAjax('ApplicationsDataClient');
ga.addParam('sysparm_name', 'getPurchaseRequired');
ga.addParam('sysparm_license', g_form.getValue('please_select_the_license_you_require'));
ga.getXMLAnswer(function(answer) {
    if (answer == 'true' && (individual == '141' || individual == '')) {
        g_form.setMandatory('license_required', true);
    } else {
        g_form.setMandatory('license_required', false);
    }
});

 

You can include this logic before the task is closed in the UI Action.

Option 3: Validate on Submit

Use a Catalog Client Script (onSubmit) to validate that license_required is filled when the task is being closed:

function onSubmit() {
    if (g_form.getValue('state') == '3') { // Closed Complete
        var individual = g_form.getValue('individual_or_floating');
        var ga = new GlideAjax('ApplicationsDataClient');
        ga.addParam('sysparm_name', 'getPurchaseRequired');
        ga.addParam('sysparm_license', g_form.getValue('please_select_the_license_you_require'));
        ga.getXMLAnswer(function(answer) {
            if (answer == 'true' && (individual == '141' || individual == '')) {
                if (!g_form.getValue('license_required')) {
                    alert('License Required must be filled before closing the task.');
                    return false;
                }
            }
        });
    }
    return true;
}
 
 
Thanks,
Rithika.ch