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

Thank you for your help, Mandar.


I will do what you suggested and hopefully I can get it to work.



Again, thank you!!!


Yeny


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


Hi Mandar,



I tried this and it worked perfectly! I tested it with different users and it works just like I wanted it to. Thank you so much for providing this code and for all your help! Now, it only displays the message on the incident that actually gets clicked on.



Thank you so much for your help, Mandar!!



Yeny.


NewUser5
Kilo Contributor

Hi 

 

I tried this but it will show for all the incidents .I use the same code.Can you please tell what are the possibilities where i go wrong.

 

Thanks

Hi Mansi, 

 

I apologize for the late response. I haven't logged in to the community for a while. Do you mean that the message appears on any incident that you view even without actually clicking on it?

This script is designed to show a message on an incident record that was viewed by someone else in the last 15 minutes (i changed mine to 5 minutes). If you want it to show on particular incidents, you can try adding conditions to the business rule. For example, if you want it to show for Priority 1 incidents, add that as your condition on the 'When to run' tab in the business rule. 

Hope that helps!

 

Yeny