Add a comment to an incident via REST API as a different user

tommylong
Kilo Explorer

Hello,

Apologies if this identical question has been asked before, I did some searching for a while before writing this but came up blank.

We want to be able to add comments to an incident via the REST API from a third-party system we control. The third-party system authenticates into ServiceNow with a single user setup especially for the system.

I know I could simply add comments to the incident directly but then these appear in the activity history as being from the user who was authenticated through the REST API. We instead want to be able to set the comments to appear as being from a different user (the id of which we know when we add the comment).

Currently we've attempted to work around this by manually updating the sys_history_set and adding to the sys_audit and sys_history_line tables but this solution smells bad and seems to sometimes cause issues with comments either not appearing in ServiceNow or being duplicated.

Any suggestions people might have would be greatly appreciated.

Thanks in advance

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

You can do this via scripted rest API where you impersonate the user prior to updating.  Here is an example function:

function updateTask(task, user, comments){
   new GlideImpersonate().impersonate(user);
   task.comments = comments;
   task.update();
}

This function expects the SysID of the user.

View solution in original post

4 REPLIES 4

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

You can do this via scripted rest API where you impersonate the user prior to updating.  Here is an example function:

function updateTask(task, user, comments){
   new GlideImpersonate().impersonate(user);
   task.comments = comments;
   task.update();
}

This function expects the SysID of the user.

tommylong
Kilo Explorer

Thanks for the help Michael, that's sorted things out nicely!

I do have two more quick queries that you (or someone else might be able to help with).

  1. I'm getting a list of existing comments on an incident by grabbing the relevant history set and then getting the history lines. Is there a better way of getting existing comments? My specific issue is that if you add a comment by API before the incident/change has been viewed in the ServiceNow API then there doesn't seem to have been a history set generated yet.
  2. If a user doesn't have a username (but has an actual name) then the title on the Activity shows as "Guest" instead of their name. This seems quite odd...

Thanks again

Glad that is working.  Regarding your additional questions:

1. You can use the getJournalEntry() function to get a certain number of entries.  See this document for an example.

2. The Guest user is there for this very reason.  If a user cannot be found then the Guest user is used to reflect the update since a user always needs to be associated to an update.

 

Please mark any post helpful or the correct answer to your question so others can benefit.

migueldele_n
Kilo Contributor
Hi Tommy,

This is another example:
session.onlineImpersonate("abel.tuter");
var current = GlideRecord('incident');
if (!gs.hasRole("itil")) {
  var u = gs.getUserID();
  var qc = current.addQuery("caller_id", u).addOrCondition("opened_by", u).addOrCondition("watch_list", "CONTAINS", u);
  gs.print("query restricted to user: " + u);
}
current.query();
while (current.next()) {
gs.print(current.number);
}
session.onlineUnimpersonate();