Disable mandatory field check on click of a particular UI action

Hardik Panchal
Mega Guru

Hello All,

I have a UI action called "L3 Support", when clicked redirects us to a new form.

But I have a requirement, when only this particular UI action button is clicked, it should not check for any mandatory fields.

It should redirect us to the new form without validating for the mandatory fields or even when mandatory fields are not filled in.

UI action Script:

// Client side onclick function
function createl3support(){
	if (g_form.getValue('comments') == '') {


		// Remove any existing field message, set comments mandatory, and show a new field message
		try {
			g_form.hideFieldMsg('comments');
		} catch(e) {}
		g_form.setMandatory('comments', true);
		g_form.showFieldMsg('comments', getMessage('Please enter a comment to open L3 Support case'), 'error');

		return false;  // Abort submission
	}
	// Call the UI Action and skip the 'onclick' function
	gsftSubmit(null, g_form.getFormElement(), 'l3_support'); // MUST call the 'Action name' set in this UI Action
}

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

function serverReopen() {
	// Set Incident state to active, update and reload the record
	var l3support = new GlideRecord("sn_customerservice_l3_case_type");
	l3support.newRecord();
	l3support.short_description = current.short_description;
	l3support.description = current.description;
	l3support.asset = current.asset;
	l3support.product = current.product;
	l3support.assignment_group = "b85d44954a3623120004689b2d5dd60a";
	l3support.parent = current.sys_id;
	l3support.u_parent_case = current.sys_id;

	//l3support.insert();
	action.openGlideRecord(l3support);
	//action.setRedirectURL(answer);
	gs.addInfoMessage(gs.getMessage("New case opened"));
}

 

Please help.

1 ACCEPTED SOLUTION

Hello Ankur,

I tried the above script and it bypasses the mandatory check, but doesn't perform, the new form redirection and copying fields over is also not happening.

I added to my script the below code and it worked fine for me:

var fields = g_form.getEditableFields();

	for (var x = 0; x < fields.length; x++) {
		g_form.setMandatory(fields[x], false);
	}

Thanks for your reply.

View solution in original post

7 REPLIES 7

@Hardik Panchal 

Since you are checking for mandatory field comments in your script; remove that check

Also add this line in client side ->     g_form.checkMandatory = false;

// Client side onclick function
function createl3support(){
    // Call the UI Action and skip the 'onclick' function
    g_form.checkMandatory = false;
    gsftSubmit(null, g_form.getFormElement(), 'l3_support'); // MUST call the 'Action name' set in this UI Action
}

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

function serverReopen() {
    // Set Incident state to active, update and reload the record
    var l3support = new GlideRecord("sn_customerservice_l3_case_type");
    l3support.newRecord();
    l3support.short_description = current.short_description;
    l3support.description = current.description;
    l3support.asset = current.asset;
    l3support.product = current.product;
    l3support.assignment_group = "b85d44954a3623120004689b2d5dd60a";
    l3support.parent = current.sys_id;
    l3support.u_parent_case = current.sys_id;

    //l3support.insert();
    action.openGlideRecord(l3support);
    //action.setRedirectURL(answer);
    gs.addInfoMessage(gs.getMessage("New case opened"));
}

Regards
Ankur

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

Hello Ankur,

I tried the above script and it bypasses the mandatory check, but doesn't perform, the new form redirection and copying fields over is also not happening.

I added to my script the below code and it worked fine for me:

var fields = g_form.getEditableFields();

	for (var x = 0; x < fields.length; x++) {
		g_form.setMandatory(fields[x], false);
	}

Thanks for your reply.

@Hardik Panchal 

Glad to know.

the script I shared did disable the mandatory fields but redirection was not working?

can you explain what was the exact issue?

Regards
Ankur

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