- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 11:13 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 11:28 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 01:30 AM
Hi @Mark J1,
Did you mean you wanted to track the 'audit' or the changes of fields in the Activity stream?
For an example:
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'
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 01:30 AM
Hi @Mark J1,
Did you mean you wanted to track the 'audit' or the changes of fields in the Activity stream?
For an example:
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'
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 02:01 AM
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 -
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 02:12 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 02:26 AM
1. Yes, i think i have added the activities formatter in my form -
2. I enabled the audit in Dictionary under System Definition -
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 -
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 11:20 AM
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)
- 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.
Thanks