Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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

Digit
Kilo Guru

Thanks for the assistance, @Mark Roethof and @Danish Bhairag2 !!

Per your suggestion, adding this line at the end prevented the 'blank' record being created.

current.setAbortAction(true);

REALLY appreciate the help guys.