how to set mandatory on close notes field in requested item

TMKAM
Tera Contributor

I have created an UI Action "Cancel Request" button as per below in the requested item table and I want to add the existing scripts when the assignee clicks on the UI Action "Cancel Request" button it will force or set a mandatory to fill in the close notes comment.

 

How can I do script it and thank you.

 

Condition

current.approval == 'requested'

 

Scripts

current.state = '4';
current.approval = 'Cancelled';
current.update();

2 ACCEPTED SOLUTIONS

pethumdesilva
Tera Guru

Hi TMKAM

 

Create a new client script for RITM
type: onSubmit
 
in the script 
get the action (assume your UI action action name "cancel_request"
 
function onSubmit() {
 
 var action = g_form.getActionName();
   if (action != 'cancel_request')
      return;
  
  
// Close notes and Close code must be on the form   
   if (!g_form.hasField('close_notes'))
      return;
  
  
 g_form.setDisplay('close_notes', true);
   g_form.setMandatory('close_notes', true);
   
if (g_form.getValue('close_notes') == '')
      return false;   
}
 
Regards,
Pethum

View solution in original post

Pavankumar_1
Mega Patron

Hi @TMKAM ,

try below script and give action name as reject_action and select Client check box.

Give onClick function name as reject() or any name must use same function on script.

function reject() {
	g_form.setMandatory('close_notes', 'true');
	var answer = confirm("Are you sure you want to Cancel Request?");
	if (answer == true) {
		gsftSubmit(null, g_form.getFormElement(), 'reject_action'); //MUST call the 'Action name' set in this UI Action
	} else {
		return false;
	}
}
if (typeof window == 'undefined') {
	current.state = '4';
	current.approval = 'Cancelled';
	current.setWorkflow(false);
	current.update();
}

Screenshot (824).png

Screenshot (825).png

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

7 REPLIES 7

pethumdesilva
Tera Guru

Hi TMKAM

 

Create a new client script for RITM
type: onSubmit
 
in the script 
get the action (assume your UI action action name "cancel_request"
 
function onSubmit() {
 
 var action = g_form.getActionName();
   if (action != 'cancel_request')
      return;
  
  
// Close notes and Close code must be on the form   
   if (!g_form.hasField('close_notes'))
      return;
  
  
 g_form.setDisplay('close_notes', true);
   g_form.setMandatory('close_notes', true);
   
if (g_form.getValue('close_notes') == '')
      return false;   
}
 
Regards,
Pethum

@pethumdesilva 

thank you for your solution and it is working as expected but how do I add a message to inform the assignee to put the message in the close notes.

thank you

pethumdesilva
Tera Guru

Hi TMKAM,

 

Refer this article: https://servicenowguru.com/scripting/ui-info-error-message-cheat-sheet/

 

Regards,

Pethum

Pavankumar_1
Mega Patron

Hi @TMKAM ,

try below script and give action name as reject_action and select Client check box.

Give onClick function name as reject() or any name must use same function on script.

function reject() {
	g_form.setMandatory('close_notes', 'true');
	var answer = confirm("Are you sure you want to Cancel Request?");
	if (answer == true) {
		gsftSubmit(null, g_form.getFormElement(), 'reject_action'); //MUST call the 'Action name' set in this UI Action
	} else {
		return false;
	}
}
if (typeof window == 'undefined') {
	current.state = '4';
	current.approval = 'Cancelled';
	current.setWorkflow(false);
	current.update();
}

Screenshot (824).png

Screenshot (825).png

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar