How to retrieve same caller records with number.

Sai25
Giga Guru

Hi All,

I want same caller incident records , right now my output.

number: INC0000029 caller_id Virtual Agent
number: INC0000037 caller_id Sam Sorokin
number: INC0000036 caller_id Sam Sorokin
number: INC0015637 caller_id Abel Tuter
number: INC0016259 caller_id Abel Tuter
number: INC0016258 caller_id Abel Tuter
number: INC0016256 caller_id Abel Tuter


from the above example I need records like this.
caller_id:abel Tuter, Numbers:INC0015637,INC0016259, INC0016258 ,INC0016256 ;
caller_id:Sam Sorokin, Numbers:INC0000037,INC0000036;

Could you please help me what I need to do get the records like this.

Script:

var arr=[];
var number=[];
var inc=new GlideRecord('incident');
inc.addQuery('state',2);
inc.setLimit(5);
inc.query();
while(inc.next()){
gs.print("number :"+inc.number+"caller_id:"+inc.caller_id.getDisplayValue());
if (arr.indexOf(inc.caller_id.toString()) == -1) {
arr.push(inc.caller_id.toString());

}

}
gs.print(arr+number);
for(i=0;i<arr.length;i++){
if(inc.caller_id==arr){
number.push(inc.number.toString());
}
var user=new GlideRecord('sys_user');
user.addQuery('sys_id',arr[i]);
user.query();
while(user.next()){
gs.print("user"+user.first_name+number);

}
}

Thank You,

Sai.

1 ACCEPTED SOLUTION

Dan H
Tera Guru

Hi Sai,

This took me quite some time to script, it may not be the most elegant or efficient way but it fulfills your requirement.

I've tried to comment it so that you can understand it better.

var incidents = new GlideRecord('incident');
incidents.addQuery('state', 2);
incidents.setLimit(5);
incidents.query();

var arrayUtil = new ArrayUtil();
var userList = [];
var numberList = [];
var objList = [];
while (incidents.next()) {
  gs.log('caller: ' + incidents.caller_id + ' - Number: ' + incidents.number);
  numberList.push(incidents.number.toString());

  //Push unique entries of caller ids to array [userList].
  if (!arrayUtil.contains(userList, incidents.caller_id.toString())) {
    userList.push(incidents.caller_id.toString());
  }
}

//For debugging: So you can see Contents of userList and NumberList
// for (i = 0; i < userList.length; i++) {
//   gs.log('Array contains: ' + userList[i]);
// }

// for (i = 0; i < numberList.length; i++) {
//   gs.log('numberList contains: ' + numberList[i]);
// }

//Checks for incidents where the caller id is one of the caller ids in the userList
//If it finds some, push those incidents into concatList, but only if theyre also in the numberList
populateObjectArray(userList, numberList);

function populateObjectArray(userList, numberList) {
  var concatList = [];
  for (i = 0; i < numberList.length; i++) {
    var incQuery = new GlideRecord('incident');
    incQuery.addQuery('caller_id', userList[i]);
    incQuery.query();
    while (incQuery.next()) {
      for (j = 0; j < numberList.length; j++) {
        if (incQuery.number == numberList[j]) {
          var obj = {
            caller_id: incQuery.caller_id.toString(),
            number: incQuery.number.toString(),
          };

          concatList.push(obj);
        }
      }
    }
  }

  //Debugging print
  // for (i = 0; i < concatList.length; i++) {
  //   gs.log(concatList[i].caller_id + ':' + concatList[i].number);
  // }

  //When a caller id has multiple incidents, concat those incidents
  //Replace the number property of the object with all numbers for that caller id
  var result = [];
  for (var i = 0; i < concatList.length; i++) {
    var data = concatList[i];
    var found = false;
    for (var j = 0; j < result.length; j++) {
      if (result[j].caller_id === data.caller_id) {
        found = true;
        //gs.log(result[j].number);
        result[j].number = result[j].number + ', ' + data.number;
        break;
      }
    }
    if (!found) {
      result.push(data);
    }
  }

  //The result array is your desired result
  for (i = 0; i < result.length; i++) {
    gs.log(
      'Caller id: ' + result[i].caller_id + ' Incidents: ' + result[i].number
    );
  }

  //Need to replace caller ids (sys id) with user name
  replaceCallerIds(result);
  function replaceCallerIds(result) {
    for (i = 0; i < result.length; i++) {
      var userRec = new GlideRecord('sys_user');
      userRec.get(result[i].caller_id);
      if (userRec) {
        gs.log('');
        result[i].caller_id = userRec.user_name;
      }
    }
  }

  //The result array is your desired result
  for (i = 0; i < result.length; i++) {
    gs.log(
      'Caller id: ' + result[i].caller_id + ' Incidents: ' + result[i].number
    );
  }
}

 

Result (Last 4 lines is the output you want):

find_real_file.png

 

Id appreciate if you could test and then mark this answer as helpful and correct

Let me know if any questions.

Regards,

Dan H

 

View solution in original post

6 REPLIES 6

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

The script below will give you all unique users, and then you can query with them one by one to collect other data

THSI IS AN EXAMPLE

 

itilUsers = new GlideAggregate("incident");
itilUsers.addAggregate("COUNT");
itilUsers.addQuery('active', true); //add your query
itilUsers.groupBy("caller_id");
itilUsers.query();

while(itilUsers.next()) {
gs.print(itilUsers.caller_id); //unique users
}

 

-Anurag

-Anurag

Dan H
Tera Guru

Hi Sai,

This took me quite some time to script, it may not be the most elegant or efficient way but it fulfills your requirement.

I've tried to comment it so that you can understand it better.

var incidents = new GlideRecord('incident');
incidents.addQuery('state', 2);
incidents.setLimit(5);
incidents.query();

var arrayUtil = new ArrayUtil();
var userList = [];
var numberList = [];
var objList = [];
while (incidents.next()) {
  gs.log('caller: ' + incidents.caller_id + ' - Number: ' + incidents.number);
  numberList.push(incidents.number.toString());

  //Push unique entries of caller ids to array [userList].
  if (!arrayUtil.contains(userList, incidents.caller_id.toString())) {
    userList.push(incidents.caller_id.toString());
  }
}

//For debugging: So you can see Contents of userList and NumberList
// for (i = 0; i < userList.length; i++) {
//   gs.log('Array contains: ' + userList[i]);
// }

// for (i = 0; i < numberList.length; i++) {
//   gs.log('numberList contains: ' + numberList[i]);
// }

//Checks for incidents where the caller id is one of the caller ids in the userList
//If it finds some, push those incidents into concatList, but only if theyre also in the numberList
populateObjectArray(userList, numberList);

function populateObjectArray(userList, numberList) {
  var concatList = [];
  for (i = 0; i < numberList.length; i++) {
    var incQuery = new GlideRecord('incident');
    incQuery.addQuery('caller_id', userList[i]);
    incQuery.query();
    while (incQuery.next()) {
      for (j = 0; j < numberList.length; j++) {
        if (incQuery.number == numberList[j]) {
          var obj = {
            caller_id: incQuery.caller_id.toString(),
            number: incQuery.number.toString(),
          };

          concatList.push(obj);
        }
      }
    }
  }

  //Debugging print
  // for (i = 0; i < concatList.length; i++) {
  //   gs.log(concatList[i].caller_id + ':' + concatList[i].number);
  // }

  //When a caller id has multiple incidents, concat those incidents
  //Replace the number property of the object with all numbers for that caller id
  var result = [];
  for (var i = 0; i < concatList.length; i++) {
    var data = concatList[i];
    var found = false;
    for (var j = 0; j < result.length; j++) {
      if (result[j].caller_id === data.caller_id) {
        found = true;
        //gs.log(result[j].number);
        result[j].number = result[j].number + ', ' + data.number;
        break;
      }
    }
    if (!found) {
      result.push(data);
    }
  }

  //The result array is your desired result
  for (i = 0; i < result.length; i++) {
    gs.log(
      'Caller id: ' + result[i].caller_id + ' Incidents: ' + result[i].number
    );
  }

  //Need to replace caller ids (sys id) with user name
  replaceCallerIds(result);
  function replaceCallerIds(result) {
    for (i = 0; i < result.length; i++) {
      var userRec = new GlideRecord('sys_user');
      userRec.get(result[i].caller_id);
      if (userRec) {
        gs.log('');
        result[i].caller_id = userRec.user_name;
      }
    }
  }

  //The result array is your desired result
  for (i = 0; i < result.length; i++) {
    gs.log(
      'Caller id: ' + result[i].caller_id + ' Incidents: ' + result[i].number
    );
  }
}

 

Result (Last 4 lines is the output you want):

find_real_file.png

 

Id appreciate if you could test and then mark this answer as helpful and correct

Let me know if any questions.

Regards,

Dan H

 

Hi Sai

Did this script work for you, or do you have any follow up queries?

If not would you mind closing the thread by accepting it as the answer?

many thanks

Jaspal Singh
Mega Patron
Mega Patron

Try below.

var arr=[];
var number='';
var res=[];
var changeArr=[];
var inc=new GlideRecord('incident');
inc.addQuery('state',2);
inc.setLimit(10);
inc.query();
while(inc.next()){
gs.print("number :  "+inc.number+"   caller_id: "+inc.caller_id.getDisplayValue());
if (arr.indexOf(inc.caller_id.toString()) == -1) {
arr.push(inc.caller_id.toString());
}
}


//Use OOB ArrayUtil
var au = new ArrayUtil();
var newChangeArr = au.unique(arr);
var newRes = au.unique(arr); //Gets unique callerids

//GlideRecord incident table to get results.
for(var i=0;i<newRes.length;i++)
{
var inci=new GlideRecord('incident');
inci.addQuery('caller_id',newRes[i]);
inci.addQuery('state',2);
inci.query();
while(inci.next())
{
number=inci.number+','+number;

}
gs.print('Caller ID: '+inci.caller_id.getDisplayValue()+' has incident numbers '+number);
}