Difference between revisions of "Result Class"
imported>Aeric |
imported>Aeric |
||
(5 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
:* '''If the return code is greater than zero''', the value is the number of records in the result set. | :* '''If the return code is greater than zero''', the value is the number of records in the result set. | ||
:: In that case, there are two options: | :: In that case, there are two options: | ||
::* '''If the API expected to return a single record''', the [[#getParameters|getParameters]] method (below) returns a [[Parameters Class|Parameters]] object that contains the record fields. | ::* '''If the API expected to return a single record''', as in a [[Java_API:Record_Handling#getRecord|getRecord]] operation, the [[#getParameters|getParameters]] method (below) returns a [[Parameters Class|Parameters]] object that contains the record fields. | ||
::* '''If the API expected to return multiple records''', as in an [[execSQL]] query or a [[searchRecords]] operation, then the [[#getIterator|getIterator]] method (below) is used to cycle through the list of Parameters objects contained in the Result set (one per record). (In that case, the <tt>getParameters</tt> method returns null.) | ::* '''If the API expected to return multiple records''', as in an [[execSQL]] query or a [[searchRecords]] operation, then the [[#getIterator|getIterator]] method (below) is used to cycle through the list of Parameters objects contained in the Result set (one per record). (In that case, the <tt>getParameters</tt> method returns null.) | ||
Line 22: | Line 22: | ||
==== getCode ==== | ==== getCode ==== | ||
Gets the error code for record handling calls such as [[#addRecord|addRecord]], [[#updateRecord|updateRecord]], [[#deleteRecord|deleteRecord]], and so on. For the [[ | Gets the error code for record handling calls such as [[#addRecord|addRecord]], [[#updateRecord|updateRecord]], [[#deleteRecord|deleteRecord]], and so on. | ||
For the [[searchRecords]] and [[getRecord]] calls, the code indicates the number of records returned. | |||
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: | ||
Line 46: | Line 48: | ||
{ | { | ||
String msg = "Account could not be added"; | String msg = "Account could not be added"; | ||
Logger.info(msg + ":\n" + result.getMessage(), "Accounts"); // Log details | |||
Functions.throwError(msg + "."); | Functions.throwError(msg + "."); // Error message | ||
} | } | ||
else | else | ||
Line 76: | Line 78: | ||
{ | { | ||
String msg = "Account could not be added"; | String msg = "Account could not be added"; | ||
Logger.info(msg + ":\n" + result.getMessage(), "Accounts"); // Log details | |||
Functions.throwError(msg + "."); | Functions.throwError(msg + "."); // Error message | ||
} | } | ||
else | else | ||
{ | { | ||
String id = result.getID(); | String id = result.getID(); | ||
// use the ID to | // use the ID when updating the record or adding a record to a related object | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 106: | Line 107: | ||
{ | { | ||
String msg = "Error adding Acme Solutions account"; | String msg = "Error adding Acme Solutions account"; | ||
Logger.info(msg + ":\n" + result.getMessage(), "Accounts"); // Log details | |||
Functions.throwError(msg + "."); | Functions.throwError(msg + "."); // Error message | ||
} | } | ||
else | else | ||
Line 134: | Line 135: | ||
{ | { | ||
String msg = "Error searching Account records"; | String msg = "Error searching Account records"; | ||
Logger.info(msg + ":\n" + result.getMessage(), "Accounts"); // Log details | |||
Functions.throwError(msg + "."); | Functions.throwError(msg + "."); // Error message | ||
} | } | ||
else if(returnCode == 0) | else if(returnCode == 0) | ||
Line 172: | Line 173: | ||
{ | { | ||
String msg = "Error retrieving Account record"; | String msg = "Error retrieving Account record"; | ||
Logger.info(msg + ":\n" + result.getMessage(), "Accounts"); // Log details | |||
Functions.throwError(msg + "."); | Functions.throwError(msg + "."); // Error message | ||
} | } | ||
else | else |
Latest revision as of 22:25, 10 September 2013
An instance of the Result class is returned by many of the Java APIs.
- Considerations
-
- If the return code is less than zero, it is an error code.
- The Result object contains an error message that explains the error.
- If the return code is zero, the call succeeded, but no records are present.
- This value is returned by an add, update, or delete, or when no records match specified search criteria.
- If the return code is greater than zero, the value is the number of records in the result set.
- In that case, there are two options:
- If the API expected to return a single record, as in a getRecord operation, the getParameters method (below) returns a Parameters object that contains the record fields.
- If the API expected to return multiple records, as in an execSQL query or a searchRecords operation, then the getIterator method (below) is used to cycle through the list of Parameters objects contained in the Result set (one per record). (In that case, the getParameters method returns null.)
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, the code indicates 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"; Logger.info(msg + ":\n" + result.getMessage(), "Accounts"); // Log details Functions.throwError(msg + "."); // Error message
} 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"; Logger.info(msg + ":\n" + result.getMessage(), "Accounts"); // Log details Functions.throwError(msg + "."); // Error message
} else {
String id = result.getID(); // use the ID when updating the record or adding a record to a 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"; Logger.info(msg + ":\n" + result.getMessage(), "Accounts"); // Log details Functions.throwError(msg + "."); // Error message
} 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"; Logger.info(msg + ":\n" + result.getMessage(), "Accounts"); // Log details Functions.throwError(msg + "."); // Error message
} 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
- <syntaxhighlight lang="java" enclose="div">
Parameters getParameters() </syntaxhighlight>
- 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"; Logger.info(msg + ":\n" + result.getMessage(), "Accounts"); // Log details Functions.throwError(msg + "."); // Error message
} else {
Parameters params = result.getParameters(); String accountName = params.get("name"); // execute your other business logic based on name
} </syntaxhighlight>