How to get the person's name who is currently working/viewing the incident?

YenGar
Mega Sage

Hi All,

 

I am working on a business rule that can tell the Help Desk analyst know who is working/viewing the incident they click on. We had a few times that multiple analysts were working on the same incident because they didn't know if someone was already working it. They clicked on it at the same time and after they put their work notes, they noticed that it had the other's analysts names on the work notes also. We would like to prevent that from happening, so I am trying to get a message to display at the top of the incident that says "User 'Jane Doe' is viewing this incident" or something like that.

 

I found a BR by Andrew Venables in the ServiceNow share that is perfect for our needs. The only problem is that when I view the open incidents, it tells me that all the users logged in are viewing it. For example, if I click on Incident #10, it tells me that Analyst1 and Analyst2 are viewing it but they have not opened it. If I click on Incident #11, it tells me the same thing.

 

What I would like to do is to display the names of the analyst in one incident that is actually clicked on and opened, not all incidents... Do you have any idea how I can get this done? Please advise!!

 

This is the BR:

 

getClientTransactions();

 

 

function getClientTransactions() {

  var transactionGr = new GlideAggregate('syslog_transaction');

  transactionGr.addQuery('client_transaction', true);

  transactionGr.addQuery('table', current.getTableName());

  transactionGr.addQuery('URL', 'STARTSWITH', '/'+current.getTableName()+'.do');

  transactionGr.addQuery('URL', 'CONTAINS', current.sys_id);

  transactionGr.addEncodedQuery('sys_created_onONLast 1 minutes@javascript:gs.minutesAgoStart(1)@javascript:gs.minutesAgoEnd(0)');

  //transactionGr.addQuery('sys_created_on', '>=', gs.minutesAgo(1));

  transactionGr.addQuery('sys_created_by', '!=', gs.getUserName());

  transactionGr.groupBy('sys_created_by');

  transactionGr.addAggregate('MAX', 'sys_created_on');

  transactionGr.orderByAggregate('MAX', 'sys_created_on');

  transactionGr.query();

  if (transactionGr.hasNext()) {

  var userGr, user, msg = '', userCount = 0;

  while (transactionGr.next()) {

  userCount++;

  userGr = new GlideRecord('sys_user');

  user = transactionGr.sys_created_by;

  if (userGr.get('user_name', transactionGr.sys_created_by)) {

  user = userGr.getDisplayValue();

  }

  var nowGdt = new GlideDateTime();

  var createdGdt = new GlideDateTime(transactionGr.getAggregate('MAX', 'sys_created_on'));

  var dur = GlideDateTime.subtract(createdGdt, nowGdt);

  msg += user+' is viewing this record '+dur.getDisplayValue().toLowerCase()+' ago';

  }

  msg = ' viewed this '+current.getClassDisplayValue()+' in the last minutes:'+msg;

  if (userCount == 1) {

  msg = 'Another user has'+msg;

  } else {

  msg = 'Other users have'+msg;

  }

  gs.addInfoMessage(msg);

  }

}

 

This is what I would like to show on incidents that actually get clicked on by any analyst so others can see it.

what i want to show.PNG

This shows on an incident that was not clicked on or opened by any of the mentioned analysts.

it shows two people.PNG

This shows that I am viewing the incident, but I did not click on it.

I havent clicked on this incident.PNG

 

Thank you all for your time and help!

Yeny

1 ACCEPTED SOLUTION

Hi Yeni,



I wrote a similar code to above one but using only GlideRecord, check if it satisfies your requirements.



Write the below code in a display business rule on incident table.



/****************************************************************************************************


getClientTransactions();




function getClientTransactions() {



  var transactionGr = new GlideRecord('syslog_transaction');


  var queryToUse = "urlSTARTSWITH/incident.do^type=form^sys_created_by!="+gs.getUserName()+"^urlLIKEsys_id="+current.sys_id+"^sys_created_onONLast 15 minutes@javascript:gs.minutesAgoStart(15)@javascript:gs.minutesAgoEnd(0)";


  transactionGr.addEncodedQuery(queryToUse);


  transactionGr.orderBy('sys_created_by');


  transactionGr.orderByDesc("sys_created_on");


  transactionGr.query();



  var previousUser = "", messageToUser = "", allUsers = [];


  var nowGdt = new GlideDateTime();


  var createdOnGdt, timeSpent;


  var userCount = 0;



  while (transactionGr.next()) {


  if(previousUser != transactionGr.getValue("sys_created_by")) {


  previousUser = transactionGr.getValue("sys_created_by");


  createdOnGdt = new GlideDateTime(transactionGr.sys_created_on);


  timeSpent = GlideDateTime.subtract(createdOnGdt, nowGdt);


  allUsers.push(getUserName(transactionGr.getValue("sys_created_by"))+' is viewing this record from '+timeSpent.getDisplayValue().toLowerCase());


  userCount++;


  }


  }



  if (allUsers.length != 0) {



  gs.addInfoMessage(allUsers.join(","));


  }


}




function getUserName(userName) {



  var sysUser = new GlideRecord("sys_user");


  sysUser.addQuery("user_name", userName);


  sysUser.query();


  if(sysUser.next()) {


  return sysUser.getDisplayValue();


  }


  return 'guest';


}


******************************************************************************************/



Note that this is specific to incident table, if you have requirements that need you to generalize it, make sure you make the necessary changes.



Thanks,


Mandar


View solution in original post

23 REPLIES 23

NewUser5
Kilo Contributor

Hi ,

 

I want if a user clicks on the particular incident, it will show the users working on that particular incident. for example for incident #1 , it will show the name of users working on it and then create a report of this and display on dashboard.

But for this script it shows for all the incidents on user working on it.

 

Thanks

 

When you get a moment, can you share a screen shot of the incident record along with the script? 

 

Yeny

NewUser5
Kilo Contributor

Hi yeni ,

It gets solved.Can you tell me can i make report of the user viewing the particular incident at same time . I want to show this on dashboard.

If it is possible please help.

Thanks

You won't be able to report on it unless you drop the user's name that's viewing the incident in a custom reference field. Since all you're doing is showing a meesage, it's not really reportable until you have the data saved somewhere. Even then, I wouldn't recommend it becuase as people are viewing the incident, you'd have to be constantly updating the incident with the new user that's viewing to make it reportable. 

NewUser5
Kilo Contributor

Yeah i created a field and push the message in that field..i m trying to referesh that particular field after every 1 min. but there is performance issues in that , database getting hitted again and again. So i drop this idea.

Do you have another method through which i can create real time dashboard ?