Bonding to existing records

Sometimes there is a need to receive messages from an external system for records which exist in your system, but aren't bonded in Unifi.

To do this setup an inbound create message for the target integration, this will ensure that the bond is created. Add the code below to the top of your stage to target script, before any auto generated code. It will look up the existing record and repoint Unifi to use that record instead of creating a new one.

var existing = new GlideRecord('change_request');
existing.addQuery(...); // Your look up code here - assuming other system knows ServiceNow sys_id/number
existing.query();

if (!existing.next()) {
  throw 'Target record with ID ' + /*whatever you use for your id here*/ + ' not found.';
}

target.$model.setRecord(existing);
target = target.$model.getRecord();
bond.setTarget(target.$model);
bond.setValue('internal_reference', target.number);
transaction.$model.setTarget(target);

bond.shouldCommitTarget(false);
bond.commit();
bond.shouldCommitTarget(true);