kennycaldwe
ServiceNow Employee

This post is part of a two-part post. The other post can be found here Service Bridge: Communicate to source instance when target instance blocks an update . When updates are synced with Service Bridge from one instance to another instance, we can think of it as sending data from a source instance and a target instance. The accompanying post details how to sync information back to a source instance from a target instance, while processing a sync transaction on the target instance. These two posts are used together to create a complete solution. See notes at bottom of this article.

 

Introduction

Service Bridge Transforms can be used with Remote Task Definition. They are used with Inbound and Outbound fields to transform inbound and outbound data. Inbound and Outbound fields are mappings between a Provider and Consumer Instance. A Provider and Consumer Instance can be either a source or target. The instance that sends the data is the source and the instance receiving that data is the target. The “input” variable in a transform contains data from the source instance. The “input” variable can be transformed, used in conditions, or passed through the transform. The “output” variable in a transform contains data that will update the target instance. Whether inbound or outbound the “input” variable always holds data from the source and the “output” variable is used to update the target.

 

Service Bridge Transforms work with Remote Task Definition, used with Inbound and Outbound fields to transform the data mapped between Provider and Consumer instances. The source instance sends data, while the target receives it. The “input” variable holds source data, which can be transformed or used in conditions before updating the target via the “output” variable. Regardless of inbound or outbound transform, “input” is always from the source, and “output” updates the target.

 

Challenge

Service Bridge transforms run one time on creation of a remote task and parent record but runs two times on updates. Running the transform two times on an update can cause conditions within a transform to evaluate to true on both runs which can create false positives for the condition.

 

Why does Service Bridge run two times for updates?

During a sync from a source to a target instance all mapped fields are sent in the payload. The transform runs two times to determine which of the mapped fields have changed so that only the changed fields are written to the work notes of the parent record.

 

On the first run of a transform the remote task and parent are updated with the “output.value”. On the second run of a transform the remote task and parent task are not updated. The previous “input.value” is transformed to compare values with the current transformed “input.value” to determine if the current value has changed from the previous value. All of the mapped fields that have changed are collected and written to the work notes field of the parent record.

 

Example of how a condition in a transform can result in a false positive

Consider a scenario involving a remote task definition where both the Provider and Consumer tables are configured as "incident." An inbound mapping is established to align the state field of the Provider incident with that of the Consumer incident. Additionally, an inbound transform on the Provider instance includes a conditional statement: if (input.value == '3') to run a section of code when the condition is met.

 

Transform condition statement.png

A consumer incident is linked to a provider incident through the remote task definition. The consumer changes the incident status from In Progress (value ‘2’) to On Hold (value ‘3’).

 

  • On the first run of the transform, input.value is 3, which satisfies the condition where state equals '3'.
  • On the second run, input.value is 2, which does not satisfy state == ‘3’.

So far, so good – however...

 

When the consumer changes the incident state from On Hold (value ‘3’) back to In Progress (value ‘2’).

 

  • On the first run of the transform, input.value is 2, which does not satisfy the condition state == '3'.
  • On the second run, input.value is 3, which satisfies the condition state == ‘3’.

The consumer changing state from On Hold to In Progress should not have triggered the condition in the transform and results in a false positive.

 

Solution

Determine whether the transform is in its first or second pass, and adjust the condition to evaluate true if input.value == '3' and it's the first pass. Also, identify if the operation is a create (runs once) or update (runs twice), as this affects how the transform executes.

This can be accomplished by testing various conditions to determine if the operation is a create or an update. If the operation is an update, a scratchpad entry is used: create it on the first pass, then remove it on the second.

 

Attached to this post is an example transform calling the isFirstPass method and using the results in the condition. Also attached is the ServiceBridgeTransformMethods script include.

 

Calling isFirstPass method and adding to condition.png

Logging is included which displays the line number of the log and the pass number which represent the transform running on the first pass or second pass.

 

After making a state update on the consumer the following filter can be applied to see the results of the transform. Sorting on created and message puts them in order.

 

Logging Printout.png

Logging is turned on by default.

 

enable - disable logging.png

Logging can be turned off by creating the system property ‘sbtm.logging.enabled’ and setting the value to false.

 

Create Logging Property.png

Notes

  • Sample scripts for demonstration purposes. Please test thoroughly in your sub-production environment and modify as needed for your specific use case. Use at your own discretion.
  • Attached is an example Service Bridge Provider transform, which contains a call to the method isFirstPass.
  • Attached is an example Global Script Include, ServiceBridgeTransformMethods, which features the addConsumerComment method.
  • The attachments are components of the complete solution discussed in this two-part post, and each post includes these attachments.
Version history
Last update:
Wednesday
Updated by:
Contributors