Need to create a multiple record when the form submit

vinuth v
Tera Expert

Hi All,

 

I am working on one of the form in this form I am using multi row variable set, contains Product Model, Keyboard Language, Quantity and Serial Number.

vinuthv_0-1697105046694.png

When the form submitted these 5 

4RW3GY3
3Q2XBY3
4962GY3
JQW3GY3
7QW3GY3 records will be created in the alm_hardware table.

 

I tried with the below script

var multiRowSet = [];
var array=[];
var ass;
var sub;
multiRowSet.push(abc);
multiRowSet = JSON.parse(multiRowSet);
workflow.scratchpad.assetlength= multiRowSet.length;
gs.log('start63:' + multiRowSet.length);
for (var i = 0; i < multiRowSet.length; i++) {
array.push(multiRowSet[i].serial_number_1);
ass = multiRowSet[i].serial_number_1;
sub = multiRowSet[i].model2;
gs.log("asssub " + i + multiRowSet[i].serial_number_1+"/"+multiRowSet[i].model2);
gs.log('asssub71:'+ass+ "-"+sub);
var gr1 = new GlideRecord('alm_hardware');
gr1.initialize();
//gr1.addQuery('serial_number',multiRowSet[i].serial_number_1);
// gr1.query();
// if(gr1.next()){
gr1.serial_number=ass;
gr1.install_status = "6";
gr1.stockroom=current.variables.stockroom;
gr1.acquisition_method=current.variables.acquisition_method;
gs.log("stoacq78:"+current.variables.stockroom +"-"+current.variables.acquisition_method);
gr1.model = sub;
gr1.insert();
}

 

This script is working fine when I give the single Serial number, like below

vinuthv_1-1697105373163.png

If I give the multiple serial number, then this script is not working.

 

 

 

Note : Tried without using multi row variable set, and it is working as expected.

 

var sto=current.variables.stock_reservation;
var serial_number =current.variables.ser_number.toString();
var arrSerialNumber = serial_number.split('\n');
for (var i in arrSerialNumber){
    insertHardwareBySerialNumber(arrSerialNumber[i]);
}

 

/**
* Create new Hardware by Serial Number
* @return {void}: insert new Hardware record
*/
function insertHardwareBySerialNumber(serial_number){
    var grHardware = new GlideRecord('alm_hardware');
    grHardware.addQuery('serial_number', serial_number);
    grHardware.query();
    if(grHardware.next()){
        return; //return if serial number existed.
    }

 //insert new hardware
    grHardware.initialize();
    grHardware.serial_number = serial_number;
    grHardware.install_status='9';
    //grHardware.substatus='Commissioned';
    grHardware.model=current.variables.Model;
    grHardware.stockroom=current.variables.stockroom;
   // grHardware.location='AE - Dubai City';
}

 

I need to create a multiple records using multi row variable set( Insert the multiple serial number on the serial number field)

vinuthv_2-1697105775478.png

Here I need to create 10 records when form get submitted.

 

 

 

Please any one provide me the input,

 

Thanks in advance,

Vinuth

6 REPLIES 6

Hi @Vishal Birajdar 

 

I tried with the above script and it is working fine and in the same script I wanted to implement to the configuration item(cmdb_ci_computer) as well,

vinuthv_0-1697183690024.png

 

 

I tried with the below script, But its not working

var mrvs = current.variables.stockroom_order_windows_computers;
 
/*2. Parse the MRVS*/
var parseMRVS = JSON.parse(mrvs);
 
/*3. Loop through parsed MRVS*/
for (var i = 0; i < parseMRVS.length; i++) {
 
/*4. Get the serial number from MRVS & split it , we will get the array*/
var serNum = parseMRVS[i].serial_number_1.toString().split('\n');
gs.log("serNum48"+serNum);
 
/*5. Loop through serNum*/
for(var j=0; j< serNum.length; j++){
 
/*6. Glide record on Hardware table */ 
var gr1 = new GlideRecord('alm_hardware');
gr1.initialize();
gr1.serial_number = serNum[j];  //serial number
gr1.install_status = "6";
gr1.stockroom=current.variables.stockroom;
gr1.acquisition_method=current.variables.acquisition_method;
gr1.model= parseMRVS[i].Model;  //variable fro  MRVS
gr1.insert();
 
var ad = new GlideRecord("cmdb_ci_computer");
ad.addQuery('serial_number', serNum[j]);
ad.query();
if (ad.next()){
ad.name = "SR-" + serNum[j];
ad.update();
}
}
}

 

Hi @vinuth v 

 

In my understanding, You already have "Configuration Item" with that Serial number & you want to update its name & set it in "alm_hardware" record in "Configuration item" field.

 

Forgive me if I'm interpreting in wrong way....is my understanding correct...??

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates