- 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-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 12:03 AM
Thank you. When i use this script after adding in my table name, i get these errors -
looks like this relates to some permission issue ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 12:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 12:33 AM
same scope. Both are in my application that i created.