How to Iterate thru 2 groups of fields in workflow?

Lon Landry4
Mega Sage

I may need two scripts, one for asset_tags & one for serial numbers.

This is for creating assets in Workflow.

 

//Don't try this code a 20 or so, junk records are created.

var grComputer = new GlideRecord('alm_hardware');
var fields = ["first_asset_tag", "first_serial_number", "second_asset_tag", "second_serial_number", "third_asset_tag", "third_serial_number""] ";

var last = fields.pop();
fields.forEach(function(field, i) {
var nextField = fields[i+1] || last;
grComputer.initialize();
grComputer.asset_tag = current.variables.fields;
grComputer.serial_number = current.variables.fields;
grComputer.insert();
});

1 ACCEPTED SOLUTION

Instead of using array, it would be better to use JSON object to store value as key-pair, while fetching it from the variables, then iterate through the object to add/update fields in the hardware table.

Its much more reliable way of handling the data, instead of using array to store two distinct information.

 

Best Regards
Aman Kumar

View solution in original post

3 REPLIES 3

Aman Kumar S
Kilo Patron

The question doesn't provide much context, share more details about the problem you are facing, the use of array doesn't seem right.

How are you accessing these values, or these are static?

Best Regards
Aman Kumar

Lon Landry4
Mega Sage

Good point,
I am trying to find a way to loop thru variable fields, the variables create a new record & update fields on the hardware table (not all fields are shown). There may be one asset being created or hundreds of assets created.

All of the variables start with a number & contain _asset_tag or _serial_number.
The variables are in sequence on the form.

 

So, I am wondering if I can find _asset_tag & _serial_number sequentially as opposed to have to write code for each field.

Instead of using array, it would be better to use JSON object to store value as key-pair, while fetching it from the variables, then iterate through the object to add/update fields in the hardware table.

Its much more reliable way of handling the data, instead of using array to store two distinct information.

 

Best Regards
Aman Kumar