The CreatorCon Call for Content is officially open! Get started here.

How to make UI Page field mandatory while cancelling the Change Request?

Admin7267
Kilo Sage

I was trying to make field mandatory while cancelling the incident, but it's not working.

When user clicks on Cancel Request UI action alert should come, if user clicks on OK on the alert, then UI page should come with mandatory field. User needs to fill that field in order to cancel the Change Request.

 

UI Action:

 

var changeConfirmCancelDialog;

function setChangeCancelState() {

	var answer = confirm(getMessage('Are you sure you want to cancel this Change Request?'));
	if(answer == false){
	return false;
	}
	
	if(answer == true){
	var uiPageSysId = '83972214c3322200b6dcdfdc64d3ae3c'; // Passing UI Page sys_id here
	var url = '/ui_page.do?sys_id=' + uiPageSysId;
	g_navigation.open(url,'_blank');
	}
	if(answer == true && (current.reason.nil() == false)){
	g_form.setValue("state", "4");
	//g_form.setValue("u_approval","Cancelled");
	gsftSubmit(null, g_form.getFormElement(), 'state_model_move_to_canceled'); 
	}
}

if (typeof window == 'undefined')
   setRedirect();

function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}

 

 

UI Page HTML:

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<g:dialog_notes_ok_cancel
		dialog_id="change_confirm_cancel"
		textarea_id="change_confirm_reason_text"
		textarea_label="${gs.getMessage('Reason')}"
		textarea_label_title="${gs.getMessage('A reason is required to cancel this change')}"
		textarea_name="change_confirm_reason_text"
		textarea_onkeyup="enableButton()"
		textarea_onchange="enableButton()"
		textarea_style="height:auto; width: 100%; resize: vertical;"
		textarea_title="${gs.getMessage('Enter the reason here')}"
		ok=""
		ok_action="cancelChangeRequest"
		ok_id="change_confirm_ok_btn"
		ok_title="${gs.getMessage('Cancel change request')}"
		ok_type="button"
		ok_style_class="btn btn-primary disabled"
		cancel_title="${gs.getMessage('Close the dialog')}"
	/>
</j:jelly>

 

 

UI Page Client  Script:

 

function cancelChangeRequest() {
	var textArea = $("change_confirm_reason_text");
	if (textArea)
		moveToCancel(textArea.value.trim());
}

(function() {
	$("change_confirm_reason_text").focus();
})();

 

 

 

 

 

3 REPLIES 3

Sid_Takali
Kilo Patron

hi @Admin7267 

Go through this link, It helps.

creating a mandatory field in ui page? 

Make UI Page Text area mandatory 

 

Regards,

Siddharam

Community Alums
Not applicable

Hi @Admin7267 ,

I have done a small change to your UI action script, it should work for you - 

var changeConfirmCancelDialog;
function setChangeCancelState() {
var answer = confirm(getMessage('Are you sure you want to cancel this Change Request?'));
if (answer == false) {
return false;
}
 
if (answer == true) {
var uiPageSysId = '83972214c3322200b6dcdfdc64d3ae3c'; // Passing UI Page sys_id here
var url = '/ui_page.do?sys_id=' + uiPageSysId;
g_navigation.open(url, '_blank');
}
 
// If the user confirms and the reason field is empty, prompt them to fill it
if (answer == true && current.reason.nil()) {
alert("Please provide a reason to cancel the Change Request.");
return false;
}
 
// If the user confirms and the reason field is filled, set the state to cancelled
if (answer == true && !current.reason.nil()) {
g_form.setValue("state", "4");
gsftSubmit(null, g_form.getFormElement(), 'state_model_move_to_canceled');
}
}

if (typeof window == 'undefined')
setRedirect();

function setRedirect() {
current.update();
action.setRedirectURL(current);
}

Ankur Bawiskar
Tera Patron
Tera Patron

@Admin7267 

if the ask is just to enter the reason then this can be done using javascript prompt box where user can type in the reason

why to use UI page for this?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader