User Tools

Site Tools


before_subscribe_script

Before Subscribe Script

About

This script will execute right before an insert or update allowing a chance to modify the record before persisting. Within your script you have access to the following variables:

Variable Description
current This represents the record that is being inserted or updated, the destination record
repl_gr This is the temporary inbound record, by default repl_gr will just be mapped into current
gr_before This represents the record before any update is made to it. If the record doesn't exist (you are doing an insert) then it will be assigned to current
qcurrent This represents the record within the psp_in_message table, the record pulled in from the message bus. With this you can use it's key value to determine the source
ignore v3.15.0 You can set the variable ignore to true in order to stop the execution of the Subscribe
qcurrentxml v3.25.0 This variable holds the xml object of the inbound record
xml_util v3.25.0 This variable holds an xml utility for working with qcurrentxml

With these variables you can perform a wide array of actions based on your needs.

Examples

Populating the data using the incoming key

Perhaps you are receiving data from multiple sources and you want to denote the source in some manner. You can use the qcurrent.key value when updating your record.

The following script prefixes the short description with the key

current.short_description = qcurrent.key + " - " + current.short_description;

Mapping data within a record

For example, to set the short_description field to the incoming record's number field, you would do this:

current.short_description = repl_gr.number;

Dot-walking

You can use dot-walking to display values from a referenced field.

For example, if you were subscribing to a ticket record, in the before subscribe script text box, you can reference a field by using the following script:

  current.name = repl_gr.assigned_to.name

This will display the display_value versus just the sys_id.

Using gr_before to fire an Event

For the case where you are updating a record that already exists in the system, “gr_before” is a GlideRecord object for this record before doing the update. This is useful for when you want to compare a record's values as they currently exist in the system with the values in the incoming record (repl_gr) and do any processing as a result.

For example, to fire the incident table's event when the assignment group is changed (the “incident.assigned.to.group” event as specified in the incident table's “incident events” business rule), you would do the following:

if (gr_before.assignment_group != repl_gr.assignment_group) {
  gs.eventQueue("incident.assigned.to.group", current, current.assignment_group, previous.assignment_group.getDisplayValue());
}

Using ignore

For example the following script from a ticket subscribe, will ignore a specific ticket with the number value of TKT0010001.

if (current.number == "TKT0010001") {
    ignore = true;
}

Users can also filter records from different instances by checking the key value. For example, the following script will ignore any records with the key value of “dev14945”.

if (qcurrent.key == "dev14945") {
    ignore = true;
}

Running Script Before a Delete

v3.20.0

Support has been added so you can run script before doing a delete such as inserting a record into another table. In these cases you should use “repl_gr” to access the incoming record in case the incoming record doesn't exist in the instance.

For example, say you receive a delete but you want to insert a record into another table instead, and not do an actual delete:

var tgr = new GlideRecord("incident");
tgr.short_description = repl_gr.short_description + " new";
tgr.insert();
ignore = true;

Firing Replication through Subscribe

There are some cases where you want to replicate a message right after Subscribing it in. In most cases you can select “Run Business Rules” to treat this replication as a normal record transaction and our Dynamic Share's Business Rule should fall in line. If however you cannot use this on the Subscribe than you can execute the following command.

var pspRepl = new PerspectiumReplicator();
//sys_id of desired Dynamic Share
pspRepl.shareRecord(current, "incident", "bulk", "d24f961b4f043200daa12ed18110c72d");
ignore = false;

This is essentially the code that gets executed from the Business Rule for a Dynamic Share. With this you will put in the current record, the table name, the flag “bulk”, and the sys_id of the Dynamic Share you want this to route through.

It is also important to add in the (ignore = false;) statement to the Subscribe. There are cases where the execution of the Dynamic Share's “Before Share Script” can cause the Subscribe to skip the update. Adding in this command will ensure the record gets committed to the Subscribing instance.

Maintaining a Column's Value

If you want to lock a column from being changed through the Subscribe you can put in the following line to set this column to it's “previous” value. Effectively setting it to itself so any change coming in won't take affect.

current.short_description = gr_before.short_description;

Accessing the XML Object of the Inbound Record

v3.25.0

For cases where the inbound record is different from the subscribing table (i.e. it has extra fields that the table on the subscribing instance doesn't have), you can now reference the XML object directly using the qcurrentxml variable.

The xml_util variable allows you to access values from the qcurrentxml object using getElementValueByTagName() function. This functions takes in two parameters, the XML object (qcurrentxml) and the name of the field in the XML.

For example, if you want to access the 'dv_priority' field's value in the XML and save it into the inbound record's short_description field:

if (qcurrentxml) {
    var elemValue = xml_util.getElementValueByTagName(qcurrentxml, 'dv_priority');
    current.short_description = elemValue;
}

Note: It is suggested that you check if qcurrentxml exists in case of any issues decrypting the value and getting the XML object.

before_subscribe_script.txt · Last modified: 2017/07/19 12:27 by ashkan.ghafari