Before subscribe scripts


A Before subcsribe script in Perspectium DataSync Express allows the current record that is being subscribed to be processed after it is loaded into a GlideRecord, but before it is saved into the target table in ServiceNow. Before subscribe scripts expect server side javascript and is preconfigured with a globally available variable called current that is the GlideRecord object that is going to be subscribed into the target table.


After subscribe scripts


An After subscribe script allows you to specify a script that will run after your subscribe has saved the record into the target table. This can be useful for when you want to do some additional processing after your subscribe has run.

(info) NOTEChanging any values in a variable in the After subscribe script will not update and save the value (i.e. setting current.short_description = "test" will not actually update the short_description value in the target incident table) since the After subscribe script is used for reference or to do other actions.

Prerequisites


(warning) First, you will need to get started with DataSync Express.

Procedure

To create before/after subscribe scripts, follow these steps:


Create a new Subscribe

Log into your ServiceNow instance and navigate to Perspectium DataSync Express > DataSync Express > Subscribe > New to create a new subscribe.

Type your Before/After subscribescript

For both Before share script and After share script options, go to the Scripting tab. Then, type your before subscribe script or after subscribe script in the associated scripting window with the following variables available for you to use.

Click to reveal variables available to use

Variables to use


Variable

Description

Accessible in Before subscribe script

Accessible in After subscribe script

current

The GlideRecord representing the current record to be subscribed, similar to how you access the current record in a business rule. This allows you to make changes to the current record before it is saved into the target table (when accessing it in Before subscribe script) or to reference it for other variables listed here and to do other scripting.

For example, if you want to update the short_description of an incident record being subscribed to something else based on the priority:

if (current.priority == 5) {
   current.short_description = "Low priority item";
}
(tick)(tick)

qcurrent

The GlideRecord of the x_perr2_datasync_psp_in_message record to represent the message as read from the Integration Mesh. This way you can access fields such as topic, type, key, name and value.

For example, if you wanted to update the short_description of an incident based on the message key with the key representing different instances you're subscribing to data from:

if (qcurrent.key == "acmeprod") {
 current.short_description = "From customer ACME";
}
(tick)(tick)

subscribe_gr

To access the subscribe configuration record.

For example, the following will log the value of the subscribe record's system id:

gs.log(subscribe_gr.sys_id);

(tick)(tick)
ignore

To prevent the current record from being subscribed into the target table of the subscribe configuration, you can set the variable ignore to true in the Before subscribe script.

For example, the following script ignores subscribing any incident record when the priority field value is 1:

if (current.priority == 1) {
    ignore = true;
}

(tick)(error)
gr_before

Record in the target table as it currently exists before any update is made to it with this incoming subscribe record. If the record doesn't exist (i.e., for an insert), then this variable will be the same as the current variable.

(tick)(error)

repl_gr

A GlideRecord representing the temporary inbound record (i.e. the record as it appears in the value field of the incoming x_perr2_datasync_psp_in_message record) before it is saved into the current variable available here.

This is useful if you want to see the raw content as first incoming into the instance from the Integration Mesh before we create the GlideRecord as it will be saved into the target table.

(tick)(error)

Click Update

Near the bottom left-hand corner of the form (just above Related Links), click Update to save the changes to your dynamic share.