using \$sp.getForm to get all fields in a form

davilu
Mega Sage

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.

7 REPLIES 7

Hi David,



Thats right data table widget only displays records. My bad.



Best Regards,


Varad


Could you clone the ootb form widget and just modify the HTML/CSS to meet the design you're trying to accomplish?


Mike Similon
Tera Contributor

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!