Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to make Two fields mandatory on clicking Close UI action button?

Ajay37
Tera Contributor

Hi All,

I want to make two fields mandatory when user clicks on Close UI action, as one of the fields is Additional comments, I can't go with data policy, because if I give a condition like Make comments mandatory in close state, comments is mandatory even after the ticket gets closed. I want the fields to become mandatory only on clicking the UI action. I have written the below code, but it is working like one after the other If condition. i.e., if I click on Close for first time, it is showing comments mandatory. After filling comments, if I click on the button again, then it is showing track number mandatory.

function closeRequest(){


    if(g_form.getValue('comments') == ''){

        //Remove any existing field message, set comments mandatory, and show a new field message

          //g_form.hideFieldMsg('comments');

          g_form.setMandatory('comments', true);

          g_form.addErrorMessage('The following mandatory fields are not filled in: Additional Comments');

        return false; //Abort submission

    }
	else if (g_form.getValue('track_number') == ''){

          g_form.setMandatory('track_number', true);

          g_form.addErrorMessage('The following mandatory fields are not filled in: Track Number');

        return false; 

    }

    //Call the UI Action and skip the 'onclick' function

    gsftSubmit(null, g_form.getFormElement(), 'close_request'); //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 = 3;
current.update();
}

Thanks

11 REPLIES 11

Ajay37
Tera Contributor

Hi @Shishir ,

Is it possible to show only one error message, with the field which is empty? I mean the message should be dynamic showing the field with empty value.

Thanks

Yes that can be done , just declare an array and start pushing it for whichever variable is empty and then print the error message.

var arr=[]

if(g_form.getValue('track_number') == ''){
arr.push(g_form.getLabelOf('track_number');
}
if(g_form.getValue('comments') == ''){
arr.push(g_form.getLabelOf('comments');)
}

g_form.addErrorMessage('The following mandatory fields are not filled in:' + arr);
}