Skip to content

Commit 353ae10

Browse files
Pre Post Processing Script of Pattern (#1937)
* Create PrePost Pattern designer.js Mapping of reference fields from patterns like location,manufacturer. * Create Pre README.md
1 parent 40dc1cf commit 353ae10

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# ServiceNow Discovery Pre Sensor Script: IP Router Association
2+
3+
This script is a **ServiceNow Discovery Pre Sensor Script** designed to enrich discovery payload data before it reaches the **Identification and Reconciliation Engine (IRE)**.
4+
5+
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.
6+
7+
8+
## Overview
9+
10+
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**.
11+
The IRE requires sys_ids for accurate relationship building.
12+
13+
This script ensures that:
14+
- The `u_configuration_item` field on interface cards is replaced with the actual `sys_id` of the corresponding router.
15+
- The router’s `managed_by_group` field is also copied to the interface card record.
16+
- Payload is properly formatted and ready for IRE ingestion.
17+
18+
## Script Logic
19+
20+
### Step-by-step Flow
21+
1. **Parse Payload:**
22+
Converts the input payload JSON string into an object for processing.
23+
24+
2. **Iterate Items:**
25+
Loops through each payload item and filters those with `className = cmdb_ci_interface_card`.
26+
27+
3. **Router Resolution:**
28+
For each interface card:
29+
- Reads the router name from `u_configuration_item`.
30+
- Searches for a matching router record in `cmdb_ci_ip_router`.
31+
- If found:
32+
- Replaces the router name with its `sys_id`.
33+
- Copies the router’s `managed_by_group` value.
34+
35+
4. **Return Updated Payload:**
36+
Returns the modified payload back to Discovery for further processing by the IRE.
37+
38+
---
39+
40+
## Example Behavior
41+
42+
### **Before Script Execution**
43+
```json
44+
{
45+
"items": [
46+
{
47+
"className": "cmdb_ci_interface_card",
48+
"values": {
49+
"name": "Router-1/Gigabit0/1",
50+
"u_configuration_item": "Router-1"
51+
}
52+
}
53+
]
54+
}
55+
56+
### **After Script Execution**
57+
{
58+
"items": [
59+
{
60+
"className": "cmdb_ci_interface_card",
61+
"values": {
62+
"name": "Router-1/Gigabit0/1",
63+
"u_configuration_item": "1b23cdef6f3123006a12ff3b8b3ee490",
64+
"managed_by_group": "287ebd7da9fe198100f92cc8d1d2154e"
65+
}
66+
}
67+
]
68+
}
69+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Pre sensor: You can change payload before it will be proccesed by Identification Engine.
3+
* Use IEJsonUtility in order to add relevant information to the payload
4+
* Input parameters in Pre sensor mode: payload, patternIds
5+
*/
6+
7+
8+
var rtrn = {};
9+
10+
// parsing the json string to a json object
11+
var payloadObj = JSON.parse(payload);
12+
13+
// Clearing payload string to save memory
14+
payload = null;
15+
16+
// Put your business logic here
17+
var handlegrIpRouterdata = function() {
18+
19+
gs.info('PD: handlegrIpRouter');
20+
21+
var ipRouterName = '';
22+
23+
var payloadItems = payloadObj.items;
24+
25+
for (var i = 0; i < payloadItems.length; i++) {
26+
27+
if (payloadItems[i].className === 'cmdb_ci_interface_card') { //Get Child class data
28+
29+
var currentItem = payloadItems[i];
30+
31+
ipRouterName = currentItem.values.u_configuration_item;
32+
33+
34+
35+
if (ipRouterName && ipRouterName.length) {
36+
37+
var grIpRouter = new GlideRecord('cmdb_ci_ip_router');
38+
39+
if (grIpRouter.get('name', ipRouterName)) {
40+
41+
42+
43+
currentItem.values.u_configuration_item = grIpRouter.sys_id + '';
44+
currentItem.values.managed_by_group = grIpRouter.managed_by_group + '';
45+
46+
}
47+
48+
49+
50+
}
51+
52+
}
53+
54+
}
55+
56+
};
57+
handlegrIpRouterdata();
58+
// For node logger, please use: prePostNodeLogger.info\warn\error\debug(prePostLogPrefix + '<YOUR_LOG_STATEMENT>')
59+
60+
// You can return a message and a status, on top of the input variables that you MUST return.
61+
// Returning the payload as a Json String is mandatory in case of a pre sensor script, and optional in case of post sensor script.
62+
// If you want to terminate the payload processing due to your business logic - you can set isSuccess to false.
63+
rtrn = {
64+
'status': {
65+
'message': 'Enter your message here',
66+
'isSuccess' :true
67+
},
68+
'patternId': patternId,
69+
'payload': JSON.stringify(payloadObj)
70+
};

0 commit comments

Comments
 (0)