- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2025 05:39 AM
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 ?
(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);
}
}
}
})();
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2025 10:47 PM
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;
.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2025 10:31 AM
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)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2025 10:47 PM
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;
.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2025 08:59 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 10:49 PM
Thank you sp much @dougmoto I was able to resolve this. 😊