Develop > Programming Guide > JavaScript and Service Manager reference > List: JavaScript examples > Example: Query a record using the SCFile object

Example: Query a record using the SCFile object

This JavaScript example demonstrates how to perform the following objectives.

  1. Query a record from a table using the SCFile object.
  2. Obtain the resulting record in XML format by using the getXML method that gives you field names in addition to values.
  3. Iterate through the resulting XML and print the field names and values. For a more robust approach, see Example: Iterating through a complex XML document.
var fContacts = new SCFile( "contacts" );

var rc = fContacts.doSelect( "contact.name = \"BROWN, NICHOLAS\" " );

if ( rc != RC_SUCCESS )  // doSelect can also return RC_BAD_QUERY or RC_NO_MORE
{
  print( "Error - doSelect returned " + RCtoString(rc) );
}
else
{
  var xmlContactsRecord = fContacts.getXML().instance;

  var fldIndex;

  for ( fldIndex in xmlContactsRecord )
  {
    var node  = xmlContactsRecord[fldIndex];

    if ( node.getFirstChildElement() != null ) // this node has child elements
    {
      var i;

      for ( i in node ) // print the child elements
      {
        var childNode = node[i];

        print( childNode.getName(), "=", childNode.getValue() );
      }
    }
    else
      print( node.getName(), "=", node.getValue() );

  }
}