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.

UI Action Still able to set State to Close Complete w/out answering requested questions

mythNOW
Tera Expert

Hello ServiceNow Community,

 

I'm working on editing tasks on a SCTASK record and I want to ensure this question is answered correctly before the State of the record is closed complete. 

 

mythNOW_0-1726781484528.png

I have a catalog client script made so that if:

An Analyst sets the state to Close Complete but 'does_cdt_approve' is set to --None--, and the analyst selects 'Update' an error message will occur, and the task is not set to Close Complete.  This is intended.

mythNOW_3-1726782142220.png

 

 

function onSubmit() {
    //Retrieve the SCTASK desc. value of rnc_lead_srat_review and state of task.
    var taskD = g_form.getValue('description');
    var cdtApprove = g_form.getValue('does_cdt_approve');
    var taskState = g_form.getValue('state');

    //Apply only to tasks with the following description and conditions
    if (taskD === 'Complete SIMM 5305-F GenAI Risk Assessment. Submit CDT GenAI ticket at CDT Service Portal.' && cdtApprove === '' && taskState === '3') {
        {
            //Prevent setting task to close complete
            g_form.addErrorMessage('Please answer "CDT has determined the following software:" prior to closing.');
            return false;

        }
    }
    //Allow submission
    return true;
}

mythNOW_1-1726781929381.png

 

 

 However, my instance also has an UI action analysts uses called 'Close Task' The contents of this UI action is below: 

mythNOW_4-1726782195482.png

function closeTask(){
	if(confirm('Are you sure you want to Close Complete this task?')) {
//Call the UI Action and skip the 'onclick' function
	gsftSubmit(null, g_form.getFormElement(), 'close_sc_task');
}
return false;

}	
//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
   updateTask();

function updateTask(){
current.state = 3;
current.update();
}

Essentially, clicking 'Close Task' and confirming this UI action will set the State to close complete, but this leaves 'does_cdt_approve' still set to --None-- which will need a selection prior to closing the task.

 

Any reason why the UI action still allows the ability to close the task?

 

1 REPLY 1

Murthy Ch
Giga Sage

Hello @mythNOW 
It took me while to understand where it was causing this issue. Any how you can try like below:

The Onsubmit will not indentify here because the state value will changes in backend using server side script.

I tried to replicate the same scenario in my PDI and this is how I achieved:

1) Create a before update BR on sc_task table and give the conditions and code like below:

MurthyCh_0-1727042691600.png

CODE:

(function executeRule(current, previous /*null when async*/ ) {

    if (gs.nil(current.variables.does_cdt_approve) && current.variables.description == 'Complete SIMM 5305-F GenAI Risk Assessment. Submit CDT GenAI ticket at CDT Service Portal.') { //double check this condition 
        gs.addErrorMessage("Please answer 'CDT has determined the following software:' prior to closing.");
        current.setAbortAction(true);
    }

})(current, previous);

Quick overview:

CatalogTask.gif

Hope it helps:)

Thanks,
Murthy