Script to update activity stream of the current record

Mark J1
Tera Contributor

Hello,

 

We would like to post some updates to the activity stream when certain updates happen on the current record/form.

 

Can i please get an example script to add in my business rule that will post updates to activity stream when the table is updated ?

 

Thanks

2 ACCEPTED SOLUTIONS

Harsh_Deep
Giga Sage
Giga Sage

Hello @Mark J1 

 

User this script in after BR-

var currentRecord = new GlideRecord(current.tableName); // Replace 'tableName' with the actual table name of your record
if (currentRecord.get(current.sys_id)) {
  var activity = new GlideRecord('sys_journal_field');
  activity.initialize();
  activity.setValue('element_id', currentRecord.sys_id);
  activity.setValue('element_table', current.tableName);
  // Set the user who performed the action (replace 'user_id' with the actual user's sys_id) or you can use gs.getUserID()
  activity.setValue('user', 'user_id');
  // Set the action type (e.g., 'comment', 'work_notes', etc.)
  activity.setValue('action', 'comment'); // You can change this to the appropriate action type
  // Set the comment or message you want to add to the activity stream
  activity.setValue('value', 'This is a new comment for the activity stream.');
  var activityID = activity.insert();
  current.comments = 'Updated activity stream. New comment added by script. [journal_field:' + activityID + ']';
  current.update();
  gs.info('Activity stream updated successfully.');
} else {
  gs.error('Unable to find the current record.');
}


 Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

View solution in original post

James Chun
Kilo Patron

Hi @Mark J1,

 

Did you mean you wanted to track the 'audit' or the changes of fields in the Activity stream?

For an example:

JamesChun_0-1708421264382.png

 

If so, you don't have to write any script. Make sure the table has the audit turned on, ensure your field is included in the 'Configure available fields'

JamesChun_1-1708421337626.png

 

And FYI, do NOT use current.update() in a Business Rule, you may end up with recursive calls.

See the link for detail - https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0715782

 

Thanks

View solution in original post

9 REPLIES 9

Harsh_Deep
Giga Sage
Giga Sage

Hello @Mark J1 

 

User this script in after BR-

var currentRecord = new GlideRecord(current.tableName); // Replace 'tableName' with the actual table name of your record
if (currentRecord.get(current.sys_id)) {
  var activity = new GlideRecord('sys_journal_field');
  activity.initialize();
  activity.setValue('element_id', currentRecord.sys_id);
  activity.setValue('element_table', current.tableName);
  // Set the user who performed the action (replace 'user_id' with the actual user's sys_id) or you can use gs.getUserID()
  activity.setValue('user', 'user_id');
  // Set the action type (e.g., 'comment', 'work_notes', etc.)
  activity.setValue('action', 'comment'); // You can change this to the appropriate action type
  // Set the comment or message you want to add to the activity stream
  activity.setValue('value', 'This is a new comment for the activity stream.');
  var activityID = activity.insert();
  current.comments = 'Updated activity stream. New comment added by script. [journal_field:' + activityID + ']';
  current.update();
  gs.info('Activity stream updated successfully.');
} else {
  gs.error('Unable to find the current record.');
}


 Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

Thank you. When i use this script after adding in my table name, i get these errors - 

 

MarkJ1_0-1708416171502.png

looks like this relates to some permission issue ?

Hello @Mark J1 

 

In which scope you have written BR and where you have Table.

same scope. Both are in my application that i created.