Auto number/increment when adding rows to multi-row variable set

Nardo32
Tera Contributor

I'm trying to auto increment the Line Number field (see screenshot) in a multi-row variable set with no luck.  I've tried different scripts but they all seemed to fail to find the existing value of the Line Number to increment to the next number.  Has anyone been successful in doing this?  

 

I've tried the code included in the this post: https://www.servicenow.com/community/now-platform-forum/how-to-create-auto-increment-variable-in-ser...

 

I've tried different variations of the code from the post and this is most recent one I have tried:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
var mrvs = g_form.getValue('line_number');
if (mrvs) { // Check if mrvs is not empty
try {
var json = JSON.parse(mrvs);
for (var i in json) {
json[i].line_number = parseInt(i) + 1; // Convert 'i' to a number and add 1
}
var jsonstring = JSON.stringify(json);
g_form.setValue('line_number', jsonstring);
} catch (error) {
g_form.addErrorMessage('Error parsing JSON: ' + error);
}
} else {
g_form.addErrorMessage('The line number value is empty.');
}
}
}

 

Nardo32_0-1696548838884.png

Any help will be greatly appreciated.  Thanks in advance!

 

1 REPLY 1

Nardo32
Tera Contributor