Service Portal - getElement
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 11:41 AM
Hello Community,
I've been scouring the community to see if I could find some solid information to reference on this issue but I've come up empty on everything I have found and tried. I am wondering if anyone has found a way to get a DOM element on the Service Portal from a client script. Before switching to the Service Portal, we implemented some field formatting via an onLoad client script that utilized the g_form.getElement() method which is now deprecated(along with g_form.getFormElement). I am able to utilize the g_form.getControl() method but it does not contain the DOM element.
Here is a snippet of the method I am trying to use. It is in a global UI Script which has been loaded into my Service Portal via the Theme JS Includes...
function startPhoneNumberFormat(id){
var phoneNumberInput = g_form.getElement(id);
phoneNumberInput.addEventListener('input', function(event){
phoneNumberInput.value = formatPhoneNumber(this.value);
}, false);
phoneNumberInput.addEventListener('onChange', function(event){
phoneNumberInput.value = formatPhoneNumber(this.value);
}, false);
}
When I call the method from an onLoad client script, i get console errors saying that g_form is not defined.(This same error was mentioned in a different document i read). So, if I can't run this method, I'd settle for recreating the functionality locally for the catalog item but regardless of the approach, I need a way to identify and retrieve the DOM element. Any suggestions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017 02:59 PM
awortham So, I am doing this on a custom table in a scoped application i'm working on, not a catalog item. So, when I go to view the dictionary record for any of the fields, it shows me the 'widget' field, however, it's just a string field and when hovering over the label, it shows 'Unused'...
If that has any credibility, that explains why the widget i have listed is not working. Basically, i created a simple widget that's just a string input field that colors the text red(so I know it's my widget) but it won't show up on the form even after listing it in the widget field.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017 03:47 PM
You will have to define g_from variable in client controller of your widget to access g_form methods
Please see example below
var g_form = $scope.page.g_form;
for(var f in fields)
g_form.setDisplay(fields[f].name, false);
Regards,
Sachin
http://www.cram.com/flashcards/servicenow-system-administration-certification-5002823
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2017 02:01 AM
My apologies, maybe i wasn't clear enough in my previous comment. I have figured out how to access the g_form methods. That's no longer the issue. It is noted in the ServiceNow Documentation that getElement is no longer a supported method. So, at this point I'm looking to re-create the functionality that exists for catalog items whereby if you are currently using a UI Macro on a form, in the Service Portal, it won't work and you'd have to replace it with a widget using the widget reference field on the variable in question. That's where I'm currently at in this process.
The problem there is that the form I'm using is not a catalog item, and the widget field on the dictionary record is also not a reference so, viewing this form in the default UI and in the Service Portal shows me the exact same field, the widget that I've created never shows up. That's the solution i'm seeking.