UI Action Still able to set State to Close Complete w/out answering requested questions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2024 02:48 PM
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.
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.
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;
}
However, my instance also has an UI action analysts uses called 'Close Task' The contents of this UI action is below:
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2024 03:40 PM
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:
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:
Hope it helps:)
Murthy