Quantcast
Channel: SNBlog – by Logicalis SMC
Viewing all articles
Browse latest Browse all 89

Dynamic Load Server-side Javascript

$
0
0

When creating custom solution in ServiceNow it is important to clarify the technical requirements of its functionality. After clarification it is good practice to check if the required functionality already exists in ServiceNow, either an out of the box or a custom created solution.
The needed functionality can reside in script includes or UI scripts.
It is easy to make use of these functions when you create server side script like business rules and access controls. This can be done using js.include(‘My Script.js’); in the beginning of the script, replacing My Script with your own script name.
There is no standard way of loading a server side function from a client side script like UI Actions, UI Policies or Client Scripts.
The following can be done to make this possible:
First create your server side function if it doesn’t already exist. See example in the next screenshot.

sample-script1

The example function “MyFunction” will pop-up a confirmation box with the option to select Ok or Cancel button. Feel free to select or create the function that you need instead of this one. Initialize is a mandatory function and make sure that the name of your UI Script or Script Include is the same name as the variable on the first line. If you are testing in Firebug, then make sure that you refresh the page after you have edited your script otherwise it might not find your function.

More info on this:
- http://wiki.servicenow.com/index.php?title=Script_Includes
- http://wiki.servicenow.com/index.php?title=UI_Scripts

To call this function you can use the following code:

sample-script2

In the function showConfirm():

The following is added to the header (getElementByTagName) on the current page with the script tag and the content type (js.type) which is used so that the browser can recognize that the content is Javascript. The source file (js.src) is a reference to the Confirmation.jsdbx which is my UI Script example. In case of UI Script you should use the jsdbx extension. For Script Includes use the .js extension. Name should be the same as the name of the script. appendChild finally adds the script tag to the header.
You can test this in a UI Action or Client Script but it is faster to you use Firebug (which also works client side on your browser) to do on the fly debugging and testing of your code.

sample-script3

Just click the Console tab in Firebug and past your code in the right window under the looking glass and hit Run to test your script.
In the log in the left windows you can see errors or warnings messages if your code is not working.
If everything is working fine the same function as above can be used in your client side scripts in service-now as well.
This might also be a good way to test server side functionality first on Firebug before implementing it in the service know environment.

If you want more information on this you can mail me on hidden[at]hidden .


Viewing all articles
Browse latest Browse all 89

Trending Articles