Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Run business rule after template is applied

xiaix
Tera Guru

Yes, I read this Run Business rule after template is applied   but he/she is not looking to do what I'm looking to do... hence this new post.

I need to detect when a certain template is applied to an Incident form, and then do ... for lack of a better term, stuff to the form (change field values, etc..)

Kinda stuck on how to do this.   Do I apply a BR on the Incident table or the sys_template table?   Or is this "listener" even handled at the BR level at all?

9 REPLIES 9

I just realized that it's because I made the new field "Read Only".   I need it Read Only though so that users can't manually trigger this field.   How frustrating this is.


Ah yes, I'd already replied before I spotted this update.


Yes... this kind of thing can be very frustrating


Hi David,



A read-only field can't be populated by applying a template. Templates are applied in the clent and the same rules apply that would otherwise apply to a user updating the fields manually. But you could hide the field with a UI policy if it's empty, and you could have a client script to disable the field once it's populated (to prevent users from populating it manually).



Jamie.


Yeah, I added this Client Script (onLoad) on the Incident Table:


function onLoad()


{


      // Remove the "Applied Template" field


      var flds = $$('DIV').each(function(fld) {


              if(fld.id && fld.id.indexOf('u_applied_template') >= 0)


              {


                      fld.hide();


              }


      });


}



Works great.


Just for sharing purposes, the reason I needed this was because of the way Templates add "Time worked" (time_worked).   If you use a Template to put time into a ticket, it rewrites the time_worked field instead of putting the time in the hours/minutes/seconds field(s).



So, here's my onChange script for the new (hidden) field I created:


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


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


              return;


      }



      if (newValue.length)


      {


              var oldTime = document.getElementById('sys_original.incident.time_worked');


              if (oldTime)


              {


                      var otv = oldTime.value;


                      var otvA = otv.split(" ");


                      var otvA_3 = otvA[1].split(":");


                      var o_hr = otvA_3[0] ? parseInt(otvA_3[0]).toFixed():0;


                      var o_min = otvA_3[1] ? parseInt(otvA_3[1]).toFixed():0;


                      var o_sec = otvA_3[2] ? parseInt(otvA_3[2]).toFixed():0;



                      var ntvA_3 = g_form.getValue('time_worked').toString().split(":");


                      var n_hr = ntvA_3[0] ? parseInt(ntvA_3[0]).toFixed():0;


                      var n_min = ntvA_3[1] ? parseInt(ntvA_3[1]).toFixed():0;


                      var n_sec = ntvA_3[2] ? parseInt(ntvA_3[2]).toFixed():0;



                      // Original


                      if (o_hr < 10) { o_hr = '0' + o_hr.toString(); }


                      if (o_min < 10) { o_min = '0' + o_min.toString(); }


                      if (o_sec < 10) { o_sec = '0' + o_sec.toString(); }


                      // New


                      if (n_hr < 10) { n_hr = '0' + n_hr.toString(); }


                      if (n_min < 10) { n_min = '0' + n_min.toString(); }


                      if (n_sec < 10) { n_sec = '0' + n_sec.toString(); }



                      var originalTime = "1970-01-01 " + o_hr.toString() + ":" + o_min.toString() + ":" + o_sec.toString();


                      g_form.setValue('time_worked', originalTime);



                      var inputs = document.getElementsByTagName('input');


                      for (var ip = 0; ip < inputs.length; ip++)


                      {


                              if (inputs[ip].id && inputs[ip].id.indexOf('tmr_') == 0)


                              {


                                      if (inputs[ip].id.indexOf('_hour') == inputs[ip].id.length - 5)


                                              inputs[ip].value = n_hr;


                                      if (inputs[ip].id.indexOf('_min') == inputs[ip].id.length - 4)


                                              inputs[ip].value = n_min;


                                      if (inputs[ip].id.indexOf('_sec') == inputs[ip].id.length - 4)


                                              inputs[ip].value = n_sec;


                              }


                      }


              }


              g_form.clearValue('u_applied_template');


      }


}








This script keeps the original time_worked values, and adds the template values into the appropriate input boxes.  



This is how it should be OOB.