The CreatorCon Call for Content is officially open! Get started here.

Differentiate client scripts and server scripts in UI actions?

ram2497
Tera Contributor

Differentiate client scripts and server scripts in UI actions?

4 REPLIES 4

Harsh Vardhan
Giga Patron

Kindly have a look on below blog, worth to read this. detail explanation with an example. 

 

https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

 

https://www.servicenowelite.com/blog/2014/2/19/client-vs-server

https://www.youtube.com/watch?v=TqSI2FcU8iE

https://www.youtube.com/watch?v=3A0qeS4rAYw

Thanks,
Ashutosh

Megha Padale
Giga Guru

Hi Ram,

UI actions in servicenow can run on server side and client side both, Mostly requirement is of to run UI action on server side. You can add UI actions to tables and database views that are in the same scope.

https://www.basicoservicenowlearning.in/2020/01/ui-action-in-servicenow.html

https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/#:~:text=...

If my answer helped you in any way, mark answer as helpful and correct.

Thanks and regards,

Megha

Anil Shewale
Mega Guru

Hi

 

The action name should be unique with respect to UI action, but it can be anything. Make sure you do no give any space in the action name field.

Onclick function runs on the client side and the rest of the functions that are written executes in the server side.

gsftSubmit(null, g_form.getFormElement(), "ui action id") triggers the UI Action which is specified in the 3rd parameter, which is the action name/element id.

It is mostly used in UI actions that have a client side and a server side script.

At the end of the client side script, you call gsftSubmit in order to trigger the UI Action again - this time running only the server side code.

 

EXAMPLE:

Name: -Button Name-
Action name: -button_action_name- (Should be unique per button on form and gets called from the UI Action script)
Client: True (MUST be checked)
Form button/Form Context Menu/Form Link: (UI Action must be one of these ‘Form’ types)
Onclick: -runClientCode();- (Points to the function in your script that should be run when the UI Action gets clicked)
Script:

//Client-side 'onclick' function
function runClientCode(){
   if(<CONDITION_TO_VALIDATE> == false){
      return false;  //Abort submission
   }
   //Call the UI Action and skip the 'onclick' function
   gsftSubmit(null, g_form.getFormElement(), '<button_action_name>'); //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')
   runBusRuleCode();

//Server-side function
function runBusRuleCode(){
   current.<field_name> = <value>;
   current.update();
   gs.addInfoMessage('You did it!');
   action.setRedirectURL(current);
}

Refer the following link for mor:

https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/

 

If it help mark helpful or correct 

Thanks and regards

Anil