Need to create multiple hardware asset records via single Record Producer submission

Liz23
Tera Expert
Use case:
When a bulk hardware order (in this case, computers) is received, the agent will access the RP via service portal. They will select a stockroom (ref. alm_stockroom), a machine type model (aka Product Model, ref. cmdb_model), and a comma separated list of serial numbers (multi line text). First, I want to verify whether any of the serial numbers already exist and, if so, report them in an additional field with a message to remove them from the original list. Then, upon submission I want to set the values for stockroom and model in addition to static values for model category and install status
 
Assumption: We are not licensed for HAM Pro
 
Problem:
Regardless of how many serial numbers are entered, a hardware record is created with only that value; none of the other values are set. Serial number entry "test001" creates a record in alm_hardware with the serial_number value of "test001"; other fields are not set correctly. Serial number entry "test001,test002,test003" creates a single record with serial_number "test001,test002,test003"; other fields are not set correctly.
 
Current build:
RP table name: Hardware [alm_hardware]
RP script: 
var modelCategory = '' + '81feb9c137101000deeabfc8bcbe5dc4'; // Computer
var model = '' + producer.mtm;
var installStatus = '' + '6'; // In stock
var stockroom = '' + producer.stockroom;
var serialString = '' + producer.serial_number;
var serialArray = serialString.split(',');
 
for (var i = 0; i < serialArray.length; i++) {
var hw = new GlideRecord('alm_hardware');
hw.initialize();
hw.model_category = '' + modelCategory;
hw.model = '' + model;
hw.serial_number = '' + serialArray[i];
hw.install_status = '' + '6';
hw.stockroom = '' + stockroom;
hw.insert();
}
 
I also tried using an onSubmit cat. client script that calls a script include, which did not work. In this case I wasn't sure what, if anything, to put in the RP script. When it's empty, the same problem occurs. When I include current.setAbortAction(true); no record is created.
 
Cat. Client Script:
function onSubmit(){
var asset = new GlideAjax('CreateHardwareAssets');
asset.addParam('sysparm_name','CreateHardware');
//asset.addParam('sysparm_model_category', g_form.getValue('fieldNameHere')); //for future use
asset.addParam('sysparm_model', g_form.getValue('mtm'));
asset.addParam('sysparm_stockroom', g_form.getValue('stockroom'));
asset.addParam('sysparm_serial_number', g_form.getValue('serial_number'));
}
 
Script Include:
var CreateHardwareAssets = Class.create();
CreateHardwareAssets.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
CheckDuplicates: function() {
var snDuplicates = [];
var serialNumber = this.getParameter('sysparm_serial_number');
var snArray = serialNumber.split(',');
 
for (var i = 0; i < snArray.length; i++) {
var assetGR = new GlideRecord('alm_hardware');
assetGR.addQuery('serial_number', snArray[i]);
assetGR.query();
 
if (assetGR.next()) {
snDuplicates.push(snArray[i]);
}
}
 
if (snDuplicates) {
snDupFound = snDuplicates.toString();
snDupFound = snDupFound.replaceAll(',', ', ');
return snDupFound;
}
},
 
CreateHardware: function() {
var model = this.getParameter('sysparm_model');
var stockroom = this.getParameter('sysparm_stockroom');
var serialNumber = this.getParameter('sysparm_serial_number');
var snArray = serialNumber.split(",");
//tried the below with and without the <'' + > as seen in the RP script, neither works
for (var i = 0; i < snArray.length; i++) {
var assetGR = new GlideRecord('alm_hardware');
assetGR.initialize();
assetGR.model_category = '' + '81feb9c137101000deeabfc8bcbe5dc4'; //Computer
assetGR.model = '' + model;
assetGR.serial_number = '' + snArray[i];
assetGR.install_status = '' + '6'; // In Stock
assetGR.stockroom = '' + stockroom;
assetGR.insert();
}
return;
},
 
type: 'CreateHardwareAssets'
});
1 ACCEPTED SOLUTION

Liz23
Tera Expert

For posterity, here is the RP that works, some of the variables needed to be declared as strings:

 

var modelCategory = '81feb9c137101000deeabfc8bcbe5dc4'; // Computer
var model = String(producer.mtm);
var installStatus = '6'; // In Stock
var stockroom = String(producer.stockroom);
var serialNums = String(producer.serial_number);
var serialArray = serialNums.split(",");

for (var i = 0; i < serialArray.length; i++) {
var hw = new GlideRecord('alm_hardware');
hw.initialize();
hw.setValue("model_category", modelCategory);
hw.setValue("model", model);
hw.setValue("serial_number", serialArray[i]);
hw.setValue("install_status", "6");
hw.setValue("stockroom", stockroom);
hw.insert();
}

current.setAbortAction(true);

View solution in original post

3 REPLIES 3

Tony Chatfield1
Kilo Patron

Hi, I think that you would want to run your ajax call\check as an onChange() client script so that anytime the serial number variable is updated, the content is validated, I would also add a hidden yes/no variable that is populated by this script and use this field to prevent submission with an on submit script.

For your other issue, what are the results of your debugging
Have you debugged\logged the content and typeof 'serialString' and  'serialArray' to confirm that  serialArray is an array and that it's content is correct and length are as expected? - This would impact the record(s) created by your loop.

 

For the verification I do indeed have an onChange script, I just didn't include it in my post because that part is working. Thanks for the yes/no variable suggestion, I'll keep that in mind when I fine-tune.

 

For the RP script (onSubmit script deactivated), when I log the value for each of my variables (before the for loop) they all come back as undefined. However, when I add a breakpoint on the line after the variables are set, each one is valued/set as expected. The for loop executes the expected number of times but alm_harward is not populating as expected.

Liz23
Tera Expert

For posterity, here is the RP that works, some of the variables needed to be declared as strings:

 

var modelCategory = '81feb9c137101000deeabfc8bcbe5dc4'; // Computer
var model = String(producer.mtm);
var installStatus = '6'; // In Stock
var stockroom = String(producer.stockroom);
var serialNums = String(producer.serial_number);
var serialArray = serialNums.split(",");

for (var i = 0; i < serialArray.length; i++) {
var hw = new GlideRecord('alm_hardware');
hw.initialize();
hw.setValue("model_category", modelCategory);
hw.setValue("model", model);
hw.setValue("serial_number", serialArray[i]);
hw.setValue("install_status", "6");
hw.setValue("stockroom", stockroom);
hw.insert();
}

current.setAbortAction(true);