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

varads_kulkarni
Giga Expert

Hi David,


$sp.getForm(data.table, data.sys_id) will render the form into your html template rather than providing you with the list of fields.


I would suggest you to use $sp.getFields(GlideRecord,List_of_fields) to get the fields on the form.



As an example, in your case,



var list = 'sys_created_on,sys_id,sys_updated_on'; // Add the field names that you want to retrieve separated by comma


data.f = $sp.getFields(gr,list);



Best Regards,


Varad


Thanks varads.kulkarni, our team is trying to display a form in the Service Portal through a Material Design modal window.   The back-end form has a lot of client scripts and UI policies that we want to bring over into the SP Widget.   I've read that using the ootb form widget will incorporate both client scripts and ui policies, but our team doesn't like the look and feel of the ootb form widget.   Ideally, we would use the form widget due to ease and convenience, but is there a way to edit the look and feel of the form widget?



Thanks.


Hi David,



Try creating a new form view on the platform and place only those fields that you require on the view. That create a new Data Table widget with this form and new view that you have created. Further tinkering regarding the look and feel can be done with the css part. - The best part here would be the client scripts and UI policies will be retained and you need not do any extra work on that.



Another method could be to build a new widget altogether and getting only the required fields through $sp.getFields(). Then you can play with these fields and place them wherever you like with the css template.



Hope that helps.



Best Regards,


Varad


Hey Varad, doesn't the Data Table widget just display the records?   I don't think that widget displays the actual form right?