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

I don't know what happened to my previous post. But

I see the correct table name: cmdb_rel_ci. Your script looks good, you should close this thread now.