Apply template to Incident OnChange

shris
Kilo Contributor

Hi.

I am a rank newbie at javascript and SNC--apologies in advance if this sounds dumb.

When the help desk is working on a ticket, they may need to change an existing ticket to P1, or to open a new P1. When this happens, we want to apply a template to that existing ticket instantly--the template sets the 'additional comments' field to have a bunch of questions that the help desk needs to answer before submitting the ticket.

So I read the wiki article on creating templates and even the snippet about applying them with scripts, but apparently I am dense. I have created a script that runs against the priority field on the incident form, of type onChange. It is active and it is also marked 'global'. The script is:

//apply the P1 template when the priority is changed to 1.

function onChange(control, oldValue, newValue) {
       if (oldValue != newValue && newValue == 1)
       current.applyTemplate("p1_Template_for_Help_Desk");

}


I have tried this with an extra set of curly braces around the applyTemplate statement the way the code checker wants me to do, but it doesn't help. I have also tried removing the if statement so it applies the template no matter how the priority changes. That doesn't seem to help either. Originally there were spaces in the template name--I removed those to make sure they weren't the problem, and nothing changed.

I am sure there must be some newbie mistake in here, I'd really appreciate a pointer.

Note: There is another piece of script with the same settings and the same If statement that pops up an alert. The alert is working, but the applyTemplate doesn't. Do I need to use something other than 'current'? Is it not OK to have to essentially identical scripts run on the same event (the action being the only difference)?

Thanks very much for your time and help.
shris
12 REPLIES 12

Just open your template record, right-click the form header, and copy the URL or sys_id. Any record points to its sys_id in the url.


Hi Mark,



I have a simple requirement of populating all fields data in Change Request page by selecting the template. Its populating the data on all fields correctly however its going to infinite loop and doesn't allow to save or proceed.



Below is the line of codes:-


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


      if (isLoading || newValue == '') {


              return;


      }    


      var temp = g_form.getValue('u_std_chg_temp');


      var t = new TemplateRecord(temp);


      t._applyResponse =   function(response)


{


      if (!response || !response.responseXML)


      return;


      this.template = response.responseXML;


      this.applyRecord();


            return;    


};


              t.apply();


return;


}



it would be great if you can let me know what is wrong and why its going on infinite loop?



Thanks in advance!!!


Hi Mark, is there a way we could specify a template via it's name here as opposed to a sys_ID? I'm just aware the sys_ID may be different when promoting the Update Sets, or would the template sys ID (if newly created in the Update Set) be carried across with the same sys_ID? Many thanks! Daniel