Multiple records insert from record producer using List Collector Type field

Srinivas K1
Tera Contributor

Hi All,
I have a requirement,

 

In Record Producer I have "List Collector" Field (name: related_claims ) and I need to insert multiple records based on number of selections in List Collector field for this I have written a script in record producer level kindly check below script
For example: If I choose two values in "related_claims" Field it should create two records in the table but It creates 3 records one record is creating from record producer  and remaining two are creating from for loop and target field is not updating (i.e: u_related_claim after splitting list collector field I need to update this field but out of 2 inserted records only one is updating other one not updating ) to restrict 3rd record while submitting I have written like current.setAbortAction(true)  after written this line it is working as expected the only concern is it should not skip auto generated number (i.e INC000XXX) ) but here I observed that it is skipping auto generated number Ex: last inserted record number is INC0000236 it created two records like INC0000238, INC0000239. so here it skipped INC0000237  to overcome this I tried few approaches but didn't work can anyone kindly assist me in this.

Thanks in advance

 

Script: 

if ((producer.category == '4000') && (producer.subcategory == '4054')) {
    var claims = producer.related_claims.toString();
    var list = claims.split(',');
    gs.info('claimsssss' + claims);
    // current.u_related_claim = producer.related_claims.toString();
    for (var i = 0; i < list.length; i++) {
        // if (i == 0) {
        // current.u_related_claim = list[i];
        //     gs.info('ifstmt' + list[i] + " , " + i);
        // } else {
        var gr = new GlideRecord('sn_customerservice_case');
        gs.info('elsestmt' + list[i]);
        gr.category = producer.category;
        gr.subcategory = producer.subcategory;
        gr.short_description = producer.short_description;
        gr.partner = producer.account;
        gr.assigned_to = producer.assigned_to;
        gr.parent = producer.parent;
        gr.contact = producer.contact;
        gr.u_contact_email = producer.u_contact_email;
        gr.u_related_order = producer.u_related_order;
        gr.u_related_claim = list[i].toString();
        gr.u_hp_contact_region = producer.u_hp_contact_region;
        gr.insert();
        //current.setAbortAction(true);
    }
}
1 ACCEPTED SOLUTION

Try the below please send the screenshot :-

 

if ((producer.category == '4000') && (producer.subcategory == '4054')) {
var claims = producer.related_claims.toString();
var list = claims.split(',');
gs.info('Claims: ' + claims);

current.setAbortAction(true);

// Insert records based on List Collector selections
for (var i = 0; i < list.length; i++) {
var claim = list[i].trim(); // Trim any extra spaces
gs.info('Processing claim: ' + claim);

if (claim) { // Ensure claim is not empty
try {
var gr = new GlideRecord('sn_customerservice_case');
gr.category = producer.category;
gr.subcategory = producer.subcategory;
gr.short_description = producer.short_description;
gr.partner = producer.account;
gr.assigned_to = producer.assigned_to;
gr.parent = producer.parent;
gr.contact = producer.contact;
gr.u_contact_email = producer.u_contact_email;
gr.u_related_order = producer.u_related_order;
gr.u_related_claim = claim; // Update with specific claim
gr.u_hp_contact_region = producer.u_hp_contact_region;

var newSysId = gr.insert();
gs.info('Inserted record with Sys ID: ' + newSysId);
} catch (e) {
gs.error('Error inserting record for claim: ' + claim + ', Error: ' + e.message);
}
} else {
gs.warn('Skipping empty claim value at index: ' + i);
}
}
}

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

10 REPLIES 10

Mark Manders
Mega Patron

You are aborting the record creation and because of that, the number is skipped. The way numbering works is that on the trigger of creation, the next available number is taken and reserved until the record is saved to the server (the actual creation of the record). If the record isn't created, the number will not be used on an actual record and the next one will be created with the next available number. 

So aborting the creation, will skip that number and you can't do anything about it, because you were starting to create it and then said 'no, I don't need it'. It works the same as when you start to create a Change request and decide half way through you don't need it and just click the form away. That number is gone and won't be re-used. 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Ravi Gaurav
Giga Sage
Giga Sage

Hi Srinivas,

 

can you use the below code :-

if ((producer.category == '4000') && (producer.subcategory == '4054')) {
var claims = producer.related_claims.toString();
var list = claims.split(',');
gs.info('Claims: ' + claims);


current.setAbortAction(true);

 

// Insert records based on List Collector selections
for (var i = 0; i < list.length; i++) {
var gr = new GlideRecord('sn_customerservice_case');
gr.category = producer.category;
gr.subcategory = producer.subcategory;
gr.short_description = producer.short_description;
gr.partner = producer.account;
gr.assigned_to = producer.assigned_to;
gr.parent = producer.parent;
gr.contact = producer.contact;
gr.u_contact_email = producer.u_contact_email;
gr.u_related_order = producer.u_related_order;
gr.u_related_claim = list[i].toString(); // Update with specific claim
gr.u_hp_contact_region = producer.u_hp_contact_region;
gr.insert();
}
}

 

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Hi Ravi,

Thank you for the response I tried above code which you updated, but now it skipping two auto generated  numbers like below.
CT0010815
CT0010814
CT0010811
CT0010810
Here It skipped two auto generated numbers.

The issue of skipped auto-generated numbers typically occurs due to aborted or failed insertions in the loop, which might be happening if the code is hitting an error or condition where the insert is not completed successfully.

To avoid this, you can wrap the insertion logic in a try-catch block to capture any potential errors.

Use the update code :-

if ((producer.category == '4000') && (producer.subcategory == '4054')) {
var claims = producer.related_claims.toString();
var list = claims.split(',');
gs.info('Claims: ' + claims);


current.setAbortAction(true);

// Insert records based on List Collector selections
for (var i = 0; i < list.length; i++) {
try {
var gr = new GlideRecord('sn_customerservice_case');
gr.category = producer.category;
gr.subcategory = producer.subcategory;
gr.short_description = producer.short_description;
gr.partner = producer.account;
gr.assigned_to = producer.assigned_to;
gr.parent = producer.parent;
gr.contact = producer.contact;
gr.u_contact_email = producer.u_contact_email;
gr.u_related_order = producer.u_related_order;
gr.u_related_claim = list[i].toString(); // Update with specific claim
gr.u_hp_contact_region = producer.u_hp_contact_region;
var newSysId = gr.insert();
gs.info('Inserted record with Sys ID: ' + newSysId);
} catch (e) {
gs.error('Error inserting record for claim: ' + list[i] + ', Error: ' + e.message);
}
}
}

 

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/