Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Script to copy the tags from cloud_key_value table to its custom fields in CMDB

MalathiP2130156
Tera Contributor

Hello Everyone,

 

The Infra team is managing tags for Cloud Hosted VM in Azure Portal and through SG-Azure, the tags are discovered either as a VM Instance or Cloud Resource. 


The requirement is tags are stored in cmdb_key_value table. How to copy those tags and to get that populated in the respective attributes in the Server class.

 

Also, we have requested the Infra team to add the tags for On-Perm Servers? Will this proposal work?

1 REPLY 1

Vinay Chavhan
Tera Contributor

Hi @MalathiP2130156,

 

You can create an business rule for copying tags to server.

I had the similar use case and I have created a business rule which runs on Key value (cmdb_key_value) table and copies tags from VM Instance record to related Server record on inser and update. Hope it helps to achieve the use case for you.

______________________________________________________________________________

 var vmSysId = current.configuration_item.sys_id;

    if (current.operation() == 'insert') {

        var rel = new GlideRecord("cmdb_rel_ci");
        rel.addEncodedQuery("type=d93304fb0a0a0b78006081a72ef08444^child.sys_id=" + vmSysId);
        rel.query();
        if (rel.next()) {
            var newKey = new GlideRecord("cmdb_key_value");
            newKey.initialize();
            newKey.key = current.key;
            newKey.value = current.value;
            newKey.configuration_item = rel.parent.sys_id;
            newKey.insert();
            //  gs.info('Record inserted');
        }
    } else if (current.operation() == 'update') {

        var relRec = new GlideRecord("cmdb_rel_ci");
        relRec.addEncodedQuery("type=d93304fb0a0a0b78006081a72ef08444^child.sys_id=" + vmSysId);
        relRec.query();
        if (relRec.next()) {
            var servRecId = relRec.parent.sys_id;
            var updateKey = new GlideRecord("cmdb_key_value");
            updateKey.addEncodedQuery("configuration_item.sys_id=" + servRecId + "^key=" + current.key);
            updateKey.query();
            if (updateKey.next()) {
                updateKey.key = current.key;
                updateKey.value = current.value;
                updateKey.update();
            }
            // gs.info('Record updated');
        }


    }
 
With above BR you can achieve copying tags to server, but to copy the values from Tag to custom server field you can again write a BR or scheduled job.

Hope it helps!

 
Regards,
Vinay