Create a business rule to establish a relationship between Application Service and Business Service

Nilanjan1
Mega Sage

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);

 

 ( 

7 REPLIES 7

Allen Andreas
Administrator
Administrator

Hello,

Do you not have access to a free personal developer instance (PDI)? You could test your script there. From just looking at your script, you're querying the cmdb_rel_ci table to look for a record that has the parent and child and type as you've specified in the top portion...if a match is found...your script is setting the same values to the same fields...again...otherwise, if it's not found, you're creating a new record on that table.

 

Is that what you're wanting to do? If not, please adjust.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thank You Allen. I am working on getting the Application Service to Business Application through two fields one the application release id in the application service table and other is the correlation id in the business application table and if a match is found then I update it with the type that is created.

Amit Gujarathi
Giga Sage
Giga Sage

HI @Nilanjan1 ,
I trust you are doing great.
To optimize your script and ensure it meets the requirements, we can make a few adjustments. Here's the revised script with optimizations and the additional condition for Operational status:

(function executeRule(current, previous /*null when async*/) {
    // Check if both application service and business application are operational
    if (current.application_service.operational_status == 'non_operational' || current.business_application.operational_status == 'non_operational') {
        gs.info('One of the CI is non-operational. Relationship creation skipped.');
        return; // Skip relationship creation if either CI is non-operational
    }

    var gr = new GlideRecord('cmdb_rel_ci');
    gr.addQuery('parent', current.application_service.sys_id);
    gr.addQuery('child', current.business_application.sys_id);
    gr.addQuery('type', '7959e7c41b684a90354d96859b4bcbff');
    gr.query();
    
    if (gr.next()) {
        gs.info('Relationship already exists. Updating...');
        gr.setValue('type', '7959e7c41b684a90354d96859b4bcbff'); // Ensure type is set
        gr.update();
    } else {
        gs.info('Creating new relationship...');
        gr.initialize();
        gr.parent = current.application_service.sys_id;
        gr.child = current.business_application.sys_id;
        gr.type = '7959e7c41b684a90354d96859b4bcbff';
        gr.insert();
    }
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hello Amit, 

 

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

Nilanjan1_1-1708447531825.png

 

When to Run Tab is updated with the following

Nilanjan1_0-1708447494423.png

 

 

(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
    }
    var gr = new GlideRecord('cmdb_rel_ci');
    gr.addQuery('parent', current.application_service.u_application_release_cit_id);
    gr.addQuery('child', current.business_application.correlation_id);
    gr.addQuery('type', '7959e7c41b684a90354d96859b4bcbff');
    gr.query();
    if (gr.next()) { // Update the record with the correct relationship if not present
        gs.info("NB BR 3 IF");
        gr.setValue('type', '7959e7c41b684a90354d96859b4bcbff');
        gr.update();
    } else { // Insert a record if not already present
        gs.info("NB BR 4 ELSE");
        gr.initialize();
        gr.parent = current.application_service.u_application_release_cit_id;
        gr.child = current.business_application.correlation_id;
        gr.type = '7959e7c41b684a90354d96859b4bcbff';
        var sysID = gr.insert();
        gs.info("NB BR 4.1 sysID " + sysID);
    }

})(current, previous);