- 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 02:56 AM
what came in logs for that object?
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 03:00 AM
it worked for me
Any query business rule on pm_project is restricting you?
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 03:01 AM
Ankur, I found the error. Revising what I had defined in client script I found a redefinition of data object like follows:
$scope.data = {
month: '1';
}
Because of this redefinition, data object never arrives to ng-repeat, so nothing is generated using this method. Thanks a lot for your help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-26-2025 03:07 AM
Hello @jordimsant
- I have tried your script in my PDI and it works fine.
- Please verify if the option schema is configured correctly.
Steps to open the option schema form:
- Click on the hamburger menu on the top-right corner and select Edit option schema
- Option schema form appears as shown below.
Note: Default value should be the sys_id of the record from table 'sys_db_object'
Result:
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar