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

try this



unction startPhysical(){


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


  var elem = g_form.getElement('sdlc_tracking');


  elem.focus();


  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);


}


-Anurag

Anurag,



This isn't working either. I get the error:



Uncaught TypeError: Cannot read property 'onfocus' of undefined


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


Travis,



That worked! Awesome - thank you so much!!