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

Hi Yeni,



Did you try using one more query in your condition to restrict the type to "form" views only.



It should restrict the transactions where users actually opened the record, (it will not consider the records from the list view).



You can use the same in a before BR and use the above function that you've written.



Let me know if it helps.



Thanks,


Mandar


Hi Mandar,



Yes I did. I added the query that you suggested but the BR is still behaving the same way. I also tried it an before BR but it did not run properly on my instance.



Here is a screen shot of what I am seeing on my instances. I am impersonating a user in IE and I am logged in as admin in chrome. It shows that both myself and the impersonated user are viewing incidents that have not been touched by either one...


two screens.PNG


Hi Yeni,



Quick question, do you want to display the message only if there's an update on the record?



Whenever someone opens the form there will be one entry in "syslog_transaction" table. So, basically whenever someone opens the form there will be an entry and a message would be displayed as above.



However if you want when an update occurs then that could be tricky, please check the URL section if it contains any specific parameter that indicates the update.



Thanks,
Mandar


Hi Mandar,



No, it doesn't have to have an update, I would just like the the message to display if a user has opened the incident prior to another user opening it, so that second user should know that it is being worked on and move on to the next incident. Right now, the BR is displaying the message but it is doing this on incidents that have not been touched by any analyst and that's what's confusing to me :/. Besically, it's displaying the message on all incidents when it should only display it on incidents actually touched by analysts.


Hi Yeni,



Though i cannot pinpoint the issue at the moment, please try a few logs in the BR. Specially try logging the encodedQuery and the record count.



Encoded query will give you an idea of why exactly the "current.sys_id" condition is not getting checked.



Thanks,
Mandar