- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 09:57 AM
Our team created a new table that extends Instance with Table and linked it to a widget to use as an option schema. Within that table, we have a field called "Subfield" which is a field_list type. In our widget, we want to display all of the Subfields a user chooses. Our server script looks like this, but it is obviously incorrect:
(function() {
var caseTable = options.table;
var me = gs.getUserID();
if (!input) {
return;
}
//After page initially loads get data
if (input && input.action == 'retrieve_data') {
var gr = new GlideRecordSecure(caseTable);
gr.addQuery('active', true);
gr.addEncodedQuery(options.filter);
gr.query();
while(gr.next()){
data.large_display=gr.getDisplayValue(options.large_display_field);
data.subfields = gr.getDisplayValue(options.sub_fields).split(',');
}
}
})();
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 12:06 PM
Hi Yundlu,
I think I'll need more information on what you'd like for these subfields to do. If you're imagining the user can edit the widget options to change the fields, that's not the intended function of Instance Options. The Options are bound to that instance of the widget, meaning if a user changes them, it will change for everyone who views that Service Portal page.
As for why you are not returning any values in the data.subfields, you will have to split the sub_fields to an array first, then iterate in a for loop for each field to grab the values. I'd recommend storing them in an object.
Something like this:
data.subfields = {};
var sfArray = options.sub_fields.split(',');
for(i=0; i<sfArray.length; i++){
data.subfields[sfArray[i]] = gr.getDisplayValue(sfArray[i])
}
You'll then have an object of all of your subfields with their field names as the key and values as the value. But even then, the way you have it now is going to overwrite your subfields and fields for each GlideRecord returned, because you are redefining them in you while(gr.next()) loop. Are you imaging building out a table where the user can specify the fields? If so, we will need to build out a config table to house the users settings and pull from there, if there aren't any present, then we can use the field_list as default. We'll also need to build out a method for the users to edit the fields. In this case, I don't think we could use the Data Table widget as-is.
I think we need to step back and reassess the problem we are trying to solve for before continuing to solution.
If my answer helped you, or is correct, please mark it as 'Helpful' or 'Correct'
Thanks,
Josh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 12:06 PM
Hi Yundlu,
I think I'll need more information on what you'd like for these subfields to do. If you're imagining the user can edit the widget options to change the fields, that's not the intended function of Instance Options. The Options are bound to that instance of the widget, meaning if a user changes them, it will change for everyone who views that Service Portal page.
As for why you are not returning any values in the data.subfields, you will have to split the sub_fields to an array first, then iterate in a for loop for each field to grab the values. I'd recommend storing them in an object.
Something like this:
data.subfields = {};
var sfArray = options.sub_fields.split(',');
for(i=0; i<sfArray.length; i++){
data.subfields[sfArray[i]] = gr.getDisplayValue(sfArray[i])
}
You'll then have an object of all of your subfields with their field names as the key and values as the value. But even then, the way you have it now is going to overwrite your subfields and fields for each GlideRecord returned, because you are redefining them in you while(gr.next()) loop. Are you imaging building out a table where the user can specify the fields? If so, we will need to build out a config table to house the users settings and pull from there, if there aren't any present, then we can use the field_list as default. We'll also need to build out a method for the users to edit the fields. In this case, I don't think we could use the Data Table widget as-is.
I think we need to step back and reassess the problem we are trying to solve for before continuing to solution.
If my answer helped you, or is correct, please mark it as 'Helpful' or 'Correct'
Thanks,
Josh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 02:13 PM
Hi Josh, thanks for your help and sorry for not explaining it clearly! You are right, I did not want users to be able to change the option schema. I meant to say, our team wants the admin to be able to change the option schema and it would be applied for all end-users. Your code was a huge help, I've modified it a bit to fit my needs:
(function() {
var caseTable = options.table;
var me = gs.getUserID();
data.subfields = [];
if (!input) {
return;
}
//After page initially loads get data
if (input && input.action == 'retrieve_data') {
var gr = new GlideRecordSecure(caseTable);
gr.addQuery('active', true);
gr.addEncodedQuery(options.filter);
gr.query();
while(gr.next()){
var sfArray = options.sub_fields.split(',');
for(i=0; i<sfArray.length; i++){
data.subfields[i] = gr.getDisplayValue(sfArray[i])
}
data.large_display=gr.getDisplayValue(options.large_display_field);
}
}
})();
and the HTML:
<div style="padding: 20px 0px;">
<h2 style="color: #{{c.options.font_color}};">Welcome, <b class="m-r-lg">{{c.data.large_display}}</b></h2>
<span class="h4" style="color: #{{c.options.font_color}};" ng-repeat="item in c.data.subfields track by $index"> {{item}}
<span ng-if="!$last">|</span>
</span>
</div>
Thanks much!