Auto create relationship and auto removal of relationship in between two class in cmdb

Bijay Kumar Sha
Giga Guru

Hi,

I've 2 tables in cmdb. cmdb_ci_ec2_instance and cmdb_ci_aws_account.

In the form of one record available in 'cmdb_ci_ec2_instance' table, there is a reference field which is a record from 'cmdb_ci_aws_account'. Please find a sample screenshot as below -

BijayKumarSha_0-1722435876848.png

In the 'cmdb_rel_ci' table, I've created a relationship between above tables as 'Depends On::Used By'. where 'cmdb_ci_ec2_instance' is a parent table of 'cmdb_ci_aws_account'.

 

Now, my requirement is whenever, the Install Status of any record from 'cmdb_ci_ec2_instance' changes to 'Installed', then automatically the relationship should get created to the AWS Cloud account which is mentioned in the reference field of that form.

And, whenever the Install Status of the any record from 'cmdb_ci_ec2_instance' changes to 'Disposed', then automatically the relationship should get removed from that AWS Cloud account which is mentioned in the reference field of that form.

How to get this done?

Any help ?

1 ACCEPTED SOLUTION

Hi @Bert_c1 ,

Below is the updated BR - 

 

(function executeRule(current, previous /*null when async*/) {

   
    // Check install_status
    if (current.install_status == 1) {      // value for "Installed"
        var ciRelationship_add = new GlideRecord('cmdb_rel_ci');
        ciRelationship_add.initialize();
        ciRelationship_add.child = current.sys_id;
        ciRelationship_add.parent = current.u_aws_account_id;   //reference field for cmdb_ci_aws_account
        ciRelationship_add.type = 'bf83653c0ab30150761028c73a4de0f4';  //Relationship Type = Managed by::Manages
        ciRelationship_add.insert();
    }
    if (current.install_status == 44) {     // value for "Disposed"
        var ciRelationship_del = new GlideRecord('cmdb_rel_ci');
        ciRelationship_del.addQuery('child', current.sys_id.toString());
        ciRelationship_del.addQuery('parent', current.u_aws_account_id.toString()); //reference field for cmdb_ci_aws_account
        ciRelationship_del.query();
        while (ciRelationship_del.next()) {
            ciRelationship_del.deleteRecord();
        }
    }

})(current, previous);

View solution in original post

5 REPLIES 5

Bert_c1
Kilo Patron

Hi,

 

You may achieve your goal using a business rule defined on the 'cmdb_ci_ec2_instance' table as shown below.

 

Screenshot 2024-07-31 123236.png

The script logic is:

 

 

(function executeRule(current, previous /*null when async*/) {

	// Check install_status
	if (current.install_status == 1) {		// value for "Installed"
		var ciRelationship = new GlideRecord('cmdb_rel_ci');
		ciRelationship.initialize();
		ciRelationship.parent = current.sys_id;
		ciRelationship.child = current.u_aws_account;	//reference field for cmdb_ci_aws_account
		// set any other fields as desired
		ciRelationship.insert();
	}
	if (current.install_status == 2) {		// value for "Disposed"
		var ciRelationship = new GlideRecord('cmdb_rel_ci');
		ciRelationship.addQuery('parent', current.sys_id.toString());
		ciRelationship.addQuery('child', current.u_aws_account.toString());	//reference field for cmdb_ci_aws_account
		ciRelationship.query();
		while (ciRelationship.next()) {
			ciRelationship.deleteRecord();
		}
	}

})(current, previous);

 

 

I do not have a reference field on the 'cmdb_ci_ec2_instance' table to a record in the 'cmdb_ci_aws_account' table, seems you do have that field. I assummed it may have name = 'u_aws_account' in the above.  I also had to defined two choice values for install_status: 1 - Installed and 2 - Disposed.

 

If the above doesn't work for you, please post details on those aspects.

Hi @Bert_c1 ,

It's almost correct except one thing. The relationship 'Type' is missing. I see relationship is getting created but type is missing. How I can integrate the Relationship Type between these 2 table?

Can you please let me know

Hi @Bijay Kumar Sha,

 

I reviewed again, and see today that I reference a non-existing table: "cmdb_ci_rel".  Please be specific on what fields are in play here. Where/how any relationship is stored.

 

1. Reference field on 'cmdb_ci_ec2_instance' to the 'cmdb_ci_aws_account' table. My guess is that field is named 'u_aws_account'.

2. Is there a reference field on the 'cmdb_ci_aws_account' to a record in the 'cmdb_ci_ec2_instance'?

3. What table does 'Type' exist on?

Hi @Bert_c1 ,

Below is the updated BR - 

 

(function executeRule(current, previous /*null when async*/) {

   
    // Check install_status
    if (current.install_status == 1) {      // value for "Installed"
        var ciRelationship_add = new GlideRecord('cmdb_rel_ci');
        ciRelationship_add.initialize();
        ciRelationship_add.child = current.sys_id;
        ciRelationship_add.parent = current.u_aws_account_id;   //reference field for cmdb_ci_aws_account
        ciRelationship_add.type = 'bf83653c0ab30150761028c73a4de0f4';  //Relationship Type = Managed by::Manages
        ciRelationship_add.insert();
    }
    if (current.install_status == 44) {     // value for "Disposed"
        var ciRelationship_del = new GlideRecord('cmdb_rel_ci');
        ciRelationship_del.addQuery('child', current.sys_id.toString());
        ciRelationship_del.addQuery('parent', current.u_aws_account_id.toString()); //reference field for cmdb_ci_aws_account
        ciRelationship_del.query();
        while (ciRelationship_del.next()) {
            ciRelationship_del.deleteRecord();
        }
    }

})(current, previous);