Can anyone help me up with JSON attachment query please! Urgent.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2023 07:51 AM
Hi Community,
Can anyone please help me up with the multiple attachment of JSON file in the RITM.
Requirement:
So from one catalog, I am triggering 2 other catalog. The parent catalog RITM will have JSON attachment. Now the thing is this JSON has host_info which contains server name. This server name should populate on the other 2 catalog item. Which is happening if the JSON attachment is just one. But when we have multiple RITM scenario, my script is not able to read the other attachment server name info. I have captured the script in Run script:
wait condition:
// Set the variable 'answer' to true or false to indicate if the condition has been met or not.\
answer = ifScript();
function ifScript() {
var gr = new GlideRecord("sys_attachment"); //checking the file format
gr.addQuery("table_sys_id", current.sys_id);
gr.query();
while (gr.next()) {
if (gr.getValue('file_name').indexOf('json') != -1) {
return true;
} else {
return false;
}
}
}
Run script:
var attData = [];
var attachment = new GlideRecord('sys_attachment'); //checking multple attachments of only json file
attachment.addQuery('table_sys_id', current.sys_id);
attachment.query();
while (attachment.next()) {
gs.log('attData '+ attachment.getValue('file_name'), 'shivani');
if (attachment.getValue('file_name').indexOf('json') != -1) {
var json = JSON.parse(attachment.getValue('sys_attachment'));
var gsa = new GlideSysAttachment();
var bytesInFile = gsa.getBytes(attachment);
var dataAsString = Packages.java.lang.String(bytesInFile);
dataAsString = String(dataAsString);
attData.push(JSON.parse(dataAsString));
}
}
//1st catalog from Parent.
gs.log('attData '+ JSON.stringify(attData), 'shivani');
var backRitm = '';
var cyberRitm = '';
var currentRITM = new GlideRecord('sc_req_item');
currentRITM.addQuery('sys_id', current.sys_id);
currentRITM.query();
while (currentRITM.next()) {
var serv = JSON.parse(currentRITM.variables.selected_package_type);
backRitm = currentRITM.variables.ritm_s_backup.toString();
cyberRitm = currentRITM.variables.ritm_s_cyberark.toString();
}
backRitm = backRitm.split(',');
cyberRitm = cyberRitm.split(',');
for (var x in backRitm) {
if (serv[x].server_type == '2dd89d51dbc63050c7c322fa139619da' || serv[x].server_type == 'a1d89d51dbc63050c7c322fa139619db') {
var currentRITM1 = new GlideRecord('sc_req_item');
currentRITM1.addQuery('number', backRitm[x]);
currentRITM1.query();
while (currentRITM1.next()) {
var serverCI = new GlideRecord('cmdb_ci_server');
serverCI.addQuery('name', attData[x]['host_info'].name);
serverCI.query();
if (serverCI.next()) {
currentRITM1.variables.server_name = serverCI.sys_id;
if (attData[x]['tattoo'].kpmg_env == 'prod') {
currentRITM1.variables.environment = 'production';
} else if (attData[x]['tattoo'].kpmg_env == 'nonprod') {
currentRITM1.variables.environment = 'non_production';
}
currentRITM1.update();
}
}
}
}
//Cyberark multi row values (2nd catalog)
for (var y in cyberRitm) {
if (serv[y].server_os_version == '8b7ba36adb353054d5574531ba961989' || serv[y].server_os_version == 'c77ba36adb353054d5574531ba961989' || serv[y].server_os_version == '037ba36adb353054d5574531ba96198a' || serv[y].server_os_version == '4f7ba36adb353054d5574531ba961989') {
gs.print(serv[y].server_os_version);
var currentRITM2 = new GlideRecord('sc_req_item');
currentRITM2.addQuery('number' , cyberRitm[y]);
currentRITM2.query();
while (currentRITM2.next()) {
// gs.print(attData[y]['host_info'].name);
currentRITM2.variables.add_windows_linux_account[0].server_name_windows_linux = attData[y]['host_info'].name;
currentRITM2.variables.add_windows_linux_account[0].account_name_windows_linux = attData[y]['host_info'].name + '_adm';
var currentRITM3 = new GlideRecord('u_server_network_table');
currentRITM3.addQuery('sys_id', serv[y].server_network);
currentRITM3.query();
if (currentRITM3.next()) {
currentRITM2.variables.add_windows_linux_account[0].environment_windows_linux = currentRITM3.u_server_network;
}
currentRITM2.update();
}
}
}