Restrict Data insert in ServiceNow Table through SG-Workspace One Integration
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2024 01:00 PM
We have implemented 'SG-Workspace ONE UEM Devices and Apps' integration using Service Graph
connectors and it populates the data in target table named 'cmdb_ci_computer' everytime when Scheduled Data Import
(below) executed
Data Source -
Script:
(function loadData(import_set_table) {
var auth_type = gs.getProperty('sn_vmwoneuem_integ.auth_type', 'oauth');
var connectionAliasGr = "";
var devConnectionAliasSysId = "";
var apiKeyGr= "";
if (auth_type == 'oauth') {
devConnectionAliasSysId = "4f49c8225340201091b8ddeeff7b121f";
connectionAliasGr = new GlideRecord("sys_alias");
connectionAliasGr.get(devConnectionAliasSysId);
} else {
devConnectionAliasSysId = '4faa28b553113010f306ddeeff7b1221';
var devAPIKeySysId = '458876ce53593010f306ddeeff7b12b9';
connectionAliasGr = new GlideRecord("sys_alias");
connectionAliasGr.get(devConnectionAliasSysId);
apiKeyGr = new GlideRecord("api_key_credentials");
apiKeyGr.query("sys_id", devAPIKeySysId);
apiKeyGr.query();
if (!apiKeyGr.next()) {
gs.error(gs.getMessage("SG-WS1 API Key(api_key_credentials) record not found"));
throw "Unable to find connection alias with sys_id " + apiKeySysId;
}
}
new SG_WS1_UEM_Datasource_Helper(import_set_table, connectionAliasGr, apiKeyGr, auth_type).loadData();
})(import_set_table);
In 'SG-Workspace ONE UEM Devices and Apps' Plugin, Robust Transform map is used, In normal transform map we can write onBefore script to achieve this.
Here I know that need to modify 'CMDB Integration Studio Application Data Source'
by marking check to 'Execute before script' (below)
Script:
/**
*
* Script executed for a given batch before calling the Identification and Reconciliation Engine (IRE)
*
* The following variables are available to the script:
* 'input' - the array where each element of the input array is an object. Each object contains a status, reason, and payload.
* 'runId' - the current import set run id.
*
* Example:
*/
// (function(input, runId) {
// for (var i = 0; i < input.length; i++) {
// // Skip all payloads which have a computer starting with name as 'TEST-'
// if (!isComputerNameValid(input[i].payload)) {
// input[i].status = 'SKIPPED';
// input[i].reason = 'Skipping IRE processing of this payload.';
// } else {
// // Add new value to items
// input[i].payload.items[0].values.short_description = 'Adding missing description';
// // Add a new relationship by passing a JSON object (Use unshift() to add a relationship at the beginning)
// // Similarly, any relation can be removed by using the pop() or shift() methods
// var relation = {
// 'parent': 0,
// 'child': 1,
// 'type': 'Runs on::Runs'
// };
// input[i].payload.relations.push(relation);
// // Update an existing reference item by passing a JSON object.
// // Similarly, the entire reference items array can also be updated e.g. input[i].payload.referenceItems = {...}
// input[i].payload.referenceItems[0] = {
// 'referenceField': 'software',
// 'referencedBy': 'internal_id2',
// 'referenced': 'internal_id1'
// };
// }
// }
// })(input, runId);
// function isComputerNameValid(payload) {
// for (var i = 0; i < payload.items.length; i++)
// if (payload.items[i].className == 'cmdb_ci_computer' && payload.items[i].values.name.startsWith('TEST-'))
// return false;
// return true;
// }
/**
* The following properties/functions are available for all parameters of type 'Array' inside the IRE payload e.g. items, relations, referenceItems, lookup and related:
* length - Returns the number of elements in an array
* push() - Add an item to the end of an array
* pop() - Remove an item from the end of an array
* shift() - Remove an item from the beginning of an array
* unshift() - Add an item to the beginning of an array
*
* Important points:
* 1) The before script is executed once per batch and not per payload. The input array elements contain all the payloads for a given batch.
* 2) Each element in the array has only 3 fields i.e. status, reason, and payload. Adding additional fields to the array elements might result in unexpected errors.
* 3) The 'status' can be set to 'SKIPPED' in order to skip a payload in this batch from further processing. Setting 'status' to any other value will be ignored by the RTE processor.
* 4) Optionally, a 'reason' can be set for skipping the payload. This will be the message on the import set run for this particular row.
* 5) To match the batch size after script execution, only the length property is available for input array e.g. input.length
* 6) Using any of the above functions e.g. push(), pop(), shift() and unshift() on input array directly will result in unexpected errors.
*
* Below is an example of an IRE input payload. There can be more fields/parameters depending on each use case. Refer to the ServiceNow Product Documentation for more examples on the IRE payload.
* {
* 'items': [
* {
* 'className': 'cmdb_ci_computer',
* 'values': {
* 'name': 'Macbook Pro 15',
* 'os_version': '5.1.2600',
* 'ram': '2048',
* 'disk_space': '1024'
* }
* }
* ]
* }
*/
(function(input, runId) {
// Add your code here
})(input, runId);
My requirement is that,
Need to restrict insert operation of data from workspace one to computer table, only allow to update the data based on 'imei' and 'phone number' fields, i.e., if imei and phone number matched then only update the data, if it does not then prevent the data insert and create a task and assign that task to 'XYZ' group.
I have wrote something like
var handHeld = new GlideRecord('cmdb_ci_computer');
handHeld.addQuery('imei',source.u_imei_1);
handHeld.addQuery('phone_number',source.u_phone_number);
handHeld.query();
if(!handHeld.next()){
target.setAbortAction(true);
}
But it is not working and also I am not aware about how to write a script to create task
Thanks