banner



Can Background Script Print Alert

We are experimenting with new training content delivery methods. This tutorial weblog post is one of those experiments. We are very interested in your feedback. Delight permit u.s. know what y'all recollect about this format and the content in the comments beneath.

Introduction

Years ago, when I first started working with the ServiceNow platform, I learned about Scripts - Background. Scripts - Background was this magical place in the platform where yous could run any server-side script. It became my testing ground for whatsoever server-side method I wanted to learn about or new script I wanted to exam considering I did not need to configure When to run logic around it similar a Business Rule. Running a script in Scripts - Background was equally easy every bit putting a script in the field and clicking the Run script button.

I will admit that for near scripts I need to run on an instance these days, I adopt other means to execute my scripts over Scripts - Background. Syntax highlighting and checking are just as well helpful for me to pass up if I have admission to them. The value of Scripts - Background lies in its simplicity. No need to create a record. Just write or paste in a script and run information technology.

NOTE: Scripts - Background should be used very carefully and but in non-production environments. Costless-form JavaScript can negatively impact data and organisation operation.

In this post, y'all larn how to work with Scripts - Background to add some demo data. Y'all also larn how to work with the rollback capability to remove the demo information.

Running Scripts - Background

To open Scripts - Background, use the Application Navigator to open Organisation Definition > Scripts - Background.

The Scripts - Background UI has a field for the script and options to set scope, rollback, and cancel long running scripts.

  • Run Script: Push to run the script.
  • Scope selector: Option list of application scopes. Default to the current scope of the chief ServiceNow window.
  • Record for rollback?: Captures database updates and insertions to rollback when possible.
  • Execute in sandbox?: Runs the script with restricted rights. Refer to the documentation on The script sandbox property for details on what methods are restricted. Executing in sandbox tin can prevent potentially harmful methods from running, but could prevent the script from its intended purpose. This case shows the letters logged when insert methods are run in sandbox.

    A script that tries to insert records when run in sandbox returns Security restricted messages.

  • Cancel after 4 hours: Cancel long running scripts after iv hours. Proceed this selected unless you are certain your script needs more than time. Fifty-fifty then, you lot should consider breaking up the work into smaller chunks.

After the script executes, examine the results.

The Scripts - Background output has three sections: script results, rollback and database updates, and script output. Each section is separated by a horizontal rule.

  • Script results: The time the script took to run and the scope in which the script ran.
  • Rollback and database updates: Access to rollback (if selected) and database updates fabricated by the script.
  • Script output: Logging generated by the script. Add together messages with gs.log (global) or gs.info (scoped app) statements.

Programmer TIP: If the script does not execute as expected and you need to suit the script and run information technology over again, click the browser's back button to return to the Scripts - Background window with the script yous ran.

Add Sample Users

In this section of the tutorial, you lot run a script in Scripts - Background to create some sample user records.

  1. Use the Application Navigator to open Organization Definition > Scripts - Background.
  2. Gear up the in scope pick list to global.
  3. Copy this script and paste it into the Run Script (JavaScript executed on server) field.

                                  // Set the number of users to create           var numUsers = three;          // Define a loop to create users           while(numUsers > 0){                              // Create a User record with numUsers as an index             var grUser = new GlideRecord('sys_user');             grUser.user_name = "mabel" + numUsers;             grUser.first_name = "Mabel";             grUser.last_name = "Gerkowski " + numUsers;             grUser.insert();              // Log user creation - use gs.info in scoped applications             gs.log("User created: " + grUser.user_name);              // Decrease numUsers by one             numUsers--;         }                          
  4. Click the Run script button.

  5. View the results of the script.

    1. How many records were added?
    2. How long did the script take to run?
    3. What data was logged?
  6. Verify user creation.

    1. Apply the Awarding Navigator to open User Administration > Users.
    2. Discover users with a User ID that contains mabel. Yous should accept 3 Mabel Gerkowskis.

Rollback

If the Record for rollback? choice is selected, the database actions performed by the script can be rolled dorsum.

Admission the Script Execution History from the Scripts - Background results by clicking available here in the Script execution history and recovery bachelor here message.

To open the history subsequently navigating away from the results, use the Application Navigator to open Rollback & Recovery > Script Execution History. Open the script execution to rollback and click the Rollback Script Execution… Related Link. Confirm the rollback.

Note: Script Execution History is only stored for seven days. After that, the execution history is no longer bachelor to view or roll back.

  1. Roll dorsum user creation.
    1. Use the Application Navigator to open Rollback & Recovery > Script Execution History.
    2. Click the timestamp in the Started column for the script execution to rollback.
    3. Click the Rollback Script Execution… Related Link.
    4. In the Rollback unabridged performance? dialog, type yes to confirm the rollback and click the OK button.
  2. Verify the rollback.
    1. Apply the Application Navigator to open User Administration > Users.
    2. Find users with a User ID that contains mabel. You should not have whatever *Mabel Gerkowski*s.

Did you do the easily-on exercise in this web log? Click here to let us know!

Challenge - NeedIt

If you take taken training on the Developer Portal, you should have the NeedIt application and table. Use what you lot have learned nearly Scripts - Background here to create some new NeedIt records. Log details for the records created. If you do not yet take NeedIt, employ the instructions in Do: Prepare Case for Client-side Scripting to configure your instance with the NeedIt awarding.

Considerations

  • What fields do you need to supply values for?
  • How do you log details for the records created?
  • How do you run the script in the NeedIt scope?

Tips and Endmost Thoughts

Scripts - Background is a simple, merely powerful tool to run JavaScript in a ServiceNow instance. While Scripts - Background tin be used for quick scripting logic tests or for quick commands in a exam surroundings, I recommend using an On Demand Scheduled Script Executions (SSE). Scheduled Script Executions accept these benefits:

  • The Script field includes syntax coloring and code syntax checking.
  • SSEs can be captured in an application.
  • The script can be edited without relying on the browser's dorsum button or saving in another file.
  • SSEs tin can be executed on demand.

On the downside: rollback is not bachelor for these script types. Regardless of how you lot execute a script on the ServiceNow platform, go on with caution.

Claiming Respond

Here is a possible answer for the Challenge. It needs to be run in scope x_58872_needit.

                      // Set the number of NeedIt records to create       var numRecords = 5;      // Define a loop to create NeedIt records       while(numRecords > 0){                  // Create a NeedIt record with numRecords as an alphabetize         // Fields may vary depending on which training y'all have completed         var newNI = new GlideRecord('x_58872_needit_needit');         newNI.u_requested_for = "Fred Luddy";         newNI.u_requested_for_email = "fred.luddy@example.com";         newNI.u_when_needed = gs.daysAgo(-numRecords - 2);         newNI.short_description = "NeedIt generated by Scripts - Background";         newNI.insert();          // Log NeedIt record creation         gs.info("NI created: " + newNI.number + " - " + newNI.short_description);          // Subtract numRecords by 1         numRecords--;     }                  

Can Background Script Print Alert,

Source: https://developer.servicenow.com/blog.do?p=/post/training-scriptsbg/

Posted by: bankswrouse77.blogspot.com

0 Response to "Can Background Script Print Alert"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel