The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Patrick DeCarl1
ServiceNow Employee
ServiceNow Employee

For those who have made the jump to creating new discovery patterns to replace you probes/sensors or new patterns in general you will find its hard to set a reference field value via the pattern for fields that are not CI base. Ex: location, company, Assigned to and etc.

However, there is a feature that is not on the doc site nor developer site call Pre/Post Processing Scripts. In the nav menu under Pattern Designer -> Pre Post Processing you will see 20 plus OOTB scripts already in use for OOTB patterns. The script allows you to select when the code needs to execute by three options.

1. Pre Execution:

Pre execution: This script will run before the execution of the assigned pattern/sit allows the user to add data that can be accessed by the running the pattern this is done by adding variables to the scriptable PrePatternExecutionData object below is an example of the possible variables that can be added.

2. Pre sensor:

You can change payload before it will be processed by Identification Engine. Use IEJsonUtility in order to add relevant information to the payload Input parameters in Pre sensor mode: payload, patternId

3. Post sensor:

You can update/add missing info to the DB based on result (Json) from Identification Engine Output parameters in Post sensor mode: payload

So why is this important?

When you use pattern step to set a reference field value you will not have a sys_id of the reference record. For example, I want to sent location field during my discovery(Outside the Discovery Schedule options)of devices or application. The devices/app only knows the location value by the string form and doesnt know anything about SN and sys_ids. In the pattern you set the field value with the string value seen below, but when record gets updated all you have is a reference field with blank value but via the xml you see the string value. So what can you do to fix this issue?

Identification_for_HTTP_S__entry_point_type_s__for_IIS7_and_above_-_IIS_and_Inbox_•_Evergreensys.png

(Yes, I know this is probably a bad example but it gets the job done.)

For this example, I choose to make a Pre Sensor executed script. My goal is to update the payload to replace string value with sys_id of location record before the identification engine runs to create/update CI record within SN.

Before we take action to update the payload you will see in below screenshot the location data coming over as string value.

https___dev31064_service-now_com_ecc_queue_do_sys_id_4561e5ffdb0c13002a98d740cf96195c_sys_target_payload_XML__sysparm_domain_restore_false_sysparm_stack_no_and_HorizontalDiscoveryProbe___ServiceNow.p

Create new pre/post script. Make sure you select the pattern(s) it needs to run after and when to run.

TEST_Location___ServiceNow.png

In the script(below) we need to read payload and update the location value(lines 18 thru 36) with sys_id that matches the location record within SN.

/*

*   1. 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

*   2. Post sensor: You can update/add missing info to the DB based on result (Json) from

*         Identification Engine

*         Output parameters in Post sensor mode: payload

*

*/

var rtrn = {};

//parsing the json string to a json object

var payloadObj = JSON.parse(payload);

//put your business logic here

var handleLocationdata = function(){

//gs.info('PD: handleLocationdata = Running');

var location = '';

var payloadItems = payloadObj.items;

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

if (payloadItems[i].className === 'cmdb_ci_microsoft_iis_web_server') { //Get location key under the class

var currentItem = payloadItems[i];

location = currentItem.values.location;

// gs.info('PD: handleLocationdata = location   == '+location);

if(location && location.length){

var loc = new GlideRecord('cmn_location');

if(loc.get('name', location)){ //look up location sys_id by location name field.

//gs.info('PD: handleLocationdata = location   == '+loc.sys_id);

currentItem.values.location = loc.sys_id+''; // need to fix.

}

}

}

}

};

handleLocationdata();

//gs.info('PD: handleLocationdata =   JSON'+ JSON.stringify(payloadObj)); //output of the new payload

//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 isSucess to false.

rtrn = {'status':{

'message':'Enter your message here',

'isSuccess':true

},

'patternId':patternId,

'payload':JSON.stringify(payloadObj)

};

After script runs you can see in the log output(below) on line 38, the script updated the payload value with sys_id of location record within SN.

Log___ServiceNow.png

I hope above example helps. I will make updates to my post when I get more information.

34 Comments