AgileApps Support Wiki Pre Release

Difference between revisions of "Code Sample:Test of Search using Java API"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
m (Text replace - '// Error dialog' to '// Error message')
 
(One intermediate revision by the same user not shown)
Line 25: Line 25:
         // Some error happened.
         // Some error happened.
         String msg = "Error during search";
         String msg = "Error during search";
         Functions.debug(msg + ":\n" + result.getMessage());  // Log details
         Logger.info(msg + ":\n" + result.getMessage(), "Search");  // Log details
         Functions.throwError(msg + ".");                     // Error dialog
         Functions.throwError(msg + ".");                           // Error message
         return("");
         return("");
     }
     }
Line 48: Line 48:
             if (first_time) {
             if (first_time) {
                 for (Object key : params.keySet() ) {
                 for (Object key : params.keySet() ) {
               Functions.debug(""+key);
               Logger.info(""+key, "Search");
                 }
                 }
                 first_time = false;
                 first_time = false;
Line 60: Line 60:
          
          
             // Echo order info
             // Echo order info
             Functions.debug("Order #" +  num + " for " + company);       
             Logger.info("Order #" +  num + " for " + company, "Search");       
         }         
         }         
         //Result code is the #of records found.
         //Result code is the #of records found.

Latest revision as of 22:30, 10 September 2013

This example does a number of things:

  • It defines a search method on the Orders object.
  • It creates a test method to run that search, taking advantage of the [RunTests] button to execute the method.
    Learn more: Unit Test Framework
  • It gets all fields in searchRecords and getRecord, using the "*" wildcard.
  • It enumerates the names of the fields that are returned, putting them into the Debug Log.
    Note: Enumerating things in a JSP Page would produce a nicer result, without much additional effort. That page could then be named as a Web Tab, which would become available for use as a tab in the application.
  • In addition data contained in an Order record, it uses the Java getRecord API and the data in the related_to_Customer lookup field to get data from the related Customer record.
    Note: The Java API Composite Object getRecord method could also be used to specify the customer name as a field to retrieve. But the "wildcard" specifier could not be used for field names, in that case.
See also: More searchRecords Examples
<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";
       Logger.info(msg + ":\n" + result.getMessage(), "Search");  // Log details
       Functions.throwError(msg + ".");                           // Error message
       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() ) {
           	   Logger.info(""+key, "Search");
               }
               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
           Logger.info("Order #" +  num + " for " + company, "Search");       
       }        
       //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>