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

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

Yes, that is correct. We want to track the audit/changes to the fields in the form, in the activity stream. 

 

I've enabled audit for the table but that doesnt seem to work. I tried the business rule approach as well as per the previous comment and that gives me a permission related error. I've shown this in my video - 

 

 

There is no need to use a Business Rule for your requirement, can you confirm the following:

 

  • Did you add the activities formatter in the form? (i.e. can you see the 'Activities' section in your form, if unsure please provide a screenshot of your record)
  • Where did you enable the audit? 
  • When you say it 'doesn't seem to work' after enabling audit, what made you come to this conclusion?

Thanks

1. Yes, i think i have added the activities formatter in my form - 

MarkJ1_0-1708424596942.png

 

2. I enabled the audit in Dictionary under System Definition - 

MarkJ1_1-1708424658172.png

 

 

3. The reason i say it doesnt seem to work is because, when i update the value of a field ex - "Col1" in my table, it doesnt show as the field being updated in my Activity Stream - 

MarkJ1_2-1708424771773.png

 

Hey @Mark J1,

 

Looks like you marked the question as resolved, so does that mean you managed to get it working?

If not, can you confirm the following:

  • Verify the history/audit is captured via going History > List (right click on header of the form) 
    JamesChun_0-1708456657197.png

     

    Ensure the list of history captures all the changes of the values.

 

  • Verify the field is included in the Filter Activity. You can do this by selecting the filter icon, and then selecting 'Configure available fields'. From the slushbucket, select the fields that you want to show in the activity stream.

JamesChun_1-1708456773037.png

 

Thanks