JSON Key Pairing - How to use catalog variables to populate records?

Lon Landry4
Mega Sage

JSON Key Pairing question

{“key”:”value”}

 

Summary:
A new asset is created using Workflow. I need to use key pairs to iterate thru catalog variables and assign the variables below to the corresponding field on the cmbd_ci_computer table. There may be hundreds of catalog variables, so I need to streamline this process as much as possible.

Keys:

Fields on the computer table being updated for the new asset:

  • asset_tag
  • serial_number
  • mac_address

Values:

  • Catalog variable fields with type of Single line text.
  • Fields ending with _asset_tag, _serial_number, & _mac_address.
  • Above fields begin with a number spelled out.

                Ex.  first_asset_tag, first_serial_number, first_mac_address

                second_asset_tag, second_serial_number, second_mac_address

 

So, I would expect the key pair to look something like:

computerFields.png

 

(But, I am not sure how to say “asset_tag of item just created”.)
 ForEach() Loop

Based on my limited (original) scripting experience, my best guess is that I can use the ForEach() Loop method to iterate through the list of key pairs.

 

Maybe:

 

computerFields2.png

How can I handle the forEach & recurring fields (which assign tag & serial number)?

 

Copy of code is in .txt file.

1 ACCEPTED SOLUTION

DrewW
Mega Sage
Mega Sage

So your computerFields var is not formatted correctly.  That is not valid object format.

You can do things like this.

var map = {
    "computerField1": "variable1",
    "computerField2": "variable2",
    "computerField3": "variable3",
}

for(var item in map){
    //Add code to get computer record here, or create one.
    computer.setValue(item, current.variables[map[item]]);

    //You can test this using a background script and using the below as output and remove the line above.
    gs.print(item + ":" + map[item]);
};

View solution in original post

1 REPLY 1

DrewW
Mega Sage
Mega Sage

So your computerFields var is not formatted correctly.  That is not valid object format.

You can do things like this.

var map = {
    "computerField1": "variable1",
    "computerField2": "variable2",
    "computerField3": "variable3",
}

for(var item in map){
    //Add code to get computer record here, or create one.
    computer.setValue(item, current.variables[map[item]]);

    //You can test this using a background script and using the below as output and remove the line above.
    gs.print(item + ":" + map[item]);
};