Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

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

Ashley Snyder1
Giga Guru

Just double checking, you have the Onclick field set to 

movearbitration(); 

Also, it looks like you're doing your client side checking outside of your client function, see this article 'Reopen Incident UI Action' section:  https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/

I think these need to be checked in movearbitration(); 

 if ((g_form.getValue('u_resolution_code') != '') && (g_form.getValue('u_resolution_description') != '') && (g_form.getValue('u_precedent') != '')){

@Ashley Snyder Yes I mentioned the Onclick as movearbitration() 

 

I have modified the UI action code as below , but now after getting the error message and when user fills the mandatory fields I am not getting the UI action any more as the state is getting updated already 

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(current.u_resolution_code != '' && current.u_resolution_description != '' && current.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);
	}
} 
	

Milind Gharte
Kilo Guru

Hi ,
Here is a link which will help you.

https://community.servicenow.com/community?id=community_question&sys_id=bad68434db3d6b00fb115583ca96...

 

If it helps,Please mark ✅ Correct and Helpful.

Warm Regards,

Milind

MrMuhammad
Giga Sage

Hi,

UI Action doesn't respect fields made mandatory using UI Policies or client script as these executes on client side/browser. 

YOu have to put if conditions and check if the value of field x is filled in only then change the state to closed. 

Sample Code

function runArbitrationcode() {
   if(close_code != '' && close_notes != '') { //check field values before closing the change
     current.state = 3;
     current.update();
     gs.addInfoMessage('Labor case has been moved to arbitration');
     action.setRedirectURL(current);
   }
} 

I hope this helps!

 

Thanks,

Sharjeel

Regards,
Muhammad