- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-26-2025 01:48 AM
I am trying to do a widget with a select field containing projects in my instance. The code I have developed is the following:
HTML:
<div class="container">
<div class="section">
<h2 class="section-header">Facturació</h2>
<div class="form-group">
<label for="whatToSee" class="control-label">QuĆØ vols veure? </label>
<select id="opcions" name="opcions" ng-model="data.whatToSee">
<option ng-repeat="element in data.elements" value="{{::element.value}}">
{{element.label}}
</option>
</select>
</div>
</div>
</div>
Server Script:
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
data.instance = gs.getProperty('instance_name');
//Carreguem els projectes com a opcions del formulari
data.elements = [];
//gs.log('options.options_table = ' + options.options_table, 'WIDGET');
var table = new GlideRecord('sys_db_object');
table.get(options.options_table);
var gr = new GlideRecord(table.name.toString());
var query = 'active=true';
gr.addEncodedQuery(query);
gr.query();
while(gr.next()) {
data.elements.push({
"value": gr.sys_id.toString(),
"label": gr.short_description.toString()
});
}
})();
I have already checked that data.elements gets correctly populated, but when I try to call element.value and element.label it seems not to be working on the HTML. Anyone knows which is the problem?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-26-2025 02:19 AM
it worked for me
I will suggest add gs.info() to check what came in data elements
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
data.instance = gs.getProperty('instance_name');
//Carreguem els projectes com a opcions del formulari
data.elements = [];
//gs.log('options.options_table = ' + options.options_table, 'WIDGET');
var table = new GlideRecord('sys_db_object');
table.get(options.options_table);
gs.info('Options Table is' + options.options_table);
var gr = new GlideRecord(table.name.toString());
var query = 'active=true';
gr.addEncodedQuery(query);
gr.query();
gs.info('Row count' + gr.getRowCount());
while(gr.next()) {
data.elements.push({
"value": gr.sys_id.toString(),
"label": gr.short_description.toString()
});
}
gs.info('data elements' + JSON.stringify(data.elements));
})();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-26-2025 01:55 AM
try this
HTML:
<div class="container">
<div class="section">
<h2 class="section-header">Facturació</h2>
<div class="form-group">
<label for="whatToSee" class="control-label">QuĆØ vols veure? </label>
<select id="opcions" name="opcions" ng-model="data.whatToSee">
<option ng-repeat="element in data.elements" value="{{element.value}}">
{{element.label}}
</option>
</select>
</div>
</div>
</div>
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-26-2025 01:58 AM
I have tried to delete "::" but it is still not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-26-2025 02:19 AM
it worked for me
I will suggest add gs.info() to check what came in data elements
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
data.instance = gs.getProperty('instance_name');
//Carreguem els projectes com a opcions del formulari
data.elements = [];
//gs.log('options.options_table = ' + options.options_table, 'WIDGET');
var table = new GlideRecord('sys_db_object');
table.get(options.options_table);
gs.info('Options Table is' + options.options_table);
var gr = new GlideRecord(table.name.toString());
var query = 'active=true';
gr.addEncodedQuery(query);
gr.query();
gs.info('Row count' + gr.getRowCount());
while(gr.next()) {
data.elements.push({
"value": gr.sys_id.toString(),
"label": gr.short_description.toString()
});
}
gs.info('data elements' + JSON.stringify(data.elements));
})();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-26-2025 02:45 AM
I have tried exactly what you have creating a new widget and it does not work for me. What can be causing this?