Calling array through an alert

kunal16
Tera Expert

I have wrote a script include for an array outcome. I am calling the same script include through a client script. What I need is to get an alert/pop-up alert to display the result of the same array. How can that be achieved?

1 ACCEPTED SOLUTION

Hi Kunal,



Instead of alerting it in the loop just add it and display it after the loop's finished:



  1.   function _processResponse(response) {
  2.           var userAttributes = response.responseXML.getElementsByTagName("userAttributes"),
  3.                   sStringToAdd = "",
  4.                   sAnswer = "";
  5.           for (var i=0; i<userAttributes.length; i++){
  6.                       sStringToAdd = i<userAttributes.length - 1 ? userAttributes[i].getAttribute("value") + "," : userAttributes[i].getAttribute("value") + "";
  7.                       sAnswer += sStringToAdd;
  8.           }
  9.           alert(sAnswer);
  10.   }


Cheers



Greg


View solution in original post

11 REPLIES 11

bernyalvarado
Mega Sage

Hi Kunal,



I put together a simple example of how it could be done. I hope it's helpful



On the script include



var getAttributesInArray = Class.create();


getAttributesInArray.prototype = Object.extendsObject(AbstractAjaxProcessor, {



getAttributes: function() {


     


  var arrAttributes =   [];



  // ..


  // Here goes the lines of code which populate your array


  //


 


  if (arrAttributes.length > 0)


  {


            for (var i=0; i<arrAttributes.length; i++){


                      this._addAttribute("attribute",arrAttributes[i].toString());


            }


  }


},



_addAttribute : function(name, value)


      {


          var userAttributes = this.newItem("userAttributes");


          userAttributes.setAttribute("name", name);


          userAttributes.setAttribute("value", value);


      }


});




On the client side:



function onChange(control, oldValue, newValue, isLoading) {


   


  var ga = new GlideAjax('getAttributesInArray');


  ga.addParam('sysparm_name','getAttributes');


  ga.getXML(_processResponse);




 


  function _processResponse(response) {


            var userAttributes = response.responseXML.getElementsByTagName("userAttributes");


            for (var i=0; i<userAttributes.length; i++){


                      alert(userAttributes[i].getAttribute("value"));


            }


  }


}


Hi Berny,


Thank you so much for your reply. It is really helpful.



Now I am facing the issue is: if there are two values in the array what I am returning from the script include, it is showing two alerts one after another. First alert with the first value then second alert with the second value.


example:


Alert(INC0000001)


Alert(INC0000001)



I want only one alert to show and the values will be ',' separated.


So I want the result like:


alert(INC0000001,INC0000002)



Please help me in achieving the result like this.


When you are pushing the values in a array in script include, before returning the value, add a toString() to the array variable.



return arrayvariable.toString();



Now, you will have the comma separated value at the client side.


Yes toString is added before only. I am trying to comma separate the result at the client side now.


Any help will be appreciated.