How do I know I'm ready for this?
This is number 6 in a series of blogs on ABAP OO for workflow, so you it would be a really good idea to make sure you have worked through the first 5 blogs. The examples we'll use here continue on from those we used in the earlier blogs.
Here's the list of the earlier blogs:
- Why use ABAP OO with Workflow?
- Getting started with ABAP OO for Workflow ... using the IF_WORKFLOW interface
- Using ABAP OO with Workflow Tasks
- Raising ABAP OO events for Workflow
- Using ABAP OO attributes in Workflows and Tasks
What are functional methods?
A functional method is a method that returns a single result, i.e. a method that has a Returning parameter. Typically a functional method calculates a value, e.g. you might use a functional method to return the number of entries in a table, or the formatted name of an employee or whatever. The method can have importing parameters, but does not have any other exporting parameters.
Here's a simple example using the same ABAP Class we used for the exercises in the previous blogs. This is a static method that retrieves a list of plants relevant for a nominated country. Here are the parameters:
And the method code - as you can see it's a very simple example. METHOD plants_of_country.
SELECT werks FROM t001w
INTO TABLE et_plants
WHERE land1 = iv_country.
What's the equivalent of functional methods in BOR?
For those who are used to coding business objects (BOR) then functional methods replace virtual attributes (a calculation derived from the object instance). Virtual attributes, and their replacement functional methods, are particularly useful where you have a value that can be derived from the current object instance, but there is enough of a performance cost with deriving the value that you don't want to derive the value every time you construct the instance.
One advantage of functional methods over virtual attributes is that you can pass importing parameters. You can derive your calculation from the class instance (assuming you are using an instance method of course) and the importing parameters. However its a fairly simple mechanism so its easy to use this for simple parameters that can be hardcoded into the workflow, but you may still need to call the functional method as the main method of a normal task if you want to dynamically set the parameters.
How can I use functional methods in workflows?
You can use a functional method in much the same way as an object attribute. For example: in a container operation, or as the source of a binding.
Here's an example where a functional method is used in container operation:
When selecting your functional method from the drop down look for the "methods" or "functional methods" section within the object. For example this is what the drop down on the Expression field of the Container Operation above looks like:
This is the format for passing importing parameters. &ZCL_PLANT.PLANTS_OF_COUNTRY(IV_COUNTRY='DE')&
If you have multiple parameters, just separate them with an ampersand (&).
Note: You can't use functional methods with importing parameters directly in logical expressions as yet (you can select them but an error is returned) - as the condition editor does not provide a way to enter the parameter values. So in this case, use your functional method in a container operation first, then use the derived container element in your logical expression.
How can I use functional methods in tasks?
You can use a functional method in much the same way as an object attribute in a task, for example in the subject text of a task or in the binding to a method, with one exception:
You can't call a functional method directly from the long text of a task.
This is a SAPScript restriction not a workflow restriction - i.e. SAPScript doesn't know how to call a functional method to build the text. However this is not a major problem as you can always create an import container element to hold the result of the functional method in your task, and then in the binding source the container element from your functional method. The example below gives an idea of what such a binding would look like.
Can I use a functional method in a task like other methods?
Yes of course you can use a functional method as the main method of a task, and then it behaves exactly like any other method called from a task. This could be helpful if you need to handle exceptions for that particular workflow, or if it's simply easier to build that way as for this particular workflow you need to pass many/complex importing parameters.
How do I decide whether to use a functional method or create a task?
The decision is much the same as for virtual attributes in the BOR repository, i.e.
- Do you need to return more than one value? - If so use a task (of course this task will call a normal method - i.e. one with multiple exporting parameters)
- Do you need to handle exceptions? - If so use a task
- Do you only need to return a single value and exceptions are unlikley or irrelevant (will be ignored, or will be checked in a subsequent step)? - If so use a functional method
Can I use functional methods for BOR objects?
You may notice that functional methods (i.e. methods with the special "result" parameter) can also be selected. However I have personally found them a little unreliable in practice (they tend to throw errors or fail to pass data). A safer approach would be to link the BOR object to an equivalent ABAP Class, and handle the call to the BOR method inside an ABAP Class method. Linking BOR objects to ABAP Classes is the topic of our next blog.