Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 01:33 PM
The code below needs to stop if one of the JSON Key pairs below has a null value. The actual var maps contains many JSON Key pairs. The system needs to know if a key pair is empty then to stop the program. This code is for Workflow and is being used in the platform as opposed to portal.
I have tried combinations where I check if maps or i is null (ex. maps !=0) , but I am not doing in correct place or have my syntax incorrect.
The code should result in hardware assets being created if JSON pair does not contain a null value.
Any idea how to stop a for loop when a JSON pair inside of a variable is null or all key pairs have been actioned upon.
var maps = [
{
'asset_tag': 'first_asset_tag',
'serial_number': 'first_serial_number',
},
{
'asset_tag': 'second_asset_tag',
'serial_number': 'second_serial_number',
},
{
'asset_tag': 'third_asset_tag',
'serial_number': 'third_serial_number',
},
];
gs.info(JSON.stringify(maps, null, '\t'));
var grHardware = new GlideRecord('alm_hardware');
for (var i = 0; i < maps.length; i++) {
var map = maps[i];
gs.debug('Setting up record ' + (i + 1));
grHardware.initialize();
for (var field in map) {
gs.debug('Setting field ' + field + ' to value of variable ' + map[field] + ' (' + current.variables[map[field]] + ')');
grComputer.setValue(field, current.variables[map[field]]);
}
grHardware.install_status = '6';
grHardware = 'available';
gs.debug('Creating record ' + (i + 1));
grHardware.insert();
}
Solved! Go to Solution.
1 ACCEPTED SOLUTION
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 02:08 PM
Hi @Lon Landry4,
Try something like below:
for (var i = 0; i < maps.length; i++) {
var map = maps[i];
var missingField = false;
var grHardware = new GlideRecord('alm_hardware');
grHardware.initialize();
for (var field in map) {
if (map[field] == '' || map[field] == null) {
missingField = true;
break;
}
grHardware.setValue(field, current.variables[map[field]]); //Not grComputer.setValue
}
if (missingField) {
break; //if any field is missing, move onto next iteration
}
grHardware.install_status = '6';
grHardware = 'available'; // not sure what this is doing
grHardware.insert();
}
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 02:08 PM
Hi @Lon Landry4,
Try something like below:
for (var i = 0; i < maps.length; i++) {
var map = maps[i];
var missingField = false;
var grHardware = new GlideRecord('alm_hardware');
grHardware.initialize();
for (var field in map) {
if (map[field] == '' || map[field] == null) {
missingField = true;
break;
}
grHardware.setValue(field, current.variables[map[field]]); //Not grComputer.setValue
}
if (missingField) {
break; //if any field is missing, move onto next iteration
}
grHardware.install_status = '6';
grHardware = 'available'; // not sure what this is doing
grHardware.insert();
}
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 02:34 PM
Thanks for the help!
And sorry about the part with Computer instead of Hardware, & line missing field on asset table.
They were typos not in original script.