How to update the worknotes form particular user through script include

varshinin
Tera Contributor

var WorknotesUpdater = Class.create();
WorknotesUpdater.prototype = {
initialize: function() {
},

// Function to update worknotes for an incident
updateWorknotes: function(currentIncident, additionalNotes) {
try {
if (currentIncident && additionalNotes) {
var workNotes = currentIncident.work_notes.getJournalEntry(1); // Get existing worknotes
var newWorkNotes = additionalNotes + '\n\n' + workNotes; // Append new notes
currentIncident.work_notes = newWorkNotes; // Update the worknotes field
currentIncident.update(); // Save the incident
return true;
}
} catch (ex) {
gs.error("Error updating worknotes: " + ex);
}
return false;
},

type: 'WorknotesUpdater'
};

 

 

In this script , I'm updating the worknotes but i wanted to update through particular user 

Please help me with your suggestions and solutions.

 

Thanks 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@varshinin You can use the GlideImpersonate API to impersonate as a user in the script and post a worknote on their behalf.

var user = gs.getUserDisplayName();
gs.print ("The current user display name is: " + user);

var impUser = new GlideImpersonate();
impUser.impersonate("62826bf03710200044e0bfc8bcbe5df1");
var user = gs.getUserDisplayName();
gs.print ("The impersonated user display name is: " + user);

 

For more information, please refer to the docs here https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/GlideImpersonateAPI#GI-c...

 

Hope this helps.

View solution in original post

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@varshinin You can use the GlideImpersonate API to impersonate as a user in the script and post a worknote on their behalf.

var user = gs.getUserDisplayName();
gs.print ("The current user display name is: " + user);

var impUser = new GlideImpersonate();
impUser.impersonate("62826bf03710200044e0bfc8bcbe5df1");
var user = gs.getUserDisplayName();
gs.print ("The impersonated user display name is: " + user);

 

For more information, please refer to the docs here https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/GlideImpersonateAPI#GI-c...

 

Hope this helps.

@Sandeep Rajput  Thanks for the solution.

 it is working now 

@Sandeep Rajput   this script works fine in global ,

But  GlideImpersonate is not allowed in scoped application . please suggest how i can achieve the same  on scoped application.

@varshinin Create a script include in Global scope, create updateWorknotes function inside it and put the impersonation code there. Call this script include inside your scoped application by using global.<ScriptIncludeName> specifier.

 

Hope this helps.