insert records with different short descrition using background script

tGhadage
Tera Contributor

Hi @all

var increcord = new GlideRecord('incident');
for(i=1;i<=100;i++){
increcord.initialize();
increcord.short_description = 'test '+' '+ i;
increcord.state == 3;
increcord.insert();

here I am trying to insert 100 records into the instance using background script with different short description for each record can anyone help me how shall I do  it using script ? short description  can be different as using inc number and other sentences. 

thanks!! 

7 REPLIES 7

Possibly.

Please explain further. I'm not sure if I understand your scenario.

Krushna R Birla
Kilo Sage

Hi @tGhadage  

 

After reviewing your script, I noticed a few issues:

  • You should use the assignment operator = instead of == in incRecord.state = 3;, as you’re setting the state value rather than comparing it.
  • Your script sets the state directly to "On Hold." Instead, consider setting the initial state to "New" or "WIP" as per the process. Also, when setting the state to "On Hold," make sure to specify the "On Hold Reason."
  • To follow best practices, ensure that you account for all mandatory fields required by the table when inserting records. 

Here's a revised script for your reference:

 

 

var incRecord = new GlideRecord('incident');
for (var i = 1; i <= 100; i++) {
    incRecord.initialize();
    incRecord.caller_id = gs.getUserID(); // Set caller to logged-in user
    incRecord.short_description = 'test ' + i; // Unique short description
    incRecord.state = 3; // On-Hold State
    incRecord.hold_reason = 1; // On Hold Reason
    incRecord.insert();
}

 

 

KrushnaRBirla_0-1728814139748.png

 

This will definitely help you resolve your issue. Let me know if you need any guidance on ServiceNow scripting best practices; you can DM me on LinkedIn.

 

If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

 

Best Regards,
Krushna Birla

Hi @tGhadage  

 

Did you tried this approach

 

If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

 

Best Regards,
Krushna Birla