Sync and Async calls

Rose17
Tera Contributor

Hi All,

I have few queries on Async and Sync calls we use in ServiceNow/Javascript

1. How to make synchronous calls to async?

2. What are different ways to make asynchronous calls in Servicenow?

3. How to print JSON with below format.(Show incidents assignment group wise) 

Eg: {"Group1" : [INC1,INC2,INC3],
"Group2" : [INC4,INC5,INC6]
}

@Ankur Bawiskar Your help is highly appreciated.

 

Thanks in advance.

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

 

### Synchronous Calls to Asynchronous:

 

1. **Option 1: Using GlideAjax with "async: false":**

   - GlideAjax is typically used for asynchronous calls, but you can make it synchronous by setting the "async" parameter to false. However, this might lead to performance issues, and it's generally not recommended.

   

var ga = new GlideAjax('YourScriptIncludeName');
    ga.addParam('sysparm_name', 'yourMethodName');
    ga.getXMLWait();
    var response = ga.getAnswer();

   

2. **Option 2: Using Script Includes with "executeNow":**

   - Script Includes are more suitable for synchronous operations. You can instantiate the script include and use the "executeNow" method.

  var myScriptInclude = new YourScriptIncludeName();
    var response = myScriptInclude.yourMethodName();

   

### Asynchronous Calls in ServiceNow:

 

1. **GlideAjax:**

   - As mentioned earlier, GlideAjax is commonly used for asynchronous calls in ServiceNow. It allows you to invoke server-side scripts without a page refresh.

 

2. **AJAX and REST API:**

   - Use client-side AJAX or ServiceNow's REST API for asynchronous communication from the client side. This is often used in UI policies, client scripts, or custom UI pages.

 

   

   // Example of client-side AJAX

   

var xhr = new XMLHttpRequest();
   xhr.open('GET', '/api/now/table/incident', true);
   xhr.setRequestHeader('Content-Type', 'application/json');
   xhr.onreadystatechange = function() {
       if (xhr.readyState == 4 && xhr.status == 200) {
           var response = JSON.parse(xhr.responseText);
           // Process the response
       }
   };
   xhr.send();

 

3. **Background Scripts:**

   - Background scripts allow you to execute server-side scripts asynchronously. These scripts can be scheduled to run at a specific time or interval.

 

### Printing JSON with Grouping:

 

To print JSON with incidents grouped by assignment group, you can use GlideAggregate to query and group the incidents. Here's a simplified example:

 

var result = {};
var gr = new GlideAggregate('incident');
gr.addAggregate('COUNT');
gr.addQuery('active', true);
gr.groupBy('assignment_group');
gr.query();
while (gr.next()) {
    var groupName = gr.assignment_group.getDisplayValue();
    var incidentNumber = gr.number.getDisplayValue();

    if (!result[groupName]) {
        result[groupName] = [];
    }
    result[groupName].push(incidentNumber);
}
gs.info(JSON.stringify(result, null, 4));

 

This script uses GlideAggregate to count incidents grouped by the assignment group. It then creates a JSON object (`result`) where each group has an array of incident numbers. Adjust it according to your specific field names and requirements.

 

Thanks,

Danish

 

View solution in original post

9 REPLIES 9

Danish Bhairag2
Tera Sage
Tera Sage

 

### Synchronous Calls to Asynchronous:

 

1. **Option 1: Using GlideAjax with "async: false":**

   - GlideAjax is typically used for asynchronous calls, but you can make it synchronous by setting the "async" parameter to false. However, this might lead to performance issues, and it's generally not recommended.

   

var ga = new GlideAjax('YourScriptIncludeName');
    ga.addParam('sysparm_name', 'yourMethodName');
    ga.getXMLWait();
    var response = ga.getAnswer();

   

2. **Option 2: Using Script Includes with "executeNow":**

   - Script Includes are more suitable for synchronous operations. You can instantiate the script include and use the "executeNow" method.

  var myScriptInclude = new YourScriptIncludeName();
    var response = myScriptInclude.yourMethodName();

   

### Asynchronous Calls in ServiceNow:

 

1. **GlideAjax:**

   - As mentioned earlier, GlideAjax is commonly used for asynchronous calls in ServiceNow. It allows you to invoke server-side scripts without a page refresh.

 

2. **AJAX and REST API:**

   - Use client-side AJAX or ServiceNow's REST API for asynchronous communication from the client side. This is often used in UI policies, client scripts, or custom UI pages.

 

   

   // Example of client-side AJAX

   

var xhr = new XMLHttpRequest();
   xhr.open('GET', '/api/now/table/incident', true);
   xhr.setRequestHeader('Content-Type', 'application/json');
   xhr.onreadystatechange = function() {
       if (xhr.readyState == 4 && xhr.status == 200) {
           var response = JSON.parse(xhr.responseText);
           // Process the response
       }
   };
   xhr.send();

 

3. **Background Scripts:**

   - Background scripts allow you to execute server-side scripts asynchronously. These scripts can be scheduled to run at a specific time or interval.

 

### Printing JSON with Grouping:

 

To print JSON with incidents grouped by assignment group, you can use GlideAggregate to query and group the incidents. Here's a simplified example:

 

var result = {};
var gr = new GlideAggregate('incident');
gr.addAggregate('COUNT');
gr.addQuery('active', true);
gr.groupBy('assignment_group');
gr.query();
while (gr.next()) {
    var groupName = gr.assignment_group.getDisplayValue();
    var incidentNumber = gr.number.getDisplayValue();

    if (!result[groupName]) {
        result[groupName] = [];
    }
    result[groupName].push(incidentNumber);
}
gs.info(JSON.stringify(result, null, 4));

 

This script uses GlideAggregate to count incidents grouped by the assignment group. It then creates a JSON object (`result`) where each group has an array of incident numbers. Adjust it according to your specific field names and requirements.

 

Thanks,

Danish

 

For 3rd answer, incident numbers are not getting printed. I am getting below output from the script you provided-

Rose17_0-1701853110460.png

 

 

Hi @Rose17 ,

 

For incident numbers to populate plz use below script

 

var result = {};
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query();
while (gr.next()) {
    var groupName = gr.assignment_group.getDisplayValue();
    var incidentNumber = gr.number.getDisplayValue();
    if (!result[groupName]) {
        result[groupName] = [];
    }
    result[groupName].push(incidentNumber);
}
gs.info(JSON.stringify(result, null, 4));

 

Thanks,

Danish

 

@Rose17 ,

 

Attaching the Output FYR

 

DanishBhairag2_0-1701929528691.png

 

Thanks,

Danish