Set Fields Style/Attribute on Variable Editor

pss_rets
Kilo Explorer

Hi - Is there any way that I can change the fields style/attribute in the Variable Editor during onLoad? I have the below script in the sc_req_item with an onLoad event but this does not work. The variable width does not change during onLoad.

function onLoad() {

  var myVar = $('sys_display.' + g_form.getControl('requested_for').id); //Get the correct reference element

  myVar.style.width = '250px'; //Set the width

}

3 REPLIES 3

tltoulson
Kilo Sage

Try this:



function onLoad() {


  var myVar = $('sys_display.' + g_form.getControl('request.requested_for').id); //Get the correct reference element


  myVar.style.width = '250px'; //Set the width


}



I modified line 2.   The getControl call was not able to find the field requested_for because requested_for is actually on the request table.   By prefixing the id with the table name you will select the correct control.   Make sure to test across browsers and to check for good UI/UX when making these adaptations.   Also, if you have an Upgrade plan for future version upgrades, make sure you include this script in that plan so you can be sure it is properly tested.


Travis - Thanks for your reply. The "requested_for" is a variable, not a field in the RITM form. I even tried the following but still no change:



function onLoad() {


  var myVar = $('sys_display.' + g_form.getControl('variables.requested_for').id); //Get the correct reference element  


  myVar.style.width = '250px'; //Set the width


}



function onLoad() {


  var myVar = $('sys_display.' + g_form.getControl('variable_pool.requested_for').id); //Get the correct reference element  


  myVar.style.width = '250px'; //Set the width


}


Ah yes, my mistake.   Variables can only be accessed through certain functions using the variables.requested_for approach as documented in Client Script Access to Variable Fields on Task Records - ServiceNow Wiki.   Here is an approach that may work (since the $ is undocumented anyway, whats the harm in diving into the deep end):



function onLoad() {


      for (var i = 0; i < g_sc_form.nameMap.length; i++) {


                  if (g_sc_form.nameMap[i].prettyName == 'requested_for') {


                          g_form.getControl(g_sc_form.nameMap[i].realName).style.width = '250px';


                  }


      }


}




This uses the nameMap for variables under the g_sc_form... obviously some undocumented client code here.   But for me it does work and I've seen a few others using the variable hacks.