Beruflich Dokumente
Kultur Dokumente
John Roberts
Application Developer ServiceNow
Jerrod Bennett
Senior Technical Consultant ServiceNow
Lab Agenda
Overview Scripting & Debugging Concepts Debugging Tools Client and Server Debugging Examples Extending Script Includes AJAX Debugging Basic Jelly Debugging Display Business Rules
Overview
Disclaimer
Advanced topics and quick pace Assumes some scripting experience and knowledge of Service-now.com concepts
Objectives
Improve scripting and troubleshooting abilities Learn some new tricks
Overview
Setup
Lab instance Script source - http://wiki.service-now.com/index.php? title=Script_Lab
Optional
Install Firefox and Firebug
Case SenSitiviTy
All commands, functions, variables,
Variable Scope
Session global risk of duplication Classes limited to class Function limited to function, best protection
var myName = john; function doThis() { var myName = roberts; }
Value
Debugging Concepts
Debugging Tools
Application
System Diagnostics / Session Debug modules Information output options
Browser
Developer Tools Add-ons such as Firebug
Result
No comment or info message
Troubleshooting
Did the rule execute
Open another existing incident and set incident state to closed, save Scroll down to see debug output and notice the following
Skipping 'k11 Exercise 1 - Incident Close Comment' on table incident; condition not satisfied: current.incident_state.changesTo("Closed")
From the debug output click on the business rule name to open it
Lets remove the condition and find out what the field value is changing to
Delete the condition so the rule will execute Comment out the comment set statement so it doesnt run on all incidents Add some debugging statements
//add comment when closing //current.comments = "Closed by " + gs.getUserName() + " at " + gs.nowDateTime(); //gs.addInfoMessage("Close comment added"); // Debug info gs.addInfoMessage("(InfoMessage) Incident state = " + current.incident_state); //output to app gs.log("(Log) Incident state = " + current.incident_state); //output to system log gs.print("(debug print) Incident state = " + current.incident_state); //output to debug console
Looks like we had the wrong condition value for closed incident state Now we can correct the condition and un-comment the business rule
Debugging
Enable the Debug Business Rule (Details) to add detail to the debug output Create another hardware incident, save View debug output
==> 'k11 Exercise 2 - Incident Priority Change' on table incident priority: 4 => 1 <== 'k11 Exercise 2 - Incident Priority Change' on table incident
Try it
Create a incident with software category Add a short description and save Notice we are getting one message at the top of the form
Notice we dont process after line 5, now we need to find out why First disable the rhino debugger by running the Disable All module
Rhino debugger can interfere or suppress error output
Now lets see how try catch block can help us From the debug output click on the business rule to open it
Unhandled errors can prevent script functionality and even other scripts from running Catch statements guarantee that you control what happens when an error occurs, especially important if you need to clean up or revert something
Try it
Lets review options for client-side debug output Enable client javascript debugger console by clicking the green bug in the top banner Create a new incident, change category to request Script will log to debug console and change urgency to low Review debug console
Close executor Try to save the form again with a blank description You just temporarily bypassed the mandatory check on the form Use it wisely and keep it a secret
We could convert the array list to an array whenever we need it but it would be nice to change the getTables method to return an array Modifying an out-of-box script include is never recommended
Theres another script include we can use to convert java objects to javascript, thats j2js
Notice that we didnt need an initialize of getTables method in our class, we inherited those from the parent (this) object
Exercise 8 AJAX
Scenario: Use AJAX to lookup the number of available development laptops when someone attempts to request one through the catalog Components
Catalog client script to submit AJAX request to server AJAX processor script include to perform the check and return the result to client Catalog client script to process the AJAX response and inform the user
Try it
Go to the catalog and load the Development Laptop item Script runs on load to check inventory and alerts user with status from server
Exercise 8 AJAX
Review scripts
Review catalog client script k11 Capacity Check Review script include k11 CatalogCapacityCheck
Debugging options
Log statements in script include Firebug to inspect payload to/from server Manual client script testing from javascript executor
Exercise 8 AJAX
Try it
From the development laptop catalog item page start the javascript executor <Shift><Ctrl><Alt><j> Run the following script to make an AJAX request
var gajax = new GlideAjax("CapacityCheckAjax"); gajax.addParam("sysparm_name", "checkDevCapacity"); gajax.getXML(ajaxResponse); function ajaxResponse(serverResponse) { alert(serverResponse.responseText); }
Exercise 9 Jelly
Scenario: In a jelly script such as a UI Page, I would like to know which jelly variables are available and what the values are Options
For known variables you can write values to HTML output Use breakpoint tag to view all available variables in a given phase
Jelly in 30 seconds
XML files executed on server and output as HTML to client Evaluated in two phases, {phase 1} is cached (content), [phase 2] for data Nested structures with variables typically being passed down (UI Page calls a UI Macro)
Exercise 9 Jelly
Try it
Review UI page k11_test Notice the j:set and j2:set lines setting values for jvar_p1_test and jvar_p2_test Review UI macro k11_macro_test called from the UI page Notice the references to the variables defined in the j:set and j2:set lines in the UI page Enable Debug Log so we can view of out of the macro breakpoints Navigate browser to /k11_test.do Examine the debug information Notice how our jvar_p1_test and jvar_p2_test only have value in their respective phases
Display business rules are processed on the server before the record data is passed to the client The g_scratchpad object is available for setting values in the rule and getting values on the client Display rules can also change field data, similar to calculated fields since they display the new value but its not written to the database until you save the record
Review the client script k11 Exercise 10 - notify on old problems on problem table
function onLoad() { //get data from display rule scratchpad var createdBy = g_scratchpad.created_by; var daysOld = g_scratchpad.days_old; if (parseInt(daysOld) > 30) { alert("This is an old record created by " + createdBy); } }
Q&A
Additional Resources
SlightlyLoony blog (Service-now.com developer)
http://community.service-now.com/blog/slightlyloony
JavaScript book
JavaScript The Definitive Guide (OReilly)
Firebug
http://getfirebug.com/
SESSION
Text us at 22333 with 19L04 A text 19L04 B text
E VA L U AT I O N
Complete the session evaluation on the Knowledge11 Portal
take survey
19L04 C text
Case is not important
space is important