Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get latest one incident record based on caller in servicenow

Haceena Shaik
Tera Expert

Hi Team,

 

I had a business requirement like, How to return list of latest  1 incident record based on caller.

Means, If Abel Tuter is having 8 incidents, I want the latest one incident.

 

Please help me with the approach.

 

Thanks in advance!

 

Regards,

Haseena

1 ACCEPTED SOLUTION

@Haceena Shaik ,

Please refer below code:

var incidentList = [];
var incident1 = new GlideRecord('incident');
incident1.query();
while(incident1.next()){
    var incident2 = new GlideRecord('incident');
   incident2.addQuery('caller_id',incident1.caller_id);
    incident2.orderByDesc('sys_created_on');
    incident2.query();
    if(incident2.next()){
        incidentList.push(incident2.number.toString());
    }
}
var uniqueIncidentList= new ArrayUtil().unique(incidentList);
gs.print(uniqueIncidentList);

Please let me know this code works for you or not

 

Please mark correct/helpful if this helps you!

View solution in original post

6 REPLIES 6

Unique45
Mega Sage
Mega Sage

Hello @Haceena Shaik 

Below code gives the desired output:

var incident = new GlideRecord('incident');
incident.addQuery('caller_id=62826bf03710200044e0bfc8bcbe5df1');//Caller is Abel Tuter
incident.orderByDesc('sys_created_on');
incident.query();
if(incident.next()){
    gs.print(incident.number);
}

 

Please mark correct/helpful if this helps you!

Thanks for the quick response.

I want the list of atleast one incident based on all callers. But not based on one caller.

@Haceena Shaik ,

Please refer below code:

var incidentList = [];
var incident1 = new GlideRecord('incident');
incident1.query();
while(incident1.next()){
    var incident2 = new GlideRecord('incident');
   incident2.addQuery('caller_id',incident1.caller_id);
    incident2.orderByDesc('sys_created_on');
    incident2.query();
    if(incident2.next()){
        incidentList.push(incident2.number.toString());
    }
}
var uniqueIncidentList= new ArrayUtil().unique(incidentList);
gs.print(uniqueIncidentList);

Please let me know this code works for you or not

 

Please mark correct/helpful if this helps you!

Anirudh Pathak
Mega Sage

Hi,

Please try the below code - 

var inc = new GlideAggregate('incident');
inc.groupBy('caller_id');
inc.query();
while(inc.next()) {
  var caller = inc.getValue('caller_id');
  var incidentCaller = new GlideRecord('incident');
  incidentCaller.addQuery('caller_id', caller);
  incidentCaller.orderByDesc('sys_created_on');
  incidentCaller.query();
  if(incidentCaller.next()) {
    gs.print(incidentCaller.number);
 }
}