Difference between revisions of "Invoke a method in a Java Class"
imported>Aeric |
Wikidevuser (talk | contribs) Tag: Manual revert |
||
(12 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
===Method Signature=== | ===Method Signature=== | ||
You can select any method defined in a [[Classes|Java class]], as long as it has the required signature: | You can select any method defined in a [[Classes|Java class]], as long as it has the required signature: | ||
:: <tt>public void someMethod(com.platform.api.Parameters | :: <tt>public void someMethod(com.platform.api.Parameters inParams)</tt> | ||
:: <tt> throws Exception {</tt> | |||
::: <tt> ...</tt> | |||
;Considerations: | ;Considerations: | ||
:* The ''incoming parameters'' are the name/value pairs contained in the Parameters object.<br>The most commonly accessed parameters are: | |||
::: <tt>String objectID = inParams.get("object_id");</tt><br><tt>String recordID = inParams.get("id");</tt> | |||
:* The <tt>throws Exception</tt> is not a necessary part of the signature, but is generally required for any method that is doing real work. (If an invoked API generates an exception, the exception percolates back up to the top-level handler, which displays the message.) | |||
:* The method can be either a static class method or an instance method. An instance of the object is created before making the call. | :* The method can be either a static class method or an instance method. An instance of the object is created before making the call. | ||
:* The method must be public, return void, and take a single <tt>Parameters</tt> argument. | :* The method must be public, return void, and take a single <tt>Parameters</tt> argument. | ||
''Learn more:'' [[Incoming Method Parameters]] | |||
===Example: Validate Incoming Data=== | ===Example: Validate Incoming Data=== | ||
{{:HowTo:Use a Java Method to Validate Incoming Data}} | {{:HowTo:Use a Java Method to Validate Incoming Data}} |
Latest revision as of 10:49, 28 July 2023
Invoking a Java method lets you use the Java APIs to interact with the platform, and do anything else that you can accomplish using Java code.
Method Signature
You can select any method defined in a Java class, as long as it has the required signature:
- public void someMethod(com.platform.api.Parameters inParams)
- throws Exception {
- ...
- Considerations
-
- The incoming parameters are the name/value pairs contained in the Parameters object.
The most commonly accessed parameters are:
- String objectID = inParams.get("object_id");
String recordID = inParams.get("id");
- String objectID = inParams.get("object_id");
- The incoming parameters are the name/value pairs contained in the Parameters object.
- The throws Exception is not a necessary part of the signature, but is generally required for any method that is doing real work. (If an invoked API generates an exception, the exception percolates back up to the top-level handler, which displays the message.)
- The method can be either a static class method or an instance method. An instance of the object is created before making the call.
- The method must be public, return void, and take a single Parameters argument.
Learn more: Incoming Method Parameters
Example: Validate Incoming Data
In addition to the data validation that is part of every Custom Object, you can use Java methods to do even more sophisticated validation.
In this example, the method ensures that the credit number is in the proper format -- exactly 16 digits, with no characters or spaces. The validation method is designed for the Orders object in the Sample Order Processing System, which has field named credit_card_number. Such a method could be invoked in a Rule Action that is triggered when a new record is created.
This is an important technique for more sophisticated validations. In this case, for example, a credit card number is too big to use a numeric field. The solution is to use a text field, use a data policy to parse the data, and throw an exception in the event of an error.
This code throws an exception if the format isn't valid. The exception will abort the transaction before it is committed.
String ccn_field = "credit_card_number"; public void validate_order(Parameters requestParams) throws Exception { String errMsg = "Invalid credit card number format. Needs 16 digits."; String ccn = (String) requestParams.get(ccn_field); if ((null != ccn) && (ccn.length() != 16)) { throw new Exception(errMsg); } for (int i=0; i<=15; i++) { char c = ccn.charAt(i); if (c < '0' || c > '9') { throw new Exception(errMsg); } } // Format validated return; }