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

Surendar4
Giga Contributor
Write both condition in an if condition using &&, then write g_form.setManfatory method inside that if condition. Try this

Ajay37
Tera Contributor

Hi @Surendar ,

I think if we use and condition, then if loop gets executed only when both are empty. If any of the fields have value, then it doesn't run. And also, how to display field name which is empty, in error message?

Thanks

Shishir
Mega Guru

Hi Ajay,

Instead of 'else if', just try with IF. In that case both the if condition will be evaluated whenever the code runs. Let me know if it helps.

Regards,

Shishir

Ajay37
Tera Contributor

Hi @Shishir ,

No Shishir, not working. Working like before itself.

Thank you.