for loop in script include using same value in each iteration

austinb
Kilo Contributor

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"

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Please try with below while loop in script include and see if this helps.



  1.         while (grChoices.next()){    
  2.                   choices[choices.length] = grChoices.name.toString(); //name is just a string  
  3.         }

View solution in original post

4 REPLIES 4

conanlloyd
Giga Guru

It looks to me that lines 7-9 in your script include overwrite the array instead of join the new value to it.


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.


Write some log messages so you can see what is really happening.


Shishir Srivast
Mega Sage

Please try with below while loop in script include and see if this helps.



  1.         while (grChoices.next()){    
  2.                   choices[choices.length] = grChoices.name.toString(); //name is just a string  
  3.         }