How to set 'updated by' in a script programmatically

Katie A
Mega Guru

I have a script that runs on the sys_email table.

The script queries for the the contents of a sent Client Email message and writes the message body back to the task comments.

Ths works for the most part, however the 'Updated By' is always being set to the 'System'. I want to set it to the person who created the email message.

I have access to that with current.sys_created_by.

I tried setting sys_updated_by in the scipt. However this does not work.

record.sys_updated_by = current.sys_created_by;

This is the full script. This is a Business Rule that runs on the Email table when a new email is inserted. This is called when a new Client Message is sent from a task record.

/* Copy messages sent from email client back to original record */
emailClientMessage();

function emailClientMessage() {

  // Copy email message from email client back to original record
  var record = new GlideRecord(current.target_table);
  //Grab the email message that matches the record
  record.addQuery('sys_id', current.instance);
  record.query();
  if (record.next()) {

    // Update comment with Email client message
    record.comments = "[code]" + current.body + "<br />Created by: " + current.sys_created_by + "[/code]";

    // Set updated by as current user 
    record.sys_updated_by = current.sys_created_by; //THIS DOES NOT WORK! Updated By is still showing up as 'System'

    // Set updated datetime
    record.sys_updated_on = gs.nowDateTime();
    record.update();
  }
}
1 ACCEPTED SOLUTION

Dominik Simunek
Tera Guru

It would be best to find a way how to "hook" the update earlier (once the client message is submitted) still in the user session. Once the business rule works in a system session as email is being processed, it might be hard to change all needed values - it is not possibly only sys_updated_by field value, but I guess also Activities would be showing wrong user as originator of the comments.

These sys fields are maintained by the system automatically. You could try function call like "record.autoSysFields(false)" which will turn it off but it might be that it turns off more things than you wish (in my case when I tried to overwrite sys_created_by, audit was not working properly then). So please try it but review heavily what are the impacts on your audit (what you see in Activities and what you see in History > List context menu in the audit). But I believe this won't fix the possible issue with Activities that would be still showing System.

Only other idea would be impersonation in the script but this is something I would really try to avoid as you never know what could be the side effects.

View solution in original post

4 REPLIES 4

Abhishek77
ServiceNow Employee
ServiceNow Employee

Hi can you try using this

 record.sys_updated_by =current.sys_created_by.getDisplayValue();

Thanks

Thank you, but this still does not solve the issue.

I think this is not working because the Journal entry has 'System' as the user that created the comment journal entry.

Table: sys_journal_field

One way of doing this might be to perhaps run a separate query on the Journal table and update the 'created by' of the journal entry for the comment however this is a bit of an involved workaround.

Dominik Simunek
Tera Guru

It would be best to find a way how to "hook" the update earlier (once the client message is submitted) still in the user session. Once the business rule works in a system session as email is being processed, it might be hard to change all needed values - it is not possibly only sys_updated_by field value, but I guess also Activities would be showing wrong user as originator of the comments.

These sys fields are maintained by the system automatically. You could try function call like "record.autoSysFields(false)" which will turn it off but it might be that it turns off more things than you wish (in my case when I tried to overwrite sys_created_by, audit was not working properly then). So please try it but review heavily what are the impacts on your audit (what you see in Activities and what you see in History > List context menu in the audit). But I believe this won't fix the possible issue with Activities that would be still showing System.

Only other idea would be impersonation in the script but this is something I would really try to avoid as you never know what could be the side effects.

Thank you I ended up disabling the autoSysFields as suggested. The journal entry of the Comment still shows updated by System, but the destination record shows updated by the user that sent the message from the Email Client. This is close enough to what we need. Thanks!

 

/* Copy messages sent from email client back to original record */
emailClientMessage();

function emailClientMessage() {

  // Copy email message from email client back to original record
  var record = new GlideRecord(current.target_table);
  //Grab the email message that matches the record
  record.addQuery('sys_id', current.instance);
  record.setLimit(1);
  record.query();
  if (record.next()) {

  // Disable automatic system values on the destination record
  // Required to set updated by as user who sent quick message
  record.autoSysFields(false);

  // Email client message copy as a comment requires keywords
  record.comments = "[code]" + current.body + "<br />Created by: " + current.sys_created_by + "[/code]";
  
  // Set updated by as current user
  record.sys_updated_by = current.sys_created_by;

  // Set updated datetime
  record.sys_updated_on = gs.nowDateTime();
  record.update();
  }
}