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

Hello Ravi,

 

I have modified the script and tested still it is skipping two auto generated numbers, I did check logs and it did not went to catch block 

Logs:
Inserted record with Sys ID: 161b555f470c56d0a83c7351e36d4361
Inserted record with Sys ID: ee1b555f470c56d0a83c7351e36d4366

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/

Hi Ravi

Updated above script and tested, now it is skipping 1 autogenerated number

SrinivasK1_0-1724242014532.png

Kindly check below logs.

image.pngimage (1).pngimage (2).png

Atleast accept the solution.. its working for you... only one is skipping..We are trying hard to provide you the solution

try the below

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

// 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/

geetha14
Tera Contributor

Hi @Srinivas K1 

Please try below script.

    if ((producer.category == '4000') && (producer.subcategory == '4054')) {
        var claims = producer.related_claims.toString();
        var list = claims.split(',');
        current.u_related_claim = list[0].toString();

        for (var i = 1; 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();
            gr.u_hp_contact_region = producer.u_hp_contact_region;
            gr.insert();
        }
    }
If my answer helped you in any way, please then mark it as helpful or correct.