Solved: Using JavaScript in a template - ServiceNow Community

Using JavaScript in a template

Bradley Ross
Tera Guru

I'm interested in making my templates a little bit smarter with a bit of JavaScript. I'm able to set the Caller to myself using this snippet for the value of Caller in the template:

javascript:gs.user_id()

I then tried to set the Short Description using this snippet:

javascript:g_form.getValue('parent.short_description') + ": add some text here"

That resulted in blanking out the short description when the template was applied. I opened the JavaScript executor in Chrome (ctrl-shift-j) while on the record and running:

alert(g_form.getValue('parent.short_description') + ": add some text here")

That gives me a popup alert box with the text I was hoping would populate in the Short Description field. So the script itself appears to be valid and working as I'd expect. But I just can't get it to run when I put it in for the value of a field in a template.

I'm trying this on a developer instance running Geneva.

1 ACCEPTED SOLUTION

Jace Benson
Mega Sage

Bradley,



The templates apply the javascript server side, not client side.   That's why the first call work, gs is available only server side.


With that being said, you can invoke a script include to do what you want.   You would have to first create the script include and it would probably look like;


javascript: new bradleysScriptInclude(current, 'parent.short_description', 'add somemore text');



the script include would probably need to look like;




var bradleysScriptInclude = Class.create();


bradleysScriptInclude.prototype = {




      initialize: function( /*GlideRecord*/ task, /*string*/ task_field, /*string*/ text) {


              this.task = task;


              if (!task || !task.isValidRecord()) {


                      if (!task.isNewRecord()) {


                              return;


                      }


              }


              return task[task_field] + text;


      },




      type: "bradleysScriptInclude"


};



View solution in original post

8 REPLIES 8

Jace Benson
Mega Sage

Bradley,



The templates apply the javascript server side, not client side.   That's why the first call work, gs is available only server side.


With that being said, you can invoke a script include to do what you want.   You would have to first create the script include and it would probably look like;


javascript: new bradleysScriptInclude(current, 'parent.short_description', 'add somemore text');



the script include would probably need to look like;




var bradleysScriptInclude = Class.create();


bradleysScriptInclude.prototype = {




      initialize: function( /*GlideRecord*/ task, /*string*/ task_field, /*string*/ text) {


              this.task = task;


              if (!task || !task.isValidRecord()) {


                      if (!task.isNewRecord()) {


                              return;


                      }


              }


              return task[task_field] + text;


      },




      type: "bradleysScriptInclude"


};



Brilliant. I hadn't considered the distinction between functions available on the client vs. the server. Thanks for the sample script. I'll work with my admins to give that a whirl.



You could also just do javascript:current.parent.short_description + 'add some text here'



Not sure why I didnt think of that earlier.


Well I guess I'm not sure if current's available, if its not you may still have to do the scriptinlcude route.


I couldn't get "current" to work, so I'll probably have to stick with the scriptInclude.