onChange client script - make comments mandatory upon modification of state

mitzaka
Mega Guru

Hi SNC,

 

I am trying to set up a use case where when a user tries to close a catalog task, he has to enter customer comment notes.

For the action of closing the catalog task I have an UI Action button on the catalog task form, which has the following code:

 

        current.task_state = 3;

        current.update();

 

For the user comments field I am using the customer comments field from the RITM.

And for the script I wrote, it's on the catalog task table, and it looks like this:


find_real_file.png

 

So at first sight it all seems OK, but if I change the state from the drop down list. However, when I try to click the UI button, it does not work. It just closes the task without prompting me for anything.

 

Any ideas where am I going wrong?

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Add this code to your ui action



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


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


                try{g_form.hideFieldMsg('comments');}catch(e){}


                g_form.setMandatory('comments', true);


                g_form.showFieldMsg('comments','Comments are mandatory','error');


                return false;   //Abort submission


          }




Use the ui action that has both client anbd server side code. this will be client side code,   and


current.task_state = 3;


        current.update();


this will be your server side code.


-Anurag

View solution in original post

8 REPLIES 8

Actually request_item.comments was the field that does the work for me.


Anurag Tripathi
Mega Patron
Mega Patron

Add this code to your ui action



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


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


                try{g_form.hideFieldMsg('comments');}catch(e){}


                g_form.setMandatory('comments', true);


                g_form.showFieldMsg('comments','Comments are mandatory','error');


                return false;   //Abort submission


          }




Use the ui action that has both client anbd server side code. this will be client side code,   and


current.task_state = 3;


        current.update();


this will be your server side code.


-Anurag

Harish Murikina
Tera Guru

Hi Dimit,



                          Your changing the state by clicking the Ui button in that case client script wont ask for any mandatory fields.



select check box "Client" and Write the code in uiaction .



alert('Please leave comments why are you setting state to completed');


g_form.setMandatory('request_item.comments', true);



For more informatio nclick this link http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/



Regards,


Harish.


I managed to get it all into the UI Action - both client and server side code. Thanks to all for the quick replies!