Skip to page contentSkip to chat
ServiceNow support
    • Community
      Ask questions, give advice, and connect with fellow ServiceNow professionals.
      Developer
      Build, test, and deploy applications
      Documentation
      Find detailed information about ServiceNow products, apps, features, and releases.
      Impact
      Accelerate ROI and amplify your expertise.
      Learning
      Build skills with instructor-led and online training.
      Partner
      Grow your business with promotions, news, and marketing tools
      ServiceNow
      Learn about ServiceNow products & solutions.
      Store
      Download certified apps and integrations that complement ServiceNow.
      Support
      Manage your instances, access self-help, and get technical support.
How to excute a script as system - Support and Troubleshooting
  • >
  • Knowledge Base
  • >
  • Support and Troubleshooting (Knowledge Base)
  • >
  • How to excute a script as system
KB0820233

How to excute a script as system


9840 Views Last updated : Apr 8, 2024 public Copy Permalink English (Original)
  • English (Original)
  • Japanese
KB Summary by Now Assist

Summary

During your troubleshooting, you may want to execute a piece of code as the system user. The schedule worker threads are the system user.

  • Configure the code below with whatever script in the "script" variable for the system to execute
  • Don't forget to escape special characters
  • In the addSecond method, you can define when the script should execute, set to one second to execute immediately 
  • Then run the code below in /sys.scripts.do.
  • This will create a [sys_trigger] record for the scheduler worker to pick up.
  • Define variable gr with what you'll want "current" to be in the script defined in the "script" variable.

Instructions


//get the gr of whatever record you want current to be on the script 
var gr = new GlideRecord("sc_req_item");
gr.get("aeed229047801200e0ef563dbb9a71c2");     

//execute one second after running this script
var gdt = new GlideDateTime();
gdt. addSeconds(1)
gs.info(gdt);

//define the script you want to run, in this example it's the stack trace, and don't forget to escape special charectors
var script = "gs.log('***** DEBUG - op:'+ current.operation() + ', sess:' + gs.getSessionID() + ', time:' + new Date().getTime() + ', sys_id:' + current.sys_id + ' - \\n' + GlideLog.getStackTrace(new Packages.java.lang.Throwable()), 'Stacktrace Debug');";

//create the sys_trigger record to be executed by the schedule worker thread
var sched = new ScheduleOnce();
sched.script = script;
sched.setTime(gdt);
sched.setDocument(gr);
sched.setLabel("run this as system");
sched.schedule();

The world works with ServiceNow.

Sign in for more! There's more content available only to authenticated users Sign in for more!
Did this KB article help you?
Did this KB article help you?

How would you rate your Now Support digital experience?

*

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

What can we improve? Please select all that apply.

What are we doing well? Please select all that apply.

Tell us more

*

Do you expect a response from this feedback?

  • Terms and conditions
  • Privacy statement
  • GDPR
  • Cookie policy
  • © 2025 ServiceNow. All rights reserved.