Difference between revisions of "Result Class"
imported>Aeric |
imported>Aeric |
||
Line 18: | Line 18: | ||
These are the rules that the record handling calls follow for setting the error code: | 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. | :*''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: | :* ''Most'' record handling calls set the error code to zero (0) if successful. The exceptions to this rule are: | ||
** <tt>getRecord</tt> sets the error code to one (1) if successful | :** <tt>getRecord</tt> sets the error code to one (1) if successful | ||
** <tt>searchRecord</tt> sets the error code to the number of records retrieved if successful | :** <tt>searchRecord</tt> sets the error code to the number of records retrieved if successful | ||
'''Signature of the Method''' | '''Signature of the Method''' |
Revision as of 21:08, 29 September 2011
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 <syntaxhighlight lang="java" enclose="div">int getCode()</syntaxhighlight>
- 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.
<syntaxhighlight lang="java" enclose="div"> 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.
} </syntaxhighlight>
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 <syntaxhighlight lang="java" enclose="div">String getID()</syntaxhighlight>
- 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.
<syntaxhighlight lang="java" enclose="div"> 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.
} </syntaxhighlight>
getMessage
Gets the message that indicates success or failure.
Signature of the Method <syntaxhighlight lang="java" enclose="div">String getMessage()</syntaxhighlight>
- 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.
<syntaxhighlight lang="java" enclose="div"> 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.
} </syntaxhighlight>
getIterator
Gets a ParametersIterator object after a searchRecords call.
Signature of the Method <syntaxhighlight lang="java" enclose="div">ParametersIterator getIterator()</syntaxhighlight>
- 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.
<syntaxhighlight lang="java" enclose="div"> 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 }
} </syntaxhighlight>
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.
<syntaxhighlight lang="java" enclose="div"> 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
} </syntaxhighlight>