0 Replies Latest reply on Aug 14, 2007 12:48 PM by JoshBeall

    Calling a web service that can returns a collection with 1 or more objects

    JoshBeall Level 1
      Hi All,

      I have a web service that can return a "Contacts" collection that looks like this in XML:

      <Contacts>
      <CurrentInformationContact>
      <ContactType>string</ContactType>
      <ContactValue>string</ContactValue>
      </CurrentInformationContact>
      <CurrentInformationContact>
      <ContactType>string</ContactType>
      <ContactValue>string</ContactValue>
      </CurrentInformationContact>
      </Contacts>

      Example ContactTypes are "Home" and "Work". The ContactValues would then be phone numbers.

      It is possible to have only one CurrentInformationContact element, or you could have 2 or more. If you have only one, the web service result can be accessed like this:

      trace(result.Contacts.ContactType + " phone is " + result.Contacts.ContactValue);

      However, if there are 2 or more CurrentInformationContact elements returned, you need to access the Contacts collection like this:

      trace(result.Contacts[0].ContactType + " phone is " + result.Contacts[0].ContactValue);

      These two methods of accessing the contacts collection are mutually exclusive. Meaning that if there is exactly one Contact, you cannot use array indices (it isn't an array). If there are 2 or more, you have to specify the array indices.

      I have taken to do the following (see attached code).

      var contacts:ArrayCollection;
      if(info.Contacts is ArrayCollection)
      {
      contacts = info.Contacts;
      }
      else
      {
      contacts = new ArrayCollection();
      contacts.addItem(info.Contacts);
      }

      for(var n:int;n<contacts.length;n++)
      {
      this.addRowToGrid(resultGrid,contacts[n].ContactType,contacts[n].ContactValue);
      }