Unable to Reverse the Parent Child Relationship

Nilanjan1
Mega Sage

Dear Experts, 

 

I have a transform map targetting the Application service & a OnAfter script which will map the Application and Business application togather. The script is working fine in creating the relationship, however instead of app service to become a parent, I need Business App to be the parent and child will be app service. Here is the code. Can someone suggest ?

[19:02] Bose, Nilanjan
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
    var appRel = target.u_application_release_cit_id;
    var businessAppSysID;
    var getBusinessApplication = new GlideRecord("cmdb_ci_business_app");
    getBusinessApplication.addEncodedQuery("correlation_id=" + appRel);
    getBusinessApplication.query();
    if (getBusinessApplication.next()) {
        businessAppSysID = getBusinessApplication.sys_id;
        var cmdbRelGR = new GlideRecord("cmdb_rel_ci");
        //cmdbRelGR.addEncodedQuery('parent=' + target.sys_id + '^child=' + businessAppSysID);
        cmdbRelGR.addEncodedQuery('parent=' + businessAppSysID + '^child=' + target.sys_id);
        cmdbRelGR.query();
        if (!cmdbRelGR.next()) {
            cmdbRelGR.initialize();
            // cmdbRelGR.parent = target.sys_id;
            // cmdbRelGR.child = businessAppSysID;
            cmdbRelGR.parent = businessAppSysID;
            cmdbRelGR.child = target.sys_id;
            cmdbRelGR.type = "Consumes::Consumed by";
            cmdbRelGR.insert();
        }
    } else {
        ignore = true;
    }
})(source, map, log, target);

 

 

7 REPLIES 7

Deepak Shaerma
Kilo Sage

HI @Nilanjan1 

Your script appears generally correct for making the Business Application the parent of the Application Service. The key parts for doing so are:

1. Setting cmdbRelGR.parent to businessAppSysID - This designates the Business Application as the parent.
2. Setting cmdbRelGR.child to target.sys_id - This designates the Application Service (the record being transformed) as the child.

These are correct as per your requirement that the Business Application should be the parent and the Application Service should be the child.

If for some reason the relationships are not being set up as you expect, here are a few things to check:

### 1. Correct Relationship Type:
Ensure that the type field is set to the correct value that represents the relationship you want to establish.

"Consumes::Consumed by" might not be the correct relationship depending on your specific ServiceNow configuration. Make sure this type reflects the intended parent-child relationship in your CMDB configuration.

### 2. Query Correctness:
Verify that the addEncodedQuery("correlation_id=" + appRel); actually retrieves the correct Business Application. The correlation_id must be correctly mapped and the Business Application should have this value populated to be retrieved by this query.

### 3. Ensure No Duplicate or Conflicting Relationships:
Ensure there are no existing conflicting relationships that might be causing issues. Though your script checks if the specific relationship exists before creating a new one, there might be other relationships affecting the CMDB hierarchy.

### 4. Execution Rights:
Verify the user executing this transformation has appropriate rights to create relationships in the cmdb_rel_ci table.

### 5. Debugging:
Add debugging logs to help identify where the script might be failing or behaving unexpectedly. For example:


gs.info("Business App Sys ID: " + businessAppSysID + ", Target Sys ID: " + target.sys_id);

 Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma

@Deepak Shaerma 

 

I have checked all the steps, but seems that the relationship is creating, but I could see that there are both kind of relationships getting created...business App - Parent ; Application Service - Child but the reverse is also getting created. I am not sure what is wrong ? 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
	var appRel = target.u_application_release_cit_id;
	var businessAppSysID;
	var getBusinessApplication = new GlideRecord("cmdb_ci_business_app");
	getBusinessApplication.addEncodedQuery("correlation_id=" + appRel);
	getBusinessApplication.query();
	if (getBusinessApplication.next()) {
		businessAppSysID = getBusinessApplication.sys_id;
		var cmdbRelGR = new GlideRecord("cmdb_rel_ci");
		cmdbRelGR.addQuery('child', target.sys_id);
		cmdbRelGR.addQuery('parent', businessAppSysID);
		cmdbRelGR.addQuery('type', '41008aa6ef32010098d5925495c0fb94'); //Sys ID of "Consumes::Consumed by"
		cmdbRelGR.query();
		if (cmdbRelGR.next())
			//do nothing
		else {
			cmdbRelGR.initialize();
			cmdbRelGR.parent = (function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
	var appRel = target.u_application_release_cit_id;
	var businessAppSysID;
	var getBusinessApplication = new GlideRecord("cmdb_ci_business_app");
	getBusinessApplication.addEncodedQuery("correlation_id=" + appRel);
	getBusinessApplication.query();
	if (getBusinessApplication.next()) {
		businessAppSysID = getBusinessApplication.sys_id;
		var cmdbRelGR = new GlideRecord("cmdb_rel_ci");
		cmdbRelGR.addQuery('parent', target.sys_id);
		cmdbRelGR.addQuery('child', businessAppSysID);
		cmdbRelGR.addQuery('type', '41008aa6ef32010098d5925495c0fb94'); //Sys ID of "Consumes::Consumed by"
		cmdbRelGR.query();
		if (cmdbRelGR.next())
			//do nothing
		else {
			cmdbRelGR.initialize();
			cmdbRelGR.parent = businessAppSysID;
			cmdbRelGR.child = target.sys_id;
			cmdbRelGR.type = '41008aa6ef32010098d5925495c0fb94';
			cmdbRelGR.insert();
		}
	}

	}
})(source, map, log, target);

 

Deepak Shaerma
Kilo Sage

Hi @Nilanjan1 

It looks like the issue in your code is because you have essentially the same block of code written twice, causing it to potentially create both types of relationships (Parent-Child and Child-Parent) due to how the queries and conditions are set up, depending on the data it encounters. The script essentially duplicates functionality, once with the cmdb_rel_ci record being created with the business application as the parent and the application service as the child, and then again (in what appears to be a mistakenly duplicated and nested function) with the roles reversed.

The first part before the mistakenly repeated and nested function should establish the relationship with the business application as the parent and the application service as the child, which seems to be your intended functionality based on your description.

 

(function runTransformScript(source, map, log, target / undefined onStart / ) {
    var appRel = target.u_application_release_cit_id;
    var businessAppSysID;

    var getBusinessApplication = new GlideRecord("cmdb_ci_business_app");
    getBusinessApplication.addEncodedQuery("correlation_id=" + appRel);
    getBusinessApplication.query();

    if (getBusinessApplication.next()) {
        businessAppSysID = getBusinessApplication.sys_id;

        var cmdbRelGR = new GlideRecord("cmdb_rel_ci");
        cmdbRelGR.addQuery('child', target.sys_id);
        cmdbRelGR.addQuery('parent', businessAppSysID);
        cmdbRelGR.addQuery('type', '41008aa6ef32010098d5925495c0fb94'); //Sys ID of “Consumes::Consumed by”
        cmdbRelGR.query();

        if (!cmdbRelGR.next()) {
            cmdbRelGR.initialize();
            cmdbRelGR.parent = businessAppSysID;
            cmdbRelGR.child = target.sys_id;
            cmdbRelGR.type = '41008aa6ef32010098d5925495c0fb94';
            cmdbRelGR.insert();
        }
    }

})(source, map, log, target);

 



Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma

Hello Deepak, 

 

Cannot thank you enough for the detailed review of the code. To answer your question, it was copy paste error in the thread. I have actually deleted the 4000+ records (with a reverse relationship App Service to Business App) and then again I ran the transform map the number of record that is created but it is reduced. The record are still getting created, using the same code as you have.