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.

Catalog item set focus

sgmartin
Kilo Guru

I've been through a ton of Community articles and down so many rabbit holes trying to find an answer that works for this.   When pulling up a Catalog Item,   I have a form for users to request access to one of our datacenters.   I can use lookups because the person that needs access could be a 3rd party vendor.   So, the entire form needs the person to input everything.   I have a working validation script for the email address and the phone #, but if the user inputs an invalid entry I would like the focus right back on that field.

Also, when the form loads I would like the focus to be on the first field.   Does anyone have anything that actually works on a Catalog Item form?

I have tried this:

field='start_date';

  var fieldToFocus = g_form.getControl(field);

  fieldToFocus.focus();

  return false;

and this:

window.setTimeout(cursor,0);

function cursor() {

      var refocus = document.getElementById("IO:908699224ff76600932e8b8d0210c7ca");

      refocus.focus();

}

1 ACCEPTED SOLUTION

alinatoc
Giga Contributor

First of all, in my opinion, I think it's better to try avoiding usage of sys_id since there's a possibility for sys_id to change (just an opinion).



Btw, going for the answer, try with a jquery approach; just add this snippet to your client script:



(function($) {



      var fieldname = 'start_date';


      $(g_form.getControl(fieldname)).focus();


 


})(jQuery.noConflict());



1. First, we wrapped it in a scope-safe IIFE (Immediately-invoked function expression - Wikipedia ), it allows us to safely use jQuery without conflict in usage of "$".
Others use $j, but in my wide experience of using jQuery, this is the safest way of doing jQuery.
2. g_form.getControl(...) method returns the DOM of the specified field name. So we transform it into a jQuery DOM object, then we invoke jQuery.focus() method.



Let me know if it works. Cheers


View solution in original post

6 REPLIES 6

tobrien
Kilo Guru

Hi, I just recently came across this thread looking to do the same thing -- simply place the focus.



What I discovered was that what you "think" is the element name may not be ...



For example the first line below succeeds, the second does not.



g_form.setValue('asset', '');


g_form.getElement('asset').focus();



When I used Chrome's INSPECT mechanism I saw that the Element ID was a bit more complex...



g_form.setValue('asset', '');


g_form.getElement('sys_display.alm_transfer_order_line.asset').focus();



And now both lines succeed.




Adam Syed1
Kilo Contributor

To focus on a field in record producer on portal side copy and paste the below code in your onload client script and replace the "011a705e1355d348eb633598d144b023" with the sys_id of that field.

 

setTimeout("var refocus = document.getElementById('sp_formfield_IO:011a705e1355d348eb633598d144b023');refocus.focus();",0);

 

 

Thanks,

Adam Syed