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

Hi Ajay,

 

You also need to remove the return false from both the IF block and put it before the submit action i.e. before the below line,

 

gsftSubmit(null, g_form.getFormElement(), 'close_request');

 

Regards,

Shishir

Ajay37
Tera Contributor

Hi @Shishir ,

If we place return statement before the gsft, the form is not getting saved when we click on Close UI action, after filling up the values in fields.

Thanks

Oh yes, can you put both the if condition in OR conditions and put the Return false in the if block and then give it a try?

Ajay37
Tera Contributor

Hi @Shishir ,

I thought to write the script as you said, but I got stopped with confusion like if I write OR condition:

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

g_form.setMandatory('comments', true);

g_form.setMandatory('track_number', true);

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

return false;

}

How to show error message, with the field Label only whose value is empty?

 

Thank you.

You can use following code for error messages before the return false.

if(g_form.getValue('track_number') == ''){
     g_form.addErrorMessage('The following mandatory fields are not filled in: Track Number');
}
else if(g_form.getValue('comments') == ''){
     g_form.addErrorMessage('The following mandatory fields are not filled in: Additional Comments');
}
else{
     g_form.addErrorMessage('The following mandatory fields are not filled in: Track Number');
     g_form.addErrorMessage('The following mandatory fields are not filled in: Additional Comments');
}