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

Hi,

you can check which field is not filled and then show the consolidated message

Regards
Ankur

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

Hi @Ankur Bawiskar 

Can it not be dynamic ? or it has be static message ? 

 

	if(flag)
alert('Please update fields in category, alignment, overview & solution');
		return false; //Abort submission

 

 

 

Hi,

you can append the field name in every if statement since you know which field is missing

at the end show that combined message

Regards
Ankur

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