Unable to update the ownership information in the Virtual machine table

Nilanjan1
Mega Sage

Dear Experts, 

 

I am in a process of getting the technical and business contact from the Azure tags updated to the owned by & Assigned to fields in the cmdb_ci_vm_instance form and consequently to the cmdb_ci_server form.. I am able to get the values but it is giving me an error . Can someone suggest on the below script ? 

 

Nilanjan1_0-1746707895078.png

 

(function() {
    var ciMap = {};

    var tagGR = new GlideRecord('cmdb_key_value');
    tagGR.addEncodedQuery('key=businessContact^ORkey=technicalContact^value!=<redacted>^ORvalue=NULL^configuration_item.sys_class_name=cmdb_ci_vm_instance');
    tagGR.setLimit(10);
    tagGR.query();

    while (tagGR.next()) {
        var ciID = tagGR.configuration_item.sys_id.toString();
        var key = tagGR.getValue('key');
        var value = tagGR.getValue('value');

        if (!ciMap[ciID]) {
            ciMap[ciID] = {
                businessContact: null,
                technicalContact: null
            };
        }

        if (key === 'businessContact') {
            ciMap[ciID].businessContact = value;
        } else if (key === 'technicalContact') {
            ciMap[ciID].technicalContact = value;
        }

        gs.info('Found key: ' + key + ' for CI: ' + tagGR.configuration_item.name);
    }

    for (var ci in ciMap) {
        var data = ciMap[ci];
        var serverGR = new GlideRecord('cmdb_ci_vm_instance');
         if (serverGR.get(ci)) {
            var valid = true;
            if (data.businessContact) {
                var owner = new GlideRecord('sys_user');
                if (owner.get(data.businessContact)) {
                    serverGR.setValue('owned_by', owner.getUniqueValue());
                } else {
                    gs.info('Invalid businessContact sys_id: ' + data.businessContact);
                    valid = false;
                }
            }

            if (data.technicalContact) {
                var tech = new GlideRecord('sys_user');
                if (tech.get(data.technicalContact)) {
                    serverGR.setValue('assigned_to', tech.getUniqueValue());
                } else {
                    gs.info('Invalid technicalContact sys_id: ' + data.technicalContact);
                    valid = false;
                }
            }

            if (valid) {
                serverGR.update();
                gs.info('Updated CI: ' + serverGR.name);
            }
        }
    }
})();

 

1 ACCEPTED SOLUTION

It is my lower environment so I have the admin access.

Also, I am able to retrieve the email addresses...it is running the else loop when retrieving the e mail address

else {
                    gs.info('Invalid businessContact sys_id: ' + data.businessContact);
                    valid = false;

View solution in original post

4 REPLIES 4

dougmoto
Tera Contributor

Did you verify manually that the sys_id's retrieved from businessContact and/or technicalContact exists in the 'sys_user' table?  if so, maybe an access issue where the script is running (i.e. are you impersonating a user or an admin)?

It is my lower environment so I have the admin access.

Also, I am able to retrieve the email addresses...it is running the else loop when retrieving the e mail address

else {
                    gs.info('Invalid businessContact sys_id: ' + data.businessContact);
                    valid = false;

Ok, so I read the script wrong, I thought you were using a sys_id as the businessContact to retrieve the user in the sys_user table, if the businessContact is an email address, the problem seems to be here:

 

var owner = new GlideRecord('sys_user');
if (owner.get(data.businessContact)) {

if this is the case, you'll need to change the get method to include the column name you wish to search for the email address:

var owner = new GlideRecord('sys_user');
if (owner.get('<name of column>',data.businessContact)) {

 otherwise it defaults to using a sys_id to retrieve the record.

Thank you sp much @dougmoto I was able to resolve this. 😊