- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2017 08:16 PM
I have a ui page, and in the client script I call a script include to get some html based off of data in a table:
$("#tableID").on('click', '.tablebutton', function(event){
var ga = new GlideAjax('ServerScripts');
ga.addParam('sysparm_name', 'GetOptions');
ga.getXML(OptionsLoaded);
});
function OptionsLoaded(response){
var result = response.responseXML.getElementsByTagName("result");
var returnedHtml = result[0].getAttribute("html");
$("#divID").append(returnedHtml);
}
In the script include I get the entries in a table and add them as options to a html select:
GetOptions:function(){
var html = "";
var result = this.newItem("result");
var choices = [];
grChoices = new GlideRecord("x_table_of_choices");
grChoices.query();
while (grChoices.next()){
choices[choices.length] = grChoices.name; //name is just a string
}
html += "<select>";
for (i = 0; i < choices.length; i++) {
html += "<option>" + choices[i] + "</option>";
}
html += "</select>";
result.setAttribute("html", html );
},
type: 'ServerScripts'
now in the case where there are 5 entries in the table, I get this returned:
<select>
<option>choice 5<option>
<option>choice 5<option>
<option>choice 5<option>
<option>choice 5<option>
<option>choice 5<option>
</select>
but if i copy this code into the client script and run it there it works as expected:
<select>
<option>choice 1<option>
<option>choice 2<option>
<option>choice 3<option>
<option>choice 4<option>
<option>choice 5<option>
</select>
Has anyone experienced something like this, or know why this would happen? Is there anything I should be doing to get it to work as expected?
<"x_table_of_choices" "x_table_of_choices""x_table_of_choices"
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2017 09:16 PM
Please try with below while loop in script include and see if this helps.
- while (grChoices.next()){
- choices[choices.length] = grChoices.name.toString(); //name is just a string
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2017 08:22 PM
It looks to me that lines 7-9 in your script include overwrite the array instead of join the new value to it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2017 08:31 PM
That shouldn't be,
while (grChoices.next()){
choices[choices.length] = grChoices.name; //name is just a string
}
would act like
choices[0] = "choice 1"
now choices.length is 1 so next iteration
choices[1] = "choice 2"
etc.
Like I mentioned, I did copy the code into my client script and it worked as expected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2017 08:48 PM
Write some log messages so you can see what is really happening.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2017 09:16 PM
Please try with below while loop in script include and see if this helps.
- while (grChoices.next()){
- choices[choices.length] = grChoices.name.toString(); //name is just a string
- }