can someone help me to userstand UI action how its working client and server side together with example ..?

s_kumar
Mega Contributor

Request

1 ACCEPTED SOLUTION

zica
Giga Guru

Okay perfect,



Here is a screenshot that shows the parameters of the UI action, Please do the same


Do not forget to put the action name, check client to true, and put the function name on Onclick()


Capture d



HEre is the script :



function reopenIncidentTest(){


     


      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','Reason is required when reopening an incident','error');


                      return false;   //Abort submission


        }


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


      gsftSubmit(null, g_form.getFormElement(), 'incident_reopen_test'); //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')


      serverReject();



function serverReject(){


      current.state = 2;


      current.update();


}


View solution in original post

31 REPLIES 31

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Pradeep



Thank you so much ..



i have got some idea about how it works..



if it possible do you have any other example which can elaborate more about this..


arnabwa
Giga Guru

Hi Sandeep,



That is a really very important question to have inside a Snow developer's mind. I will try my best to make you understand with my own code :



find_real_file.png


The above is a UI action, a button named "Refer Back" which is present for Approvers so that they can redirect the incident,change,task etc. to certain users if they feel the information is not enough for their approval.


Now for the button to behave as both Server side and Client side, we just need to check the "Client" check box. If unchecked it default behaves like a Server side script. On checking "Client" field you get a field "Onclick" to be shown just above the Condition field (See screenshot). Define any function in this field. Now this function body is defined in the Script field. What it means is that On-click of this button we actually want to call this function ( referBack() in my screenshot) and execute the codes inside this function. It this function where we write certain client side codes to be executed like making fields mandatory false or true, state changes, making fields read only true or false,etc. The classic case is when you want to click a button to make an update to a record, but only if the user has provided the correct input first. To bring into action this correct inputs are done we need client side running scripts and these are the scripts written inside the Onclick function. See my code below and do read the comments in the code lines :



  1. function referBack(){ // This function is called when we click on the button to execute certain client side codes.
  2.   if (g_form.getValue('comments') == '') {
  3.   // Remove any existing field message, set comments mandatory, and show a new field message
  4.   try {
  5.   g_form.hideFieldMsg('comments'); // Client side execution : hiding field messages in Comments field if any
  6.   } catch(e) {}
  7.   g_form.setMandatory('comments', true); // Client side execution : setting mandatory
  8.   g_form.showFieldMsg('comments', getMessage('Please enter a comment why would you like to refer back'), 'error'); // If you want the approver to describe the changes he needs. Client side execution
  9.   return false;   // Abort submission
  10.   }
  11.   gsftSubmit(null, g_form.getFormElement(), 'refer_back'); // MUST call the 'Action name' set in this UI Action
  12. }
  13. // Code that runs without 'onclick' . This is the server side code all below and outside the Onclick function because you need to execute this on the server side
  14. if (typeof window == 'undefined')
  15.   serverReopen();
  16. function serverReopen() {
  17.   var chg = new GlideRecord("change_request");
  18.   chg.addQuery('sys_id' , current.getValue('sysapproval'));
  19.   chg.query();
  20.   if(chg.next())
  21.   {
  22.   chg.state = 1; //changing the state of change req back to 'Open' from 'Approval Requested'
  23.   current.state = 'not requested'; // you might want to change the state of approval in the sysapproval_approver table from "requested" to "Not requested"
  24.   }
  25.   chg.update();
  26.   current.update();
  27.   gs.addInfoMessage(gs.getMessage("Request is referred back for changes")); // user friendly msg on success
  28.   action.setRedirectURL(current);
  29. }

I believe this helps you and makes your understanding even stronger. After all you are a SNOW Developer.



Thanks,


Arnab



Please mark Correct/Helpful according to the worthiness of my explaination. Thank you in advance.