Making a GlideRecord insert/update appear to from SYSTEM or another user
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2012 10:43 AM
This is something small that's been annoying me for awhile; I have dozens of server-side background scripts in various modules that insert/write to their related tables via GlideRecord(). When they are ran; the update/insert is listed as the user who initiated the script. In most cases this is fine; but in a few cases I actually would like the script to appear to have ran as SYSTEM when it writes to fields such as work notes.
.sys_updated_by doesn't do what I need it to do, and .opened_by only works on inserts not updates.
Example:
Output:
11-14-12 10:23:29 AM Smith, John - Changed: Work Notes
some notes
What I want it to do:
11-14-12 10:23:29 AM SYSTEM - Changed: Work Notes
some notes
var gr = new GlideRecord('incident');
gr.get('number','INC00000');
gr.work_notes = 'some notes';
// gr.sys_updated_by = 'some user'; // Updates the sys_updated_by field; but not the activity / history
// gr.opened_by = 'some user'; // Only works on inserts
gr.update();
Even though John Smith initiated the script; I need the record to show that system ran it.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2012 07:25 AM
There MIGHT be a way to do this, but it would be a pretty fun little hack job. This is a limitation of the platform as most of the time the user that caused a script to run is what we want to record.
Can you explain what condition is causing this script to run? Scheduled Jobs support a "Run as" field that would allow you to get what you want here, but that might not be appropriate for your condition.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2012 10:12 AM
Well, I have multiple situations; but most of them are on business rules attached to the table to run on insert or update. In one example I am witting to a custom table and I want the same information written in work-notes on the Incident table; but I want it to appear as of "SYSTEM" wrote the update.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2012 12:04 PM
Where it's a business rule, we're going to continue to hit this limitation. If you have some sort of timed scripting, you could get SYSTEM with the Scheduled Job idea, but I'm betting that is not ideal or possible with your situations.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2012 07:23 PM
Try using this as a starting point. You may have to just figure out how to set the user to System.
function impersonate(){
var userEmail = email.from;
var ugr = new GlideRecord("sys_user");
ugr.addQuery("email", userEmail);
ugr.query();
if (ugr.next()) {
return gs.getSession().impersonate(ugr.sys_id);
}
return null;
}