- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 11:27 PM - edited 12-05-2023 11:29 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 11:34 PM - edited 12-05-2023 11:38 PM
### 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 06:16 AM
Can you explain these lines of code?
if (!result[groupName]) { result[groupName] = []; } result[groupName].push(incidentNumber);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 06:39 AM - edited 12-07-2023 06:40 AM
Hi @Rose17 ,
Certainly! These lines of code are checking if an array exists in the `result` object under the key `groupName`. If it doesn't exist, it creates an empty array for that key. Afterward, it pushes the value of `incidentNumber` into the array associated with the `groupName` key.
Here's a breakdown:
1. `if (!result[groupName])`: This condition checks if there is no array associated with the key `groupName` in the `result` object.
2. If the condition is true (i.e., there is no array for `groupName`), it creates an empty array for that key:
result[groupName] = [];
3. Finally, it pushes the value of `incidentNumber` into the array associated with the `groupName` key:
result[groupName].push(incidentNumber);
In summary, these lines ensure that there is an array for each `groupName` in the `result` object, and it appends `incidentNumber` to that array.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 09:25 AM
Hi @Rose17 ,
If I have answered your questions plz mark the responses as Solution Accepted & Helpful.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 08:16 PM
Thank you 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 09:53 PM
@Ankur Bawiskar Could you please help here?