UI Action to Check for Mandatory fields before Updating State

Mrman
Tera Guru

Hi All,

I have requirement to update the state using the UI action and this UI action should update the state only when Mandatory fields are getting populated . 

This is UI action is making state to 'Closed' and we have UI policy to make certain fields mandatory when state changes to Closed .

So this UI action should not be updating the state with out checking for Mandatory fields.  

Below UI action created with client checkbox selected , this is not working .

function movearbitration() {

    //Call the UI Action and skip the 'onclick' function
    gsftSubmit(null, g_form.getFormElement(), 'hr_labor_arbitration'); //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')
    if ((g_form.getValue('u_resolution_code') != '') && (g_form.getValue('u_resolution_description') != '') && (g_form.getValue('u_precedent') != '')){
        runArbitrationcode();

function runArbitrationcode() {
    current.state = 3;
    current.update();
    gs.addInfoMessage('Labor case has been moved to arbitration');
    action.setRedirectURL(current);

} 
	}

else{
	gs.addErrorMessage("Please fill in mandatory fields");
}
13 REPLIES 13

Agreed.  You have to perform your field validation within the Client-side 'onclick' function and set those fields mandatory within the same function, as illustrated in the example I posted. Then you abort the UI Action within the client-side checking if it doesn't meet the parameters, as it shouldn't move on to any server-side actions if the record doesn't meet the conditions for mandatory fields.

https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/

This allows for the user to see 'oh these fields are mandatory' even though they were before with the UI Policy, but won't let them progress with empty field values until the client-side conditions are met in the UI Action.

HI Muhammad,

 

I have tried the below UI action , on click on this it is chaning the state and throwing the Error message , bit once I fill the mandatory fields I am not getting the UI action anymore as state is already getting updated.

function movearbitration() {

    //Call the UI Action and skip the 'onclick' function
    gsftSubmit(null, g_form.getFormElement(), 'hr_labor_arbitration'); //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')
    
        runArbitrationcode();

function runArbitrationcode() {
	current.state = 3;
    current.update();
	if(u_resolution_code != '' && u_resolution_description != '' && u_precedent !=''){
    gs.addInfoMessage('Labor case has been moved to arbitration');
    action.setRedirectURL(current);
	}
	
	else{
		urrent.setAbortAction(true);
		gs.addInfoMessage("Please fill in the mandatory fields under Resolution tab");
		action.setRedirectURL(current);
	}
} 
	

Ashley Snyder1
Giga Guru

Here's an example, note this code hasn't been tested, some of the if conditions may need to be adjusted, as I just did this on the fly:

   function movearbitration() {
    if (g_form.getValue('u_resolution_code') =='') && (g_form.getValue('u_resolution_description') == '') && (g_form.getValue('u_precedent') == '')) {
    g_form.setMandatory('u_resolution_code', true);
    g_form.setMandatory('u_resolution_description', true);
    g_form.setMandatory('u_precedent', true);
    g_form.showFieldMessage('u_resolution_code', 'If you want one')
    return false; //abort submission, this is what does all of your field checking and stops the server script from moving forward if not met
    }
    //Call the UI Action and skip the 'onclick' function
    gsftSubmit(null, g_form.getFormElement(), 'hr_labor_arbitration'); //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')
    runArbitrationcode();

    function runArbitrationcode() {
    current.state = 3;
    current.update();
    gs.addInfoMessage('Labor case has been moved to arbitration');
    action.setRedirectURL(current);
    }

Hi Ashley,

Please update below lines as [the second parameter is missing (true/false)]

g_form.setMandatory('u_resolution_code', true);
g_form.setMandatory('u_resolution_description', true);
g_form.setMandatory('u_precedent',true);

@ramamr  Please try the above code by Ashley. If you get any issues do provide the screenshot of other UI action configurations as well for better understanding.


Thanks,

Sharjeel

Regards,
Muhammad

Hi @Ashley Snyder @Muhammad 

I tried the following UI action code. Now on click on the button the fields are becoming mandatory but user will not come to know that they have to fill the mandatory fields .

These fields are under a section on the form at the middle of form and it is not Scroll down to those fields that they have to fill those . How do I get redirected to those fields.

function movearbitration() {

    if (g_form.getValue('u_resolution_code') == '' && g_form.getValue('u_resolution_description') == '' && g_form.getValue('u_precedent') == '') {
    g_form.setMandatory('u_resolution_code', true);
    g_form.setMandatory('u_resolution_description', true);
    g_form.setMandatory('u_precedent', true);
    g_form.showFieldMessage('u_resolution_code', 'fill resolution code','error',true);
	g_form.showFieldMessage('u_resolution_description', 'fill resolution description', 'error', true);
    return false;
}

//Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'hr_labor_arbitration'); //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')

    runArbitrationcode();

function runArbitrationcode() {
    //if (u_resolution_code != '' && u_resolution_description != '' && u_precedent != '') {
        current.state = 3;
        current.update();
        gs.addInfoMessage('Labor case has been moved to arbitration');
        action.setRedirectURL(current);
     /*else {
        urrent.setAbortAction(true);
        gs.addInfoMessage("Please fill in the mandatory fields under Resolution tab");
        action.setRedirectURL(current);
    }*/
}