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

Hi Jaspal,

Tried with the its giving duplicate records in each line.

*** Script: Caller ID: Virtual Agent has incident numbers ,INC0000029
*** Script: Caller ID: Sam Sorokin has incident numbers ,INC0000029,INC0000037
*** Script: Caller ID: Abel Tuter has incident numbers ,INC0000029,INC0000037,INC0015637,INC0016259,INC0016683,INC0016070,INC0016725,INC0015469,INC0017098
*** Script: Caller ID:  has incident numbers ,INC0000029,INC0000037,INC0015637,INC0016259,INC0016683,INC0016070,INC0016725,INC0015469,INC0017098,INC0016137

Hi Sai, did you try mine? No duplicates