Set variable Mandatory when 'Close Task' button pressed

Stan53
Tera Contributor

Hi SN community gurus,

 

I'm new to SN and I need help. To be honest, I don’t know where to dig yet.

I have a Catalog Task, the 'Selected retired laptop' variable is a drop down list and it is not mandatory (I can't make it mandatory because there could be some cases to leave it empty (--None--) and close the task with status 'Closed Incomplete' or 'Closed Skipped'.

What I need is if the 'Close Task' button is clicked and the 'Selected retired laptop' variable is still empty, make the 'Selected retired laptop' variable mandatory to prevent the task from being closed and remaining in the task view with a warning like 'Please select a laptop'. Or don't make it mandatory, but still prevent the task from being closed and remain in the task view with the same warning.

I don't know how to implement it yet.

Can anyone help, please?
Community quest #1.PNG

Thank you in advance.

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@Stan53 Create a UI Policy on the Catalog item where the initial request originates and choose Apply on Catalog Task option'

 

Screenshot 2024-03-10 at 7.39.06 AM.png

Make the field mandatory via UI Policy Actions.

 

Also, found an article on the same topic https://www.servicenow.com/community/now-platform-blog/what-is-the-best-way-to-make-catalog-task-var... 

 

Hope this helps.

omkar_jadhav_
Tera Contributor

hello @Stan53 
These steps might help you:
1 . go to Client Scripts
2. Select the table that is sc_task
3. select type onSubmit
4. write a script

omkar_jadhav__0-1710074251007.png

5. or else you can add this script in UI Action where you have created the "Close Task" button

Weird
Mega Sage

You can simply check if the value has been populated and prevent submission if it has not.
So, in your UI action script you could add a check for the item with the variable and then check the variable value. For example something like this:

if(current.requested_item.cat_item == "YOUR ITEM SYS_ID"){
if(current.variables.YOUR_VARIABLE == ""){
gs.addErrorMessage("Please populate the XXX variable before closing the task");
}else{
//What the script normally does. For example update.
}
}

It depends a bit on whether your UI action is running client or server side, but you should be able to catch the logic here. You don't have to make the variable mandatory, but you can prevent user from closing the task with the UI action, if the variable hasn't been populated.

Oh, and I don't remember if you can reference the variables directly on the sc_task. I think you should be able to, but if not, then just use current.request_item.variables instead of current.variables.