using \$sp.getForm to get all fields in a form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2017 07:55 PM
Hi Experts,
I'm trying to retrieve all the fields in a form by using $sp.getForm in my widget server script, but it's returning an empty object array. My code looks like this:
(function() {
var me = gs.getUserID();
data.user = gs.getUserDisplayName();
data.table ='x_dnf_questionnaire';
var gr = new GlideRecord('sys_ui_form');
gr.addQuery('name', data.table);
gr.query();
if(gr.next()) {
data.sys_id = gr.getValue('sys_id');
}
data.f = $sp.getForm(data.table, data.sys_id);
gs.addInfoMessage(data.f);
The above gs.addInfoMessage returns {}. When I do a gs.addInfoMessage(data.f._sections), it actually returns all the different sections of my form in an object array as shown here:
https://community.servicenow.com/community/develop/blog/2017/02/07/portal-diaries-service-portal-app.... Why is _fields not returning the fields of the form?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2017 07:09 AM
Hi David,
Thats right data table widget only displays records. My bad.
Best Regards,
Varad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2018 10:19 AM
Could you clone the ootb form widget and just modify the HTML/CSS to meet the design you're trying to accomplish?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2021 02:24 PM
Hello,
For anyone still facing this issue:
$sp.getForm is not returning anything in '_fields' because in the example above, you're passing the wrong value in data.sys_id.
The first parameter you pass in the $sp.getForm function should be the table name, for example "sys_user". The second parameter you pass in the function should be the sys_id of the record in that table you want to retrieve, not the sys_id of the form itself. That's why in the example of the User Profile widget, we're passing the User record sys_id as second parameter.
I hope this helps!
Please mark as helpful if this was able to help you out!