Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Pre-Post Processing Script - To perform lookup on a reference field

gagribben
Tera Contributor

Hello, I will start off by saying that my scripting skills are probably my weakest area in ServiceNow. I have a scenario where I have created a custom Discovery Pattern that is discovering an HP iLO (physical card installed on a server that allows remote management/console) via HTTP and creating an out-of-band device, this is working as intended. However, I would also like to perform a lookup against another table (potentially cmdb_ci_computer) and populate the host field on the cmdb_ci_outofband_device table. For example, if I am able to discover an HP ilo Device, name:hp_ilo and this device resides on a server, name:ilo_host. I would want to populate the hostfield in the screenshot with the server name. I believe this can be accomplished by a pre-post processing script. I am not exactly sure where to start or how to accomplish this. Thanks in advance for any advice or assistance. 

18 REPLIES 18

Doug, I was actually following that article, which got me to the point I am now. It was extremely helpful when building the HTTP classifier and hitting the HP iLO API. 

Doci1
Kilo Sage

Hello @gagribben , here is you Pattern pre/post script! 

/*
* 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, patternId
*/

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
// For node logger, please use: prePostNodeLogger.info\warn\error\debug(prePostLogPrefix + '<YOUR_LOG_STATEMENT>')

var msg = 'Host poulation completed succesfully.';
var iloDeviceName;
var server;
var success = true;
var payloadItems = payloadObj.items;

for (var i = 0; i < payloadItems.length; i++) {
if (payloadItems[i].className === 'cmdb_ci_outofband_device') {
var currentItem = payloadItems[i];
iloDeviceName = currentItem.values.host; << you have to change the "host" according to result

if(iloDeviceName.length) {
var computerGR = new GlideRecord('cmdb_ci_computer');
if (computerGR.get('name', iloDeviceName)) {
 
currentItem.values.host = computerGR.getUniqueValue();
msg += 'Host: ' + iloDeviceName + ' found ';
} else {
msg = ('Unable to find host by serial number. HostName: ');
success = true;
}

}
}
}

// 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': msg,
'isSuccess': success
},
'patternId': patternId,
'payload': JSON.stringify(payloadObj)
};

gagribben
Tera Contributor

@Doci1 I am certainly willing to give it a try, this may be a silly question, but the pre processing script would be used in place of the post? I feel like I am not explaining the requirement properly.  Let me show you an example of where I am and where I would like to be. There are 3 screenshots. The screenshot labeled Current shows the current CI that I am creating with the pattern, with the host name failing to be populated. The second screenshot labeled Desired shows what we would like to capture based on the value being captured and assigned to the variable in the pattern. The screenshot labeled Pattern, is the pattern where I am returning the desired value and assigning it to the sysServer variable in the pattern. I am having a hard time understanding how do we pass the variable in the pattern to do the lookup on the cmdb_ci_computer table and return the sysid based on the sysServer name. 

Hello @gagribben , my script does exactly what you have mentioned. You have the "sysServer" variable, in other term the host name. What you have to do is to find that record in CMDB, simply query CMDB for the hostname and get the sysID of the hostname. It is not working that you will simply provide the name of the hostname and SN will do the magic. 

Therefore you have the Pre/Post script, where you can do all the logic. Query [cmdb_ci_computer], return sysID, provide it into cmdb_ci_outofband_device.host

Believe, I did the same for xCLarity last week 😉

Just mark my answers helpful if you will find them helpful please 😉