Creating custom script generator on ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2016 03:43 AM
Hi guys,
Its been a while since I posted on the community. Anyway, I have an idea on which I will create an onchange generator of the scripts written in a text box.
I am using a macro type of variable with div tags included. The code is like this:
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<h3>Live Display</h3>
<br></br>
<div id = "div_id">
</div>
<script>
<div id = "js_script">
</div>
</script>
</j:jelly>
The problem is that I can put html tags and generate them successfully in my catalog but I cannot assign functions for its buttons. See Screenshot of the catalog:
The html tags work but the function script doesn't work. So sad.
Here are my onChange catalog Client scripts for each variables:
for Script Variable:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var getDiv = document.getElementById("div_id").innerHTML = newValue;
}
for Function Script Variable:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var js_script_value = document.getElementById("js_script").innerHTML = newValue;
}
Does my plan doable?
Thanks guys,
a.c.manlangit
serviceNow Developer - Philippines

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2016 06:38 AM
That sure looks like some nasty DOM manipulation... not to sure wether I like it or not
Anyway, when assigning your script you will probably need to add some <script></script> tags to it. But still, this might not work, as I am not sure wether the browser will understand your on-the-fly defining a new function to be called.
I would rather think you will need some generic function already in the UI macro which would then need to assign itself to any element dynamically added (e.g. button). Code would than need to run eval on your js_script function script.
Just some thoughts....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2016 07:08 PM
Hi,
I thought about this yesterday and earlier and I think this will not work according to what I am planning to do. hehehe yes its a dom but I think its okay since I am only using my own objects.
Anyway, thank you for your thoughts

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2016 07:20 PM
The issue is probably the <div> tag within the <script> tag.
Try changing this
<script>
<div id = "js_script">
</div>
</script>
to this:
<script>
</script>
If that doesn't work, then you might need to find a way to have the JavaScript populate into the Macro as it loads. All examples of "Execute Now" buttons in ServiceNow cause the page to reload.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2016 08:30 PM
Hi Geoffrey,
Thanks! But I can only modify the script once and then I need to refresh the page to change its functionality.
But anyway, thank you for your help,