The CreatorCon Call for Content is officially open! Get started here.

UI Action onfocus

Blair5
Tera Guru

When a user clicks on a UI action, I want them to be focused on the mandatory variables on the RITM. When I have the function to setMyOnFocus, the whole script breaks. Is it possible to do what I want in in a UI action for variables on an RITM?

function startPhysical(){

confirm('Are you sure you want to start the Physical Phase?');

var elem = g_form.getControl('variables.sdlc_tracking');

elem.onfocus = setMyOnFocus;

  g_form.setMandatory('variables.sdlc_tracking', true);

  g_form.setMandatory('variables.project_compliant_with_corp_standards', true);

  g_form.setMandatory('variables.aca_agree_disagree', true);

  g_form.setValue('u_start_physical', true);

}

}

function setMyOnFocus() {

this.select();

}

1 ACCEPTED SOLUTION

tltoulson
Kilo Sage

Hi Blair,



The problem is that the g_form API only respects variables using certain functions on Task records (like RITM), sadly getControl is not one of them (Client Script Access to Variable Fields on Task Records - ServiceNow Wiki).   In fact, the getControl function is deprecated due to the Mobile UI but for now we can still make things work with getControl but it is a bit of a hack.



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


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


              g_form.getControl(g_sc_form.nameMap[i].realName).focus();


  }


}



This is using the nameMap variable which maps the "project_code" variable name to the internal name ServiceNow uses for the variable.   So basically we loop through nameMap, find the variable in the map, get the real name, use getControl on the realName and then execute the focus on the element.   I hope this helps.



Kind regards,



Travis




Related: Set Fields Style/Attribute on Variable Editor


View solution in original post

12 REPLIES 12

Saved me couple of hours here . Between how did you came about writing this code ? Just by looking the html source of the RITM?


Hi Kalai,



Basically, yes, I looked at the html source code of the RITM.   In particular, I targeted the GlideForm source code.   The getControl function didn't give much direction for me so I started looking through the rest of the code.   Through that, I discovered the nameMap property and recalled someone else mentioning it before though I don't recall who it was now.   But didn't see it used much in GlideForm.   So then I did a quick search of the source code for nameMap and saw it was part of an undocumented object ServiceCatalogForm, which eventually led to the solution using g_sc_form.


I was struck at a similar problem and was basically looking the HTML source and nameMap's in particular .. Did a community search using that and had a working code ready



Thanks for sharing this... It was a real life saver today