Result Class

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

An instance of the Result class is returned by many of the Java APIs. You can check data in the Result object and take action appropriately.

For example, the Result object returns an error message if a call fails and you can display that error message or a custom error message in the user interface.

Methods

The Result class has following methods.

getCode

Gets the error code for record handling calls such as addRecord, updateRecord, deleteRecord, and so on. For the searchRecords and getRecord calls, this call gets the number of records returned.

These are the rules that the record handling calls follow for setting the error code:

  • All record handling calls set the error code to negative one (-1) if not successful.
  • Most record handling calls set the error code to zero (0) if successful. The exceptions to this rule are:
    • getRecord sets the error code to one (1) if successful
    • searchRecord sets the error code to the number of records retrieved if successful

Signature of the Method

int getCode()
Example
This example calls addRecord, 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.
Parameters myAccount = Functions.getParametersInstance();
myAccount.add("name", "Acme Solutions");
Result result = Functions.addRecord("ACCOUNT", params);
int returnCode = result.getCode();
if(returnCode < 0)
{ 
    String msg = "Account could not be added";
    Functions.debug(msg + ":\n" + result.getMessage());  // Log details
    Functions.throwError(msg + ".");                     // Error dialog
}
else
{
  // take other actions according to 
  // your business logic.
}

getID

Gets the record identifier set by addRecord and generateDocument. When these calls fail, the record identifier is an empty string.

Signature of the Method

String getID()
Example
This example calls addRecord, 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 call was successful, the code calls getID to extract the record identifier.
Parameters myAccount = Functions.getParametersInstance();
myAccount.add("name", "Acme Solutions");
Result result = Functions.addRecord("ACCOUNT", myAccount);
int returnCode = result.getCode();
if(returnCode < 0)
{
    String msg = "Account could not be added";
    Functions.debug(msg + ":\n" + result.getMessage());  // Log details
    Functions.throwError(msg + ".");                     // Error dialog
}
else
{
  String id = result.getID();
  // use the ID to addTask, addActivity or 
  // add any other related object.
}

getMessage

Gets the message that indicates success or failure.

Signature of the Method

String getMessage()
Example
This example calls addRecord, 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 call was not successful, the code calls getMessage to extract the message string.
Parameters myAccount = Functions.getParametersInstance();
myAccount.add("name", "Acme Solutions");
Result result = Functions.addRecord("ACCOUNT", params);
int returnCode = result.getCode();
if(returnCode < 0)
{
    String msg = "Error adding Acme Solutions account";
    Functions.debug(msg + ":\n" + result.getMessage());  // Log details
    Functions.throwError(msg + ".");                     // Error dialog
}
else
{
  String id = result.getID();
  // use the ID to addTask, addActivity or 
  // add any other related object.
}

getIterator

Gets a ParametersIterator object after a searchRecords call.

Signature of the Method

ParametersIterator getIterator()
Example
This example calls searchRecords, 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 call was successful and records were returned, the code creates an instance of ParametersIterator. In a while loop, the code calls the ParametersIterator.next method to get an instance of Parameters from which it then extracts the record identifier.
Result result = Functions.searchRecords("ACCOUNT", "record_id,name, number,city", "name starts with 'Acme'");
int returnCode = result.getCode();
if(returnCode < 0)
{
    String msg = "Error searching Account records";
    Functions.debug(msg + ":\n" + result.getMessage());  // Log details
    Functions.throwError(msg + ".");                     // Error dialog
}
else if(returnCode == 0)
{
  Functions.throwError("No records found");
}
else
{
  ParametersIterator iterator = result.getIterator();
  while(iterator.hasNext())
  {
    Parameters myAccount = iterator.next();
    String id = myAccount.get("record_id");
    // execute your other business logic
  }
}

getParameters

Creates a Parameters object.

Signature of the Method

Parameters getParameters()
Example
This example calls getRecord, 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 call was successful and records were returned, the code creates an instance of Parameters and extracts the name.
String accountID = "";
// Some code to populate accountID variable
Result result = Functions.getRecord("ACCOUNT", accountID);
int returnCode = result.getCode();
if(returnCode < 0)
{
    String msg = "Error retrieving Account record";
    Functions.debug(msg + ":\n" + result.getMessage());  // Log details
    Functions.throwError(msg + ".");                     // Error dialog
}
else
{
  Parameters params = result.getParameters();
  String accountName = params.get("name");
  // execute your other business logic based on name
}