UI Action to Check for Mandatory fields before Updating State
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 12:22 PM
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");
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 12:28 PM
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') != '')){
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 12:42 PM
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);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 12:30 PM
Hi ,
Here is a link which will help you.
If it helps,Please mark ✅ Correct and Helpful.
Warm Regards,
Milind

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 12:39 PM
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
Muhammad