Create a business rule to establish a relationship between Application Service and Business Service
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 03:56 PM
Dear Experts,
I am trying to establish a relationship between app service and bus service based on the data present in a field (App Service) of the application service. I have created the script and created the type which will be needed for establishing the relationship. lot of restrictions are present in the client ServiceNow, which prevents me from adding a test application service and test business service to run the relationship mapping. Hence checking how this script can be best optimized. Also one more condition I have to put is to make the Operational status as non- operations in the application and business table so to say. Can someone guide ?
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('parent', current.getValue('application_service.sys_id'));
gr.addQuery('child', current.getValue('business_application.sys_id'));
gr.addQuery('type', '7959e7c41b684a90354d96859b4bcbff');
gr.query();
//gs.info(' NB ' + gr.getRowCount());
if (gr.next()) {
//gs.info(' NBIf ');
gr.setValue('parent', current.getValue('application_service.sys_id'));
gr.setValue('child', current.getValue('business_application.sys_id'));
gr.setValue('type', '7959e7c41b684a90354d96859b4bcbff');
gr.update();
} else {
gs.info(' NBelse ');
gr.initialize();
gr.setValue('parent', current.getValue('application_service.sys_id'));
gr.setValue('child', current.getValue('business_application.sys_id'));
gr.setValue('type', '7959e7c41b684a90354d96859b4bcbff');
gr.insert();
}
})(current, previous);
(
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 09:46 PM
Hi @Nilanjan1
Step 1: Define Relationship Type and Fields
Define the relationship type (if not already defined). Make sure you have the sys_id of the relationship type and know the field names you are mapping (for both the app and business service).
Step 2: Script Logic
Your script should include logic to retrieve relevant records from the application service and business service based on your criteria, and incorporate the operational status check into this query.
Step 3: Create or Update Relationship Records
For each matching pair, create or update cmdb_rel_ci records that represent the relationship between the application service and business service.
var AppToBusServiceRelationship = Class.create();
AppToBusServiceRelationship.prototype = {
initialize: function() {
this.relationshipTypeSysId = ‘YOUR_RELATIONSHIP_TYPE_SYS_ID’; // Replace with your type’s sys_id
},
establishRelationship: function () {
// Query the Application Service table for records based on some field that holds the relevant data
var appServiceGR = new GlideRecord(‘cmdb_ci_service_discovered’); // Replace with actual table name if different
appServiceGR.addQuery(‘operational_status’, ‘!=’, ‘non-operational’); // Adjust field and value accordingly
appServiceGR.query();
while (appServiceGR.next()) {
// Extract the data which will be used to find the matching Business Service
var matchingFieldData = appServiceGR.getValue(‘your_matching_field’); // Adjust ‘your_matching_field’ accordingly
// Query the Business Service table for records that match the criteria based on app service data
var busServiceGR = new GlideRecord(‘cmdb_ci_service’); // Replace with actual table name if different
busServiceGR.addQuery(‘your_corresponding_field’, matchingFieldData); // Replace ‘your_corresponding_field’
busServiceGR.addQuery(‘operational_status’, ‘!=’, ‘non-operational’);
busServiceGR.query();
if (busServiceGR.next()) {
// If a Business Service is found, create/update the relationship record
this.createOrUpdateRelationship(appServiceGR.getUniqueValue(), busServiceGR.getUniqueValue());
}
}
},
createOrUpdateRelationship: function (appServiceSysId, busServiceSysId) {
// Check if the relationship already exists
var relGR = new GlideRecord(‘cmdb_rel_ci’);
relGR.addQuery(‘parent’, appServiceSysId);
relGR.addQuery(‘child’, busServiceSysId);
relGR.addQuery(‘type’, this.relationshipTypeSysId);
relGR.query();
if (!relGR.next()) {
// If it doesn’t exist, create a new relationship record
relGR.initialize();
relGR.setValue(‘parent’, appServiceSysId);
relGR.setValue(‘child’, busServiceSysId);
relGR.setValue(‘type’, this.relationshipTypeSysId);
relGR.insert();
}
// Else, the relationship already exists and we don’t need to do anything, or you can update it if needed
},
type: ‘AppToBusServiceRelationship’
};
Please Mark this as Helpful/ Solved if this solves your query
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 09:19 PM
Hello Deepak,
Thank you so much for the reply. however when I am trying to get the relationship established by using these two fields as a reference to each other, the script is failing. These fields are 'u_application_release_cit_id' in which value should match to the business application correlation field. When I ran the background script all the gs.log are getting validated but giving me an error at the end. Can you guide through this ?
Error after running the background script
When to Run Tab is updated with the following
var AppToBusServiceRelationship = Class.create();
AppToBusServiceRelationship.prototype = {
initialize: function() {
this.relationshipTypeSysId = '7959e7c41b684a90354d96859b4bcbff';
},
establishRelationship: function () {
// Query the Application Service table for records based on some field that holds the relevant data
var appServiceGR = new GlideRecord('cmdb_ci_service_auto');
appServiceGR.addQuery('operational_status', '!=', '2');
appServiceGR.query();
while (appServiceGR.next()) {
// Extract the data which will be used to find the matching Business Service
var matchingFieldData = appServiceGR.getValue('u_application_release_cit_id');
// Query the Business Service table for records that match the criteria based on app service data
var busServiceGR = new GlideRecord('cmdb_ci_business_app');
busServiceGR.addQuery('correlation_id', matchingFieldData);
busServiceGR.addQuery('operational_status', '!=', '2');
busServiceGR.query();
if (busServiceGR.next()) {
// If a Business Service is found, create/update the relationship record
this.createOrUpdateRelationship(appServiceGR.getUniqueValue(), busServiceGR.getUniqueValue());
}
}
},
createOrUpdateRelationship: function (appServiceSysId, busServiceSysId) {
// Check if the relationship already exists
var relGR = new GlideRecord('cmdb_rel_ci');
relGR.addQuery('parent', appServiceSysId);
relGR.addQuery('child', busServiceSysId);
relGR.addQuery('type', this.relationshipTypeSysId);
relGR.query();
if (!relGR.next()) {
// If it doesn't exist, create a new relationship record
relGR.initialize();
relGR.setValue('parent', appServiceSysId);
relGR.setValue('child', busServiceSysId);
relGR.setValue('type', this.relationshipTypeSysId);
relGR.insert();
}
},
type: 'AppToBusServiceRelationship'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 11:29 PM
Thank you so much everyone for your help and support. I was able to get it resolved through the following code and now I am getting the dependency as expected.
(function executeRule(current, previous /*null when async*/ ) {
gs.info("NB BR 1");
if (current.application_service.operational_status == 2 || current.business_application.operational_status == 2) {
gs.info("NB BR 2 NON OP");
return; //skip relationship creation if either ci is non-operational
}
gs.info("NB BR 3 : "+ current.u_application_release_cit_id);
var businessAppSysID;
var getBusinessApplication = new GlideRecord("cmdb_ci_business_app");
getBusinessApplication.addEncodedQuery("correlation_id=" + current.u_application_release_cit_id);
getBusinessApplication.query();
gs.info("NB BR 3.1 count "+getBusinessApplication.getRowCount());
if (getBusinessApplication.next()) {
businessAppSysID = getBusinessApplication.sys_id;
gs.info("NB BR 4 businessAppSysID "+businessAppSysID);
var gr = new GlideRecord('cmdb_rel_ci');
gr.initialize();
gr.parent = current.sys_id;
gr.child = businessAppSysID;
gr.type = '7959e7c41b684a90354d96859b4bcbff';
var ins = gr.insert();
gs.info("NB BR 5 ins : "+ins);
}
})(current, previous);