
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2022 01:21 AM
I wanted to print incident numbers: But, my first approach is printing the correct output, but the second approach is printing the wrong output. I am not able to understand why?
//APPROACH 1 printing the correct output
var newIncidents = [];//a new array for the incs
var counter = 1;
var incidentGR = new GlideRecord('incident');
while(counter <= 5){
incidentGR.newRecord();
incidentGR.short_description = 'Incident #' +counter;
counter++;
newIncidents.push(incidentGR.insert());
}
for(var i = 0; i < newIncidents.length; i++){
gs.print(incidentGR.number);
}
//APPROACH 2 :printing the wrong output
var newIncidents = [];//a new array for the incs
var counter = 1;
var incidentGR = new GlideRecord('incident');
while(counter <= 5){
incidentGR.newRecord();
incidentGR.short_description = 'Incident #' +counter;
counter++;
newIncidents.push(incidentGR.insert());
}
for(var i = 0; i < newIncidents.length; i++){
gs.print(newIncidents[i].number);
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2022 01:28 AM
var newIncidents = [];//a new array for the incs
var counter = 1;
var incidentGR = new GlideRecord('incident');
while(counter <= 5){
incidentGR.newRecord();
incidentGR.short_description = 'Incident #' +counter;
counter++;
newIncidents.push(incidentGR.insert()); //This will return sys_ids of records
}
for(var i = 0; i < newIncidents.length; i++){
//to get number for each you need to locate record & print
var inci=new GlideRecord('incident');
inci.get(newIncidents[i]);
gs.print(inci.number);
}
Also, the first approach will not work. It will pritn same number only. Give it a check.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2022 01:25 AM
Hi,
What is that you are looking for , am a bit confused.
Generally to get newly created incidents this is what we do :
var c=0;
var check= new GlideRecord('incident');
check.addQuery('category','software');
check.setLimit(10);
check.query();
while(check.next())
{
c++
gs.print('check the :'+check.number);}
gs.print('total nnumber of record is:'+c);
Mark my answer correct & Helpful, if Applicable.
Thanks,
Sandeep

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2022 01:36 AM
Hi, Sandeep thanks for your response,
I have created 5 new incidents, but now when I want to print those incident numbers I am not able to do it.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2022 01:28 AM
var newIncidents = [];//a new array for the incs
var counter = 1;
var incidentGR = new GlideRecord('incident');
while(counter <= 5){
incidentGR.newRecord();
incidentGR.short_description = 'Incident #' +counter;
counter++;
newIncidents.push(incidentGR.insert()); //This will return sys_ids of records
}
for(var i = 0; i < newIncidents.length; i++){
//to get number for each you need to locate record & print
var inci=new GlideRecord('incident');
inci.get(newIncidents[i]);
gs.print(inci.number);
}
Also, the first approach will not work. It will pritn same number only. Give it a check.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2022 01:38 AM
Hi Jaspal,
Okay, now it makes sense.
Thank you.