The CreatorCon Call for Content is officially open! Get started here.

Audit history shows User is (empty) User ID is system and User Name is System

David Dietrich1
Tera Expert

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?

1 ACCEPTED SOLUTION

Kristen Ankeny
Kilo Sage

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

View solution in original post

3 REPLIES 3

Kristen Ankeny
Kilo Sage

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

I'm not sure why you don't recommend it, but I was able to get it working. Here is what I did:

  1. Disabled autoSysFields and setWorkflow
  2. Set the sys_updated_by, sys_updated_on, and sys_mod_count fields
  3. Inserted a new record into the sys_audit table
  4. Queried for any existing sys_history_set records
  5. 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.

Community Alums
Not applicable

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