- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2024 03:17 PM
I am updating a record using a Script Action that calls a function inside a Script Include. Everything is inside a scoped app. Each time the Script Action is processed, the field is updated successfully, but the Audit History shows the update was made by User Name "System", User ID "system" and an empty "User" field. How do I get the system to assign values to these Audit History fields that match the user identified in the "Created By" field of the sysevent record that triggered the update?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2024 03:41 PM
It is possible, but I wouldn't recommend it. It reflects system because it is being done asynchronously. If you do want to manually set that to a particular value, then you can follow this KB article from ServiceNow - but keep in mind that you would need to manually set any sys_ field that the function causes ServiceNow to not set. https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0859763
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2024 03:41 PM
It is possible, but I wouldn't recommend it. It reflects system because it is being done asynchronously. If you do want to manually set that to a particular value, then you can follow this KB article from ServiceNow - but keep in mind that you would need to manually set any sys_ field that the function causes ServiceNow to not set. https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0859763
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2024 03:32 PM - edited ‎07-10-2024 03:33 PM
I'm not sure why you don't recommend it, but I was able to get it working. Here is what I did:
- Disabled autoSysFields and setWorkflow
- Set the sys_updated_by, sys_updated_on, and sys_mod_count fields
- Inserted a new record into the sys_audit table
- Queried for any existing sys_history_set records
- Inserted a new sys_history_line record if a sys_history set was found
After this code was written, modifications made by async events were audited exactly like changes made synchronously. This provides me with a clear audit trail regardless of whether the change was async or not.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2024 05:43 PM
Hi @David Dietrich1 ,
As Kristen said , this is doable. The only concern is if the script is being executed in the background then by default it will run as system admin hence the updates wont be in a user's name-
If you want to update based on the session then you can follow the below logic-
var userSysId = current.sys_created_by;
var gr = new GlideRecord('your_table');
if (gr.get(current.sys_target)) {
// Disable auto system fields and workflow
gr.autoSysFields(false);
gr.setWorkflow(false);
// Manually set the system fields
gr.sys_created_by = userSysId;
gr.sys_updated_by = userSysId;
gr.update();
// Re-enable auto system fields and workflow
gr.autoSysFields(true);
gr.setWorkflow(true);
Include the above snippet to achieve it.
Note: Here we are overriding the default behavior of the system which can have impact on the audit or it might lead to some performance impact as we are adding extra layer of updates to the records.
If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!
Thanks & Regards,
Sanjay Kumar