Issue with UI action with mandatory fields

psyherin
Kilo Sage

Hi All,

I am new to scripting and trying to get my head around with UI action script and making fields mandatory. I have built the script based on https://servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/ . The script is performing field validation but it seems like if I update any one mandatory field with some data and click the button it moves ahead and updates the record. It should not submit the form until all the mandatory fields are updated.

Please advise what am i doing wrong with the below script:

function pending() {
    if ((g_form.getValue('category') == '') && (g_form.getValue('u_alignment') == '') && (g_form.getValue('u_overview') == '') && (g_form.getValue('u_solution') == '')) {
        g_form.setMandatory('category', true);
        g_form.setMandatory('u_alignment', true);
        g_form.setMandatory('u_overview', true);
        g_form.setMandatory('u_solution', true);
        return false; //Abort submission
    }
    //Call the UI Action and skip the 'onclick' function
    gsftSubmit(null, g_form.getFormElement(), 'pending_solution_approval'); //MUST call the 'Action name' set in this UI Action
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
    updateTask();

function updateTask() {
        current.state = '150'; 
        current.update();
        action.setRedirectURL(current);
}

 

Thank you

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

function pending() {

	var flag = false;

	if (g_form.getValue('category') == ''){
		g_form.setMandatory('category', true);
		flag = true;	
	}

	if(g_form.getValue('u_alignment') == ''){
		g_form.setMandatory('u_alignment', true);
		flag = true;	
	}

	if(g_form.getValue('u_overview') == ''){
		g_form.setMandatory('u_overview', true);
		flag = true;	
	}

	if(g_form.getValue('u_solution') == ''){
		g_form.setMandatory('u_solution', true);
		flag = true;	
	}

	if(flag)
		return false; //Abort submission

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

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
	updateTask();

function updateTask() {
	current.state = '150'; 
	current.update();
	action.setRedirectURL(current);
}

Regards
Ankur

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

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

check for each field separately

Regards
Ankur

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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

function pending() {

	var flag = false;

	if (g_form.getValue('category') == ''){
		g_form.setMandatory('category', true);
		flag = true;	
	}

	if(g_form.getValue('u_alignment') == ''){
		g_form.setMandatory('u_alignment', true);
		flag = true;	
	}

	if(g_form.getValue('u_overview') == ''){
		g_form.setMandatory('u_overview', true);
		flag = true;	
	}

	if(g_form.getValue('u_solution') == ''){
		g_form.setMandatory('u_solution', true);
		flag = true;	
	}

	if(flag)
		return false; //Abort submission

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

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
	updateTask();

function updateTask() {
	current.state = '150'; 
	current.update();
	action.setRedirectURL(current);
}

Regards
Ankur

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

Thank you @Ankur Bawiskar . it worked !!

Hi @Ankur Bawiskar ,

I am not getting message "The mandatory fields are not filled: xxxx" is there any way to populate this ? I was getting mandatory field message in my previous script.

Let me know.

Thanks