How to check who has viewed the Record.

devservicenow k
Tera Contributor
 
1 ACCEPTED SOLUTION

Pavankumar_1
Mega Patron

Hi @devservicenow k ,

 

Fields:

u_viewedrecord -reference field type on Case/Incident table,

u_username - User reference field type on User table,

u_usertime - Date/Time filed type

 

No need 2 reference fields on case table. u_parent, u_viewedrecord. Use any one of them.

 

Create display business rule on case table. but I tried on Incident table.

Script: 

 

(function executeRule(current, previous /*null when async*/ ) {

var graud = new GlideRecord("u_audit"); //audit table name
graud.initialize();
//graud.u_viewedrecord = current.sys_id; // current.sys_id and getunique value both will work as same.
graud.u_viewedrecord = current.getUniqueValue();
graud.u_username = gs.getUserID(); // to get the user id
graud.u_usertime = new GlideDateTime(); // to get the time that viewed by user
graud.insert();

})(current, previous);

 

Give all your field names that you created on audit  table it will work.

 

Relationship Query:

 

(function refineQuery(current, parent) {

// Add your code here, such as current.addQuery(field, value);
current.addQuery('u_viewedrecord', parent.sys_id);

})(current, parent);

 

Screenshot (197).png

 

Audit records LIST:

Screenshot (198).png

 

Audit Relationship: It will display only parent incident/case related records.

 

Screenshot (200).png

 

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

18 REPLIES 18

Hi,

Make sure your reference table should be your case table. Hence it will update the Viewed record as case record. It is working fine for me.

 

For reference -

scripts -
var record = new GlideRecord("my_table_name");
record.initialize();
record.u_viewer = gs.getUserID();
record.u_last_viewed_time = new GlideDateTime().getLocalTime();
record.u_viewed_record = current.getUniqueValue();
record.insert();

 

Screenshot 2022-10-07 at 9.45.11 AM.png

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Have you written business rules for Before /after?

 

Hi @devservicenow k,

I have written a Display Business rule on Case table. nor before/after.

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Mahendra RC
Mega Sage

Hello @devservicenow k 

Please check with below script:

var auditRecordGR = new GlideRecord("u_audit");
auditRecordGR.initialize();
auditRecordGR.u_username = gs.getUserID();
var gdt = new GlideDateTime();
auditRecordGR.u_usertime.setDateNumericValue(gdt.getNumericValue());
auditRecordGR.u_viewedrecord = current.getUniqueValue();
auditRecordGR.u_parent = current.getUniqueValue();
auditRecordGR.insert();

viewed record field is not storing the value.