Hi everyone,
I have build Bidirectional e bonding with some cases:
case 1 : Inc created on A will create on B with POST Method, B stored A's sys_id and
A stores B's Sys_id.
POST: Before insert BR:
(function executeRule(current, previous /*null when async*/ ) {
if(current.u_from_integration==true){
return;
}
var r = new sn_ws.RESTMessageV2('SNOW_to_SNOW_Integration', 'Incident Creation');
r.setStringParameterNoEscape('core_id',current.sys_id);// it will store Source sys_id on Target Corelation ID
r.setStringParameterNoEscape('sd', current.short_description);
r.setStringParameterNoEscape('assto', current.assigned_to);
r.setStringParameterNoEscape('des', current.description);
r.setStringParameterNoEscape('caller', current.caller_id);
r.setStringParameterNoEscape('assgrp', current.assignment_group);
r.setStringParameterNoEscape('prt', current.priority);
r.setStringParameterNoEscape('int','true');//it makes sure this comming from inegration
//execute will send the rest message to the target end point and it will stored on varibale called response
var response = r.execute();
var responseBody = response.getBody(); // gets content of REST response body.
gs.log('response from target is ' + responseBody); // for checking purpose.
var prsData = JSON.parse(responseBody); // it convets JSON into string and stores whole result in prsData variable.
var targetIncSysId = prsData.result.sys_id; //JSON will store in result so dotwalking varibale.result.number
current.correlation_id=targetIncSysId;
gs.log('target incident number is ' + targetIncSysId);
var httpStatus = response.getStatusCode(); // gets the HTTP status code
gs.log('response code is ' + httpStatus);
})(current, previous);
Reset flag After Insert BR:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
if(current.u_from_integration==true){
var gr = new GlideRecord('incident');
if(gr.get(current.sys_id)){
gr.u_from_integration=false;
gr.setWorkflow(false);
gr.update();
}
}
})(current, previous);
JSON Payload:
{
"short_description":"${sd}",
"caller_id":"${caller}",
"description":"${des}",
"assignment_group":"${assgrp}",
"assigned_to":"${assto}",
"priority":"${prt}",
"u_from_integration":"${int}",
"correlation_id":"${core_id}"
}
case 2: when inc gets update on A it should get update on B
Before Update BR for PATCH:
(function executeRule(current, previous /*null when async*/ ) {
if(current.u_from_integration==true){
return;
}
// if any of those fileds changes only this code will execute to avoid unnecessary calls.
if (current.short_description.changes() || current.priority.changes() ||
current.comments.changes() || current.assigned_to.changes() ||
current.assignment_group.changes() || current.state.changes()) {
var r = new sn_ws.RESTMessageV2('SNOW_to_SNOW_Integration', 'Update Incident');
r.setStringParameterNoEscape('sys_id', current.correlation_id); // as it was sys_id using the setStringparameter
// objBody will store the whols json payload
var objBody = {
short_description: current.short_description.toString(),// for integrations always use toString() in Json payload otherwise it will not update the field or it will update it with{}
impact: current.impact.toString(),
urgency: current.urgency.toString(),
state: current.state.toString(),
assigned_to: current.assigned_to.getDisplayValue(),
assignment_group: current.assignment_group.getDisplayValue(),
comments: current.comments.getJournalEntry(1)
};
r.setRequestBody(JSON.stringify(objBody)); // it will convert JS into JSON format
var response = r.execute();
var responseBody = response.getBody();
gs.log('target instance response body is ' + responseBody);
var httpStatus = response.getStatusCode();
}
})(current, previous);
RESET Flag After update BR:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
if(current.u_from_integration==true){
var gr = new GlideRecord('incident');
if(gr.get(current.sys_id)){
gr.u_from_integration=false;
gr.setWorkflow(false);
gr.update();
}
}
})(current, previous);
Case 3 is vice versa of case 1, case 4 is vice versa of case 2.
Case 5: when inc created on A it will create on B(replica of A) and if user update on B manuavlly inc on A should also update.
for this i have stored A's sysid on B correlatuin and vice versa let me know what changes i need to do to achieve case 5.
Thanks in Advance.