debendudas
Mega Sage

When any ServiceNow developer starts working on the Service Portal Widgets they discover there are mainly two out of the box (OOTB) methods that are used to communicate between the Client Side and Server Side and those are c.server.update() and c.server.get(). Both rerun the widget’s server script, but they behave differently and are meant to be used for different types of operations. In this article, we will dive into the concepts behind these two methods and understand the right way to choose which one to use in different scenarios.

 

Core Idea:

At a high level, you can think of these two methods like this:

  • c.server.get() -> "Get me some data" Primarily used to get or retrieve specific data from the server.
  • c.server.update() –> "Update what I have" Primarily used for update the changes from the client side back to the server side.

Both calls:

  • Re-execute the widget's server script.
  • Return an updated data object to the client.

The real distinction is what is sent from the client to the server and how the data object is getting updated.

 

How c.server.get() Works

c.server.get() is a client-to-server call that is used when the widget needs more data from the server. You can explicitly decide what to send as input, and the server script returns whatever you put on the data object.


Key characteristics:

  • Purpose: Fetch or retrieve data.
  • Input (Optional): Optional object you pass in.

    For example:
    c.server.get({ priority: 'high', active: true });​
    
     // On the server, this is available in input.priority and input.active.​
  • What is not sent: The full widget data is not send to the Server side. Only the object you pass in sends to the server.
  • Retrieved data: This method is asynchronous. It returns a promise and by using the then() method we can handle the response data which returned from the server side.

 

Typical use cases:

  • Filter a list based on user selections (priority, state, date range, etc).
  • Load additional details when a user clicks a row in a table.
  • Retrieve related records or preview data without saving anything.

 

Simple example (client script):

// Triggers when user selects priority 
c.filterData = function(){ 
    // Retrieving data from Server 
    c.server.get({ priority: c.data.selectedPriority }).then(function (response) {
        // Data retrieved from Server 
        console.log('Response', response); 
    });
 } 

// In the server script, read input.priority, run a query, and put results into something like data.tasks.

 


How c.server.update() Works

c.server.update() is designed for sending the current state/data of the widget back to the server so you can preserve the changes or perform server-side logic based on user input.


Key characteristics:

  • Purpose: Save, update, or process client-side changes.
  •  Input (Automatic): Automatically uses the entire data object as input.
  • Data sent: Everything under c.data is sent as input in the server script.
  • After completion: Server updates data and the widget is re-rendered with any new values.

 

Typical use cases:

  • Submitting a form (create/update Incident, Request, custom record).
  • Complex Server side logic which need to modify the entire widget data.
  • Multi-field updates where many properties may have changed on the client.

 

Sample Example (Client script):

// Triggers when user clicks on the save button 
c.saveForm = function () { 
    // Assume form fields are bound to c.data.formFields 
    c.server.update(); 
}; 

// The Server script can access all the values present in the data object under input object

 

 

When to use c.server.get() vs c.server.update()

Below are 5 practical scenarios to help you decide between c.server.get() and c.server.update():

 

SL No. Scenario What the user is doing in the portal Recommended method Why this method fits best
1 Filtering a list User selects Priority, Category, State and clicks on "Apply Filter" button c.server.get() You only need to fetch records based on filter criteria
2 Loading record details on row click User clicks a row in a list to see more details of the record c.server.get() You only need to fetch more details about the selected records
3 Creating a custom form with multiple fields User fills the form and click on submit button c.server.update() The entire widget's data storing the form field values must be sent to the server and saved
4 Complex Widget configuration which deals with data that are dependent on each other User perform a action which required complex calculation of the widget data c.server.update() When you need to perform complex calculations on interdependent data, it is better to use c.server.update() so that no data is missed in the server‑side calculation logic
5 Paginating through results User clicks "Next" or "Previous" to move between pages of results in a table. c.server.get() You are just requesting data from the server based on the pagination

 

In real implementations, the decision to use c.server.get() or c.server.update() may vary depending on your specific requirements, data model, and performance considerations, so always choose the approach that best fits your scenario.

 

You can also explore the c.server.refresh() method, which re-runs the server script and refreshes the widget's data without passing any data from client side to server side. It can be useful when you simply need to reload the widget state after certain actions or changes.

Version history
Last update:
2 hours ago
Updated by:
Contributors