Create record for each item selected in List Collector

Dan Solomon
Tera Contributor

Hi there. 

Created a custom table that extended Task that I am wanting to add records to via a Record Producer. 

On the Record Producer, I have added a List Collector with the List table of Location [cmn_location]. 

Long story short - I want the employee to be able to create multiple records at once, with the only thing varying being the location.

So I've tried various iterations of the following: 

var locations = producer.locations.toString();
var array = locations.split(',');

for (var i=0; i < array.length; i++) {

var vst = new GlideRecord('custom_table_here');
vst.initialize();
vst.setValue('location',array[i]);
vst.update();

}

I've adding some logging, and I am seeing the sys_id of the location after the split as I would expect:
find_real_file.png

but the location remains empty on the new record I've created. I even tried adding the single quotes

var ID = "'" + array[i] + "'"  and then changing the setValue to ('location',ID); but still no luck. 

I'm real new and I know I'm just missing something obvious, but I have been trying at this for a while and wouldn't appreciate any assistance. 

Thank you.

 

3 REPLIES 3

HarshTimes
Tera Guru

Hi Dan,

First, I will suggest not to use the variable name as 'array', this is a keyword and it is not a  good practice to use keywords as a variable. It may break the code sometime.

Try using var vloc = Array[i].toString();

Also, print this variable and check the sysid in the location table. Just to confirm.

and then use vloc variable while creating a record.

Also,I will suggest to use insert() while creating record instead of using update().

 

-Harsh

Harsh, 

Thanks for advice. The location is definitely mapping now. 

That being said, I am now getting some weird behavior where I either get an extra record that is empty: 

find_real_file.png

or a duplicate numbered record with a different sys_id:

find_real_file.png

I did see some articles on how I could make the number field unique, but that would only fix one of the behaviors I'm seeing - so I'm instead trying to figure out why its occurring. Any thoughts?

Here is the updated code (if it helps):

var locations = producer.locations.toString();
var locSplit = locations.split(',');

for (var i=0; i < locSplit.length; i++) {
var vloc = locSplit[i].toString();

current.initialize();
current.location = vloc;
current.insert();
}

Thanks again for your help up to this point. 

 

Well I hope you already got the solution to the duplicate number and the extra record - but in case others view this question I just wanted to give the answer.

If you do a loop in a record producer to insert records, you must remember to abort the record producer - otherwise it will created the records within the loop AND one ordinary record (the one that the record producer always creates).

So at the end (outside the loop) insert

current.setAbortAction(true);

and the record producer stops from creating the last record.

Also a good idea is to redirect the user to some sort of "records created" page - in order to do that use

producer.portal_redirect = 'page-you-redirect-to';