Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Specialized Areas/ITOM/Discovery/Pre README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# ServiceNow Discovery Pre Sensor Script: IP Router Association

This script is a **ServiceNow Discovery Pre Sensor Script** designed to enrich discovery payload data before it reaches the **Identification and Reconciliation Engine (IRE)**.

It focuses on linking **Interface Cards (`cmdb_ci_interface_card`)** to their parent **IP Routers (`cmdb_ci_ip_router`)** by resolving the router name to its corresponding `sys_id` in the CMDB.


## Overview

When ServiceNow Discovery runs a pattern that identifies multiple components (e.g., routers and their interface cards), some payload items may contain **router names instead of sys_ids**.
The IRE requires sys_ids for accurate relationship building.

This script ensures that:
- The `u_configuration_item` field on interface cards is replaced with the actual `sys_id` of the corresponding router.
- The router’s `managed_by_group` field is also copied to the interface card record.
- Payload is properly formatted and ready for IRE ingestion.

## Script Logic

### Step-by-step Flow
1. **Parse Payload:**
Converts the input payload JSON string into an object for processing.

2. **Iterate Items:**
Loops through each payload item and filters those with `className = cmdb_ci_interface_card`.

3. **Router Resolution:**
For each interface card:
- Reads the router name from `u_configuration_item`.
- Searches for a matching router record in `cmdb_ci_ip_router`.
- If found:
- Replaces the router name with its `sys_id`.
- Copies the router’s `managed_by_group` value.

4. **Return Updated Payload:**
Returns the modified payload back to Discovery for further processing by the IRE.

---

## Example Behavior

### **Before Script Execution**
```json
{
"items": [
{
"className": "cmdb_ci_interface_card",
"values": {
"name": "Router-1/Gigabit0/1",
"u_configuration_item": "Router-1"
}
}
]
}

### **After Script Execution**
{
"items": [
{
"className": "cmdb_ci_interface_card",
"values": {
"name": "Router-1/Gigabit0/1",
"u_configuration_item": "1b23cdef6f3123006a12ff3b8b3ee490",
"managed_by_group": "287ebd7da9fe198100f92cc8d1d2154e"
}
}
]
}

70 changes: 70 additions & 0 deletions Specialized Areas/ITOM/Discovery/PrePost Pattern designer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Pre sensor: You can change payload before it will be proccesed by Identification Engine.
* Use IEJsonUtility in order to add relevant information to the payload
* Input parameters in Pre sensor mode: payload, patternIds
*/


var rtrn = {};

// parsing the json string to a json object
var payloadObj = JSON.parse(payload);

// Clearing payload string to save memory
payload = null;

// Put your business logic here
var handlegrIpRouterdata = function() {

gs.info('PD: handlegrIpRouter');

var ipRouterName = '';

var payloadItems = payloadObj.items;

for (var i = 0; i < payloadItems.length; i++) {

if (payloadItems[i].className === 'cmdb_ci_interface_card') { //Get Child class data

var currentItem = payloadItems[i];

ipRouterName = currentItem.values.u_configuration_item;



if (ipRouterName && ipRouterName.length) {

var grIpRouter = new GlideRecord('cmdb_ci_ip_router');

if (grIpRouter.get('name', ipRouterName)) {



currentItem.values.u_configuration_item = grIpRouter.sys_id + '';
currentItem.values.managed_by_group = grIpRouter.managed_by_group + '';

}



}

}

}

};
handlegrIpRouterdata();
// For node logger, please use: prePostNodeLogger.info\warn\error\debug(prePostLogPrefix + '<YOUR_LOG_STATEMENT>')

// You can return a message and a status, on top of the input variables that you MUST return.
// Returning the payload as a Json String is mandatory in case of a pre sensor script, and optional in case of post sensor script.
// If you want to terminate the payload processing due to your business logic - you can set isSuccess to false.
rtrn = {
'status': {
'message': 'Enter your message here',
'isSuccess' :true
},
'patternId': patternId,
'payload': JSON.stringify(payloadObj)
};
Loading