Help with Key Pairs and JSON - creating records only when JSON key values are populated

Lon Landry4
Mega Sage
I need to create hardware records when JSON key pairs do not have a value of null.
Sometimes, all five of the key pairs may be populated or just one of the key pairs is populated.
 
I had something kind of working earlier, but after populated records with key pairs, other records were created with empty asset_tag, serial_number, & mac_address (for the key pairs that are empty)
 
Any ideas?
 
//Text file attached is easier to read...
//This is just a snippet of my code the var maps contains many, many key pairs.
var maps = [
{
'asset_tag': 'first_asset_tag',
'serial_number': 'first_serial_number',
'mac_address': 'first_mac_address',
},
{
'asset_tag': 'second_asset_tag',
'serial_number': 'second_serial_number',
'mac_address': 'second_mac_address',
},
{
'asset_tag': 'third_asset_tag',
'serial_number': 'third_serial_number',
'mac_address': 'third_mac_address',
},
{
'asset_tag': 'fourth_asset_tag',
'serial_number': 'fourth_serial_number',
'mac_address': 'fourth_mac_address',
},
{
'asset_tag': 'fifth_asset_tag',
'serial_number': 'fifth_serial_number',
'mac_address': 'fifth_mac_address',
},
];
 
var grComputer = new GlideRecord('cmdb_ci_computer');
for (var i = 0; i < maps.length; i++) {
var map = maps[i];
var emptyField = false;
 
// Begin creating a new record
grComputer.initialize();
 
for (var field in map){
if (map[field] == '' || map[field] == null){
emptyField = true;
break;
}
//set values on new computer record from JSON pairs in var map
grComputer.setValue(field, current.variables[map[field]]);
}
//Set some other values on new Computer record
grComputer.install_status = '6';
grComputer.hardware_substatus = 'available';
//insert new record into cmdb_ci_computer table
gr.Computer.insert();
}
 
 
1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @Lon Landry4 ,
I trust you are doing great.
Here's a revised version of your code snippet with the necessary adjustments:

var maps = [
  {
    'asset_tag': 'first_asset_tag',
    'serial_number': 'first_serial_number',
    'mac_address': 'first_mac_address',
  },
  {
    'asset_tag': 'second_asset_tag',
    'serial_number': 'second_serial_number',
    'mac_address': 'second_mac_address',
  },
  {
    'asset_tag': 'third_asset_tag',
    'serial_number': 'third_serial_number',
    'mac_address': 'third_mac_address',
  },
  {
    'asset_tag': 'fourth_asset_tag',
    'serial_number': 'fourth_serial_number',
    'mac_address': 'fourth_mac_address',
  },
  {
    'asset_tag': 'fifth_asset_tag',
    'serial_number': 'fifth_serial_number',
    'mac_address': 'fifth_mac_address',
  },
];

var grComputer = new GlideRecord('cmdb_ci_computer');
for (var i = 0; i < maps.length; i++) {
  var map = maps[i];
  var allFieldsEmpty = true; // Flag to track if all fields are empty
  
  // Check if any field in the map has a non-null value
  for (var field in map) {
    if (map[field] && current.variables[map[field]]) {
      allFieldsEmpty = false; // At least one non-empty field found
      break;
    }
  }
  
  // If at least one non-empty field found, create the record
  if (!allFieldsEmpty) {
    grComputer.initialize();
    
    for (var field in map) {
      // Only set values for non-empty fields
      if (map[field] && current.variables[map[field]]) {
        grComputer.setValue(field, current.variables[map[field]]);
      }
    }
    
    grComputer.install_status = '6';
    grComputer.hardware_substatus = 'available';
    
    // Insert the record into cmdb_ci_computer table
    grComputer.insert();
  }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

2 REPLIES 2

Amit Gujarathi
Giga Sage
Giga Sage

HI @Lon Landry4 ,
I trust you are doing great.
Here's a revised version of your code snippet with the necessary adjustments:

var maps = [
  {
    'asset_tag': 'first_asset_tag',
    'serial_number': 'first_serial_number',
    'mac_address': 'first_mac_address',
  },
  {
    'asset_tag': 'second_asset_tag',
    'serial_number': 'second_serial_number',
    'mac_address': 'second_mac_address',
  },
  {
    'asset_tag': 'third_asset_tag',
    'serial_number': 'third_serial_number',
    'mac_address': 'third_mac_address',
  },
  {
    'asset_tag': 'fourth_asset_tag',
    'serial_number': 'fourth_serial_number',
    'mac_address': 'fourth_mac_address',
  },
  {
    'asset_tag': 'fifth_asset_tag',
    'serial_number': 'fifth_serial_number',
    'mac_address': 'fifth_mac_address',
  },
];

var grComputer = new GlideRecord('cmdb_ci_computer');
for (var i = 0; i < maps.length; i++) {
  var map = maps[i];
  var allFieldsEmpty = true; // Flag to track if all fields are empty
  
  // Check if any field in the map has a non-null value
  for (var field in map) {
    if (map[field] && current.variables[map[field]]) {
      allFieldsEmpty = false; // At least one non-empty field found
      break;
    }
  }
  
  // If at least one non-empty field found, create the record
  if (!allFieldsEmpty) {
    grComputer.initialize();
    
    for (var field in map) {
      // Only set values for non-empty fields
      if (map[field] && current.variables[map[field]]) {
        grComputer.setValue(field, current.variables[map[field]]);
      }
    }
    
    grComputer.install_status = '6';
    grComputer.hardware_substatus = 'available';
    
    // Insert the record into cmdb_ci_computer table
    grComputer.insert();
  }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Lon Landry4
Mega Sage

Thank you so much for your help. I have been struggling with JavaScript as I get into more advanced concepts.

I hope to be able to take the official ServiceNow coding course this year.

Any recommendations for ServiceNow JavaScript training beyond Chuck Tomasi's or Earl Duque's JavaScript training?


(I do not create custom apps, all of my work is done in global application with an emphasis on ITSM.

Thanks again,

Lon