Why is for loop in script creating an extra record

Digit
Kilo Guru

I am in the middle of creating a record producer that creates 1-5 records based on the user selection. When I run the code (see below) and select x number of records, I get the expected number of records, but there is always an additional record created first with blank values. (Ex: User selects one for number_of_build_trackers, but gets 2 records - the correct one plus a blank one)

I assume it has something to do with the for loop logic or the arrays initializing strangely, but I can't figure out how to prevent the 'extra' record from being created along with the correct ones.

 

    // Create # of build records based on user selection         
    for (var i = 1; i <= producer.number_of_build_trackers; i++) {

        // Set variable values           
        var appService = producer['app_service' + i];
        var buildName = producer['build_name' + i];

        var buildRecord = new GlideRecord('u_build_tracker');
        buildRecord.initialize();

        // Check if build name is provided             
        if (buildName) {
            // If build name is provided, concatenate app service and build name                 
            buildRecord.setValue('u_name', appService.getDisplayValue() + ' ' + buildName);
        } else {
            // If no build name provided, use app service value                 
            buildRecord.setValue('u_name', appService.getDisplayValue());
        }
        // Set other field values             
        buildRecord.setValue('u_tech_review', producer.tech_review);
        buildRecord.setValue('u_managed_service', appService);
        buildRecord.insert();
    }

 I'd sure love some advice on this one! 

1 ACCEPTED SOLUTION

Then it is already like what Danish mentioned. The Record Producer itself will submit a record + your script will submit 1 or more records. So you will always have one additional record.

 

You might do an current.setAbortAction + a redirect on your Record Producer.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

6 REPLIES 6

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Digit ,

 

I don't think there is anything wrong with the script but it is just the OOTB behaviour when we use record producer. Whenever we submit a record producer a record gets inserted into the table which is mapped to the record producer.

 

To confirm this u can try the same script in background script with giving dummy values or logs it will only create or print 5 times no more or less.

 

But in case of records producer it does create an extra record in the table to which it is mapped. This is the OOTB behaviour of the record producer.

 

Thanks,

Danish

 

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Against which table is your record producer running? Is that also against u_build_tracker?

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi @Mark Roethof ,
Yes, u_build_tracker is the table the record producer is running against.

Hi @Digit 

 

try removing this line n try

 buildRecord.insert();

 

Thanks,
Danish