AgileApps Support Wiki Pre Release

Code Sample:Test of Search using Java API

From AgileApps Support Wiki
Revision as of 00:38, 10 August 2011 by imported>Aeric (Created page with "This example does a number of things: :* It defines a search method on the Orders object, and uses the Unit Test Framework to execute it. :* It gets all fields in <tt>searchR…")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This example does a number of things:

  • It defines a search method on the Orders object, and uses the Unit Test Framework to execute it.
  • It gets all fields in searchRecords or getRecord, using the "*" wildcard.
<syntaxhighlight lang="java" enclose="div">

package com.platform.demo.test;

import com.platform.api.*;

public class SearchTest {

 public String searchOrders() throws Exception {
 
   Result result = Functions.searchRecords("Order", "*", "");
      //Toss the exception, if one occurs
   
   int resultCode = result.getCode();
   if (resultCode < 0)
   {
       // Some error happened.
       String msg = "Error during search";
       Functions.debug(msg + ":\n" + result.getMessage());  // Log details
       Functions.throwError(msg + ".");                     // Error dialog
       return("");
   }
   else if(resultCode == 0)
   {
       // No records found. Take action accordingly
       Functions.throwError("No results returned from search");
       return("");
   }
   else
   {
       //Records retrieved successfully
       ParametersIterator iterator = result.getIterator();
       boolean first_time = true;
       while(iterator.hasNext())
       {
           Parameters params = iterator.next();
           String num= params.get("order_number");
       
           // List the fields present in the params object
           if (first_time) {
               for (Object key : params.keySet() ) {
           	   Functions.debug(""+key);
               }
               first_time = false;
           }
                   
           // Use the Lookup value in related_to_Customer to get customer info
           String customerID = params.get("related_to_Customer");
           Result customer = 
       	Functions.getRecord("Customer", "*", customerID);
           String company = customer.getParameters().get("customer_name");
       
           // Echo order info
           Functions.debug("Order #" +  num + " for " + company);       
       }        
       //Result code is the #of records found.
       return ""+resultCode;
   }
 }   
 
 @TestMethod
 public void runSearchOrders() throws Exception {
    String record_count = searchOrders();
    RunTest.assertEquals(record_count, "6");
 }
 

} </syntaxhighlight>