- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2015 10:06 AM
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();
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2015 12:12 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2015 11:32 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2015 11:38 AM
Anurag,
This isn't working either. I get the error:
Uncaught TypeError: Cannot read property 'onfocus' of undefined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2015 12:06 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2015 12:12 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2015 12:47 PM
Travis,
That worked! Awesome - thank you so much!!