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

I was messing with this after the fact after Christian asked below and I also could not get current to work in any form.   Which means, I couldn't get the script include to work as it needs to know what record your on to get it's parent.


Hi Jace



I tried using the script include but am not able to get the current object to work. Is there anything additional i need to do for this?



Thanks


Aman


themrindy
Tera Expert

Hi Jace,



I have the same issue here with project apply template. Where did you call the first script include javascript: new bradley.... (from the template record?)



Would really appriciate some help!



/Christian


peter_foreman
Tera Guru

Hi,

As I understand, current may not available in the templates. Instance hardening removed this feature in our case.