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

Srikanth_Peru
Tera Guru

 

To go deep into this we will have a little brief about the following

  1. What is Client Side Scripting
  2. What is Server Side Scripting

What is Client Side Scripting?

Run JavaScript on the client (web browser) when client-based events occur, such as when a form loads, after form submission, or when a field changes value.

Types of Client Side Scripting:

  • onLoad(): Runs when a form is loaded.
  • onChange(): Runs when a particular widget changes value.
  • onSubmit(): Runs when a form is submitted.
  • onCellEdit(): Runs when a cell on a list changes value.

 

What is Server Side Scripting?

Server scripts run on the server or database. They can change the appearance or behavior of ServiceNow or run as business rules when records and tables are accessed or modified.

 

In how many ways we can retrieve data from server side to client side in ServiceNow

  1. GlideAjax
  2. getReference
  3. g_scrachpad

GlideAjax: The GlideAjax class allows the execution of server-side code from the client. GlideAjax calls pass parameters to the script includes, and, using naming conventions, allows the use of these parameters.

There are two types of GlideAjax call in ServiceNow

 


  1. Asynchronous GlideAjax: Create a client script as normal. This sends the parameters to server, which then does the processing. So that the client does not wait for the result, a callback function is used to return the result, passed to the getXML() function.

          In Asynchronous GlideAjax call code is executed with 

  1. getXML() – var answer = response.responseXML.documentElement.getAttribute("answer");
  2. getXMLAnswer() – var response = response;

Both getXML() & getXMLAnswer() are same response call will be different

  1. 2. Synchronous GlideAjax: This will slow down your code and lock the user session until the response is received. The getXMLWait() method is not available in scoped applications.

           In Synchronous GlideAjax call code is executed with

           “getXMLWait()” – ga.getAnswer();

NOTE: - Generally recommended that you use getXML()

 

A Sample GlideAjax in ServiceNow

Client Side Scripting

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var caller = g_form.getValue("caller_id");
    var gr_ajax = new GlideAjax("AssignedConfiguration");
    gr_ajax.addParam('sysparm_name', 'assigne');
    gr_ajax.addParam('sysparm_user_name', caller);
    gr_ajax.getXML(HelloWorldParse);

    function HelloWorldParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue("short_description", answer);
    }

}

Server Side Scripting: To available server side to client side must have to enable “Client callable”

var AssignedConfiguration = Class.create();
AssignedConfiguration.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    assigne: function() {
        var objConfig;
        var callerEmail = this.getParameter("sysparm_user_name");
        var assConfig = new GlideRecord("sys_user");
        assConfig.addQuery("sys_id", callerEmail);
        assConfig.query();
        while (assConfig.next()) {
            objConfig = assConfig.email;
        }
        return objConfig;
    },

getReference(): If a callback function is present, this routine runs asynchronously. The browser (and script) processing continues normally until the server returns the reference value, at which time, the callback function is invoked. If a callback function is not present, this routine runs synchronously and processing halts (causing the browser to appear to hang) while waiting on a server response.

Client Side Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var caller = g_form.getReference("assigned_to", doBack);
    function doBack(caller) {
        g_form.setValue("short_description", caller.email);
    }
}

g_scratchpad: To use this we need to have “Display Business Rule”.

Display rules are processed when a user requests a record form.

The data is read from the database, display rules are executed, and the form is presented to the user. The current object is available and represents the record retrieved from the database. Any field changes are temporary since they are not yet submitted to the database. To the client, the form values appear to be the values from the database; there is no indication that the values were modified from a display rule. This is a similar concept to calculated fields.

We will call that business rule in the Client Script as we require

Display Business Rule:

(function executeRule(current, previous /*null when async*/ ) {
    g_scratchpad.email = gs.getUserDisplayName();

})(current, previous);

Client Side Script:

function onLoad() {
    //Type appropriate comment here, and begin script below
    var aler = g_form.setValue("caller_id", g_scratchpad.email);
}

 

Thanks and Regards,

Srikanth

Comments
Sekhar8
Tera Explorer

Thank You! Great info  

Chetan Bavaska1
Kilo Contributor

Great

Community Alums
Not applicable

Thank you, Info was helpful. Can you please tell me out of 3 which one is better to use and why??? 

 

Anish Reghu
Kilo Sage

Good one! One suggestion..

Display rules are processed when a user requests a record form.

The data is read from the database, display rules are executed, and the form is presented to the user. The current object is available and represents the record retrieved from the database. Any field changes are temporary since they are not yet submitted to the database. To the client, the form values appear to be the values from the database; there is no indication that the values were modified from a display rule. This is a similar concept to calculated fields.

I would rewrite the above in simple terms for any fresher to understand it.

When you ask for a record form in ServiceNow, it gets the information from the database and shows it to you in a form. The display rules run and change some parts of the form if needed, but these changes are only temporary and not saved in the database yet. The form looks like it has the original values from the database, even though the display rules might have changed them. This is similar to having calculated fields that change based on other values.

Hope that helps!

 

Cheers,

Anish

Munukuruv
Tera Explorer

Thanks for making it simple and ease to understand the concepts.

Version history
Last update:
‎10-04-2021 02:16 AM
Updated by: