Insert 10 incidents from Incident table in an array

vinuth v
Tera Expert

Hi All,

 

How to Insert 10 incidents from Incident table in an array.

 

Thanks in advance,

Vinuth

6 REPLIES 6

SN_Learn
Kilo Patron
Kilo Patron

Hi @vinuth v ,

 

Please try the below in background script:

var incArr =[];

var incGr = new GlideRecord('incident');
incGr.addActiveQuery();
incGr.setLimit(10);
while(incGr.next())
{
incArr.push(incGr.number.toString());
}
gs.info(incArr);

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Hi @SN_Learn ,
Thank you,
Actually I wanted to create new 10 records in incident table using array and I tried with the below code but it's not working.
var incArr =[];
var incGr = new GlideRecord('incident');
incArr.initialize();
incGr.addActiveQuery();
incGr.caller_id='62826bf03710200044e0bfc8bcbe5dd6';
incGr.short_description='test';
incGr.setLimit(10);
while(incGr.next())
{
incArr.push(incGr.number.toString());
incArr.insert();
}
gs.info(incArr);
 
and Tried with the creating single record and it is working as expoected.

var gr=new GlideRecord('incident');

gr.intialize();

gr.caller_id='62826bf03710200044e0bfc8bcbe5dd6';

gr.short_description="62826bf03710200044e0bfc8bcbe5dd6+'\n'+test";

gr.insert();

@vinuth v ,

 

Try the below:

var incidentNumbers = [];

for (var i = 0; i < 10; i++) {
   
        var incident = new GlideRecord('incident');
            incident.initialize();
                incident.short_description = 'Incident Test';               
                 incident.caller_id = '62826bf03710200044e0bfc8bcbe5dd6':
                 incident.insert(); 

        incidentNumbers.push(incident.number);
        }

gs.info('Incidents Created: ' + incidentNumbers.join(', '));
----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

This script will create 10 incident records, then result in an array of the new incident Numbers:

 

var incArr =[];
var cnt = 0;
while (cnt < 10) { //create 10 incident records
    var incGr = new GlideRecord('incident');
    incGr.initialize();
    incGr.caller_id='62826bf03710200044e0bfc8bcbe5dd6';
    incGr.short_description='test';
    incGr.insert();
    incArr.push(incGr.number);
    cnt++;
}
gs.info(incArr.join(',');