searchRecords Examples

From AgileApps Support Wiki
Revision as of 21:33, 23 May 2011 by imported>Aeric
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Example: Basic Search

This example calls searchRecords with three parameters, assigning the returned value to an instance of Result and calling Result.getCode to assign the error code to a variable which is then conditionally checked to determine the code to execute.

If the search was not successful, the code calls throwError to display an error dialog.
If the search was successful and returned records, the code creates an instance of ParametersIterator. In a while loop, the code calls ParametersIterator.next to get an instance of Parameters from which it then extracts the name and number keys.
String accountID = "";
// Some logic to populate accountID variable.
Result result = Functions.searchRecords("ACCOUNT", "record_id,name,number", 
    "name contains 'Acme'");
int resultCode = result.getCode();
if (resultCode < 0)
{
    // Some error happened.
    String msg = "Account could not be retrieved";
    Functions.debug(msg + ":\n" + result.getMessage());  // Log details
    Functions.throwError(msg + ".");                     // Error dialog
}
else if (resultCode == 0)
{
   // No records found. Take action according to your business logic
}
else
{
  //Records retrieved successfully
  ParametersIterator iterator = result.getIterator();
  while(iterator.hasNext())
  {
    Parameters params = iterator.next();
    String accountID = params.get("record_id");
    String accountName = params.get("name");
    String number = params.get("number");
    // Take action according to your business logic
  }
}
Example: Sorted Search

This example calls searchRecords with nine parameters, assigning the returned value to an instance of Result and calling Result.getCode to assign the error code to a variable which is then conditionally checked to determine the code to execute.

If the search was not successful, the code calls throwError to display an error dialog.
If the search was successful and returned records, the code creates an instance of ParametersIterator. In a while loop, the code calls ParametersIterator.next method to get an instance of Parameters from which it then extracts the name and number keys.
// Next searchRecords call will sort the results, first by city
// (ascending) and then by number (descending). It will return
// first 200 records matching the criteria.

Result result = Functions.searchRecords("ACCOUNT", "record_id,city,name,number",
"name contains 'California'", "city", "asc", "number", "desc", 0, 200);
int resultCode = result.getCode();
if(resultCode < 0)
{
    // Some error happened.
    String msg = "Account could not be retrieved";
    Functions.debug(msg + ":\n" + result.getMessage());  // Log details
    Functions.throwError(msg + ".");                     // Error dialog
}
else if(resultCode == 0)
{
  // No records found
  // Take action according to your business logic
}
else
{
   //Records retrieved successfully
   ParametersIterator iterator = result.getIterator();
   while(iterator.hasNext())
   {
      Parameters params = iterator.next();
      String accountID = params.get("record_id");
      String accountName = params.get("name");
      String number = params.get("number");
      // Take action according to your business logic.
   }
}
Example: Searching Audit Logs
Audit Logs can be searched using the getRecord and searchRecord APIs
Learn more: Reference documentation for Audit Log fields
How it Works
Provide search criteria to search multiple audit log records and retrieve a list of audit log fields. The getRecord and searchRecords APIs are used in actions that Invoke a Java Method.
Syntax
public static Result Functions.searchRecords(String objectId, String fields, String criteria)
objectId
log
fields
record_id, ownerid, type, operation, object, date_created, description, type_code
recordId
Audit log record Id
Return
Result object
Audit Log Example Based on Filtering Criteria
Result result = Functions.searchRecords("log", "record_id,type,operation,"+
"object,description", "(record_id starts with'65') and (description contains 'audit')");

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
}
else if(resultCode == 0)
{
   // No records found. Take action according 
   Functions.throwError("No results returned from search");
}
else
{
  //Records retrieved successfully
  ParametersIterator iterator = result.getIterator();
  while(iterator.hasNext())
  {
    Parameters params = iterator.next();
    String desc= params.get("description");
    String type= params.get("type");
    String obj = params.get("object");
    String record_id = params.get("record_id");
    String operation = params.get("operation");                   
 
    // Take action according to your business logic
  }
}
Learn more: Reference documentation for Audit Log fields