Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

aUnable to Run the onAfter Transform Script

Nilanjan1
Mega Sage

Dear Experts. 

 

I need a quick help struck with a code. I am trying to run a onAfter transform scrip to create relationship and also once created the next run of the transform map should not create new one (if a relationship has already been inserted) - I am able to create the relationship which is working as expected but somehow the conditions that will prevent relationship is not getting created. 

 

What I am thinking is if Application Name.sys_id OR Business_App.sys_Id or TYPE is present, it should not create a new one. Can someone help ? 

(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.initialize();
            cmdbRelGR.parent = target.sys_id;
            cmdbRelGR.child = businessAppSysID;
            cmdbRelGR.type = "Consumes::Consumed by";
            cmdbRelGR.insert();
			}
			else {
				ignore = true;
			}
        })(source, map, log, target);
1 ACCEPTED SOLUTION

maroon_byte
Mega Sage

Try below:

(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 = target.sys_id;
			cmdbRelGR.child = businessAppSysID;
			cmdbRelGR.type = '41008aa6ef32010098d5925495c0fb94';
			cmdbRelGR.insert();
		}
	}
})(source, map, log, target);

View solution in original post

4 REPLIES 4

maroon_byte
Mega Sage

Try below:

(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 = target.sys_id;
			cmdbRelGR.child = businessAppSysID;
			cmdbRelGR.type = '41008aa6ef32010098d5925495c0fb94';
			cmdbRelGR.insert();
		}
	}
})(source, map, log, target);

Thank you Maroon Byte.. I was able to edit the code. However I wanted to make the business application as the Parent and application service as a child. I am unable to switch that ! Any advise

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

I  

type needs to be the sys_id of "Consumes::Consumed by", otherwise, insert will work but type will be empty.

For my instance, its below but probably same sys_id of type in your instance as well.
cmdbRelGR.type = "41008aa6ef32010098d5925495c0fb94";
 
Regards,
Sharad

are you able to advise on how we can make the reverse the parent and the child.

Business App as Parent

App Service as Child

 

Whenever I am reversing the it, it is working but I am also taking the App Service as Parent and Business App as child.