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

solutioningnow
Giga Guru

Hello,



I have also received similar requirement from one of my client. Actually this is a case of Simultaneous Update where multiple Analyst are working on same tickets. To solve this issue, there are two solutions which are as follows:


1) Create onSubmit Script as mention in Wiki Article Simultaneous Update Alert - ServiceNow Wiki


2) Install the update set from SNCGuru article » How to Perform a Simultaneous Update Alert Check in Service-now



Please mark answer as correct/helpful, if it was helpful



Regards,


Solutioner


Logo.png


Enhance Knowledge NOW@ www.solutioningnow.com


http://www.solutioningnow.com/


Ohh, looks like we need similar functionality on Community itself.



Both of us replied to the same thread...


ohhgr
Kilo Sage
Kilo Sage

Hi Yeni,



If you just want to stop the simultaneous updates then it can be done using the client script functionality provided. It will display an error when the user submits the record that has already been updated.



Simultaneous Update Alert - ServiceNow Wiki



Else, if you want to save the time people spend on analysis itself, then you might need to make a few changes in above script itself.



Check if restricting the records to "Form" view only helps you. You can try something like



transactionGr.addQuery('type', 'form');




Thanks,


Mandar


Thank you both so much for your answers! The simultaneous update script worked when I tried it but I find that this only works on submit, so once the analyst tries to save their changes... I tried to modify it to onLoad to show the pop-up as soon as the analyst opens the incident, but it didn't work.



What I would like to do is to as soon as the incident is opened by any analyst, it gives them a message saying that someone else is already viewing that particular incident so they can move on to another incident in the queue. I just don't know how to accomplish that... Do you think I would have better luck with a Client Script to check for any users that have an incident open.. or what could I do to the business rule to fix it?



Thank you!


Yeny