Creating and Implementing Consistency Checks in Business Object Builder
Creating and Implementing Consistency Checks in Business Object Builder
Level of complexity:
Time required for completion:
Author:
Company:
Created on:
Beginner
15 minutes
Thea Hillenbrand
SAP AG
15 January 2014
TABLE OF CONTENTS
BEFORE YOU START ...................................................................................................................................... 3
Objectives ......................................................................................................................................................... 3
Prerequisites .................................................................................................................................................... 3
Systems, Releases, and Authorizations ............................................................................................................ 3
Knowledge ......................................................................................................................................................... 4
ENHANCING THE STRUCTURE OF A BUSINESS OBJECT NODE ............................................................. 4
Procedure ......................................................................................................................................................... 4
Launch the Business Object Builder (BOB)....................................................................................................... 4
Launch the test tool ........................................................................................................................................... 6
Result ................................................................................................................................................................ 7
ADD A VALIDATION ........................................................................................................................................ 7
Prerequisites .................................................................................................................................................... 7
Procedure ......................................................................................................................................................... 7
Start the Wizard to Create Consistency Validations .......................................................................................... 7
Define Name and Description of the Validation ................................................................................................. 8
Define the Implementing Class .......................................................................................................................... 9
Define the Request nodes ............................................................................................................................... 10
Define the Behavior ......................................................................................................................................... 11
Finishing the wizard ......................................................................................................................................... 12
Implement the Validation ................................................................................................................................. 12
Result .............................................................................................................................................................. 14
TESTING THE VALIDATION .......................................................................................................................... 14
Procedure ....................................................................................................................................................... 14
Start Business Object Test Environment ......................................................................................................... 14
Create an instance of ZD_SALES_QUOTE .................................................................................................... 14
Result .............................................................................................................................................................. 15
The tutorial starts with the business object created in the Getting Started with Business Object Processing
Framework. It consists of the ROOT node with minimal header information, like QUOTE_ID and the ITEM
node with position data like PRODUCT_ID, quantity and price information. The goal is to allow entering a
discount between 0 and 100 percent for each item. Therefore we need an additional field (DISCOUNT) in the
ITEM database table and a validation check for the item node (CHECK_DISCOUNT).
Prerequisites
To be able to perform the tutorial, make sure the following prerequisites are fulfilled.
Systems, Releases, and Authorizations
BOPF is part of the Business Suite Foundation Layer and, therefore, included in the following SAP
Business Suite releases:
SAP Business Suite EHP5, SP11
SAP Business Suite EHP6, SP05
SAP Business Suite EHP7, all SP
To create a consistency validation, your SAP user requires the developer authorization profile
(S_DEVELOP authorization object)
To implement this example, you need the business object created in the tutorial Getting started with
Business Object Processing Framework.
Knowledge
Basic knowledge in ABAP OO
Experience with DDIC tools
ENHANCING THE STRUCTURE OF A BUSINESS OBJECT NODE
In this step, you will enhance the ITEM node of the Business Object (BO) SALES_QUOTE. This Business
Object follows the semantics of the sales quote based on the NetWeaver Enterprise Procurement Model
(EPM).
Procedure
Launch the Business Object Builder (BOB)
Transaction BOB provides the design time for custom business objects as well as business object
enhancements.
On the left side of the initial screen you see three categories of BOs that are currently available in your
system:
Custom Business Objects: BOs created by the customer in their system
SAP Business Objects: Extensible BOs that are delivered by SAP
Business Object Enhancement: BOs that are enhancements to an existing business object.
Select the Business Object ZD_SALES_QUOTE.
Fig. 3: SALES_QUOTE resulting from Getting started with Business Object Processing Framework
Select the node ITEM and navigate to the definition of the persistent structure ZDS_ITEM_D in the ABAP
data dictionary.
In the ABAP Dictionary, switch to edit mode and add the new component DISCOUNT with the data type DEC
length 6 decimals 2. In an enhancement scenario, define the new component in an append structure.
Fig. 5: Define the additional component either directly in the DDIC structure or as Append Structure
Save and activate the definition. Then go back to the Business Object Builder.
Launch the test tool
Back in transaction BOB, choose the
Test button and start the BOPF test tool. Create a new sales quote instance by choosing the Add Node
Instance button on the ROOT node.
Enter at least a QUOTE_ID and navigate to the ITEM node by choosing the navigation menu button.
You can now create an instance of the ITEM node by choosing the Add Node Instance button. The new
component DISCOUNT is visible and can be maintained.
Enter the data for the item node. If you play with the data in the DISCOUNT field, you can enter decimal
values greater than 100 or even negative values. Introducing the consistency check will be the task of the
next step.
Result
You have now enhanced the original ITEM structure by the DISCOUNT field. You can enter data, and also
store and retrieve it.
ADD A VALIDATION
In this step we will enhance the BO item node by adding a consistency validation. Consistency validations
check whether a node instance is consistent with respect to the consistency criteria imposed by the business
requirements. If inconsistencies exist, consistency validations can return messages that should be displayed
to the user. In our case the business requirement is that the discount has to be between 0 and 100 percent
and that the BO instances accept only values in this interval.
Prerequisites
The ITEM node of the BO is opened in the configuration view of the Business Object Builder.
Procedure
Start the Wizard to Create Consistency Validations
Open the context menu of the ITEM node in the node browser pane. Select the entry Create Consistency
Validation. Press Continue to go to the first step.
The aim of this validation is to check the input in the field discount of the ITEM node of the sales quote. We
therefore name the validation CHECK_DISCOUNT.
Continue to the next step.
Define the Implementing Class
At runtime, a validation is represented by an ABAP class, implementing the validation interface. In this step
you are able to define the name of that class.
For this demo we wont change the proposed name, but instead continue with the next step.
Define the Request nodes
At runtime, we want the validation to be executed only when the user has changed the discount. Therefore
we have to define the condition for the validation to be triggered. In our example, DISCOUNT is an attribute
of the ITEM node and the check has to be called when the node is updated.
Mark the ITEM node and the checkbox for Create and Update. The validation should not be executed if the
node instance is deleted this is not necessary from a business point of view.
10
11
As we have to assume in our scenario that the user will send the sales quote to the customer immediately
after having saved it, the data entered should be valid. So we set the radio button Return messages and
prevent saving.
Finishing the wizard
Finish the validation creation by choosing Complete. In the background, the system updates the BO
configuration and generates the implementing class of the validation.
Implement the Validation
The new validation is now visible in the Entity Browser pane of the BO configuration view. On the right, the
settings of the selected validation are shown. Double-click the name of the implementing class to navigate to
the class builder. Open the empty implementation of method /BOBF/IF_FRW_VALIDATION~EXECUTE and
provide the following source code.
method /BOBF/IF_FRW_VALIDATION~EXECUTE.
DATA lt_item
TYPE
zdt_item. " Combined table type
DATA lr_item
TYPE REF TO zds_item. " Combined structure type
DATA ls_incons_item_key LIKE LINE OF it_key.
DATA ls_error_location
TYPE /bobf/s_frw_location.
DATA lo_message
TYPE REF TO /bobf/cm_sepm_soq_messages.
12
io_read->retrieve(
EXPORTING
iv_node = zif_d_sales_quote_c=>sc_node-item
it_key = it_key
IMPORTING
et_data = lt_item ).
LOOP AT lt_item REFERENCE INTO lr_item
WHERE discount > 100 OR discount < 0.
" Collect the keys of the indonsistent item
ls_incons_item_key-key = lr_item->key.
APPEND ls_incons_item_key TO et_failed_key.
" Raise message
" 1.) Prepare error location info
ls_error_location-bo_key = zif_d_sales_quote_c=>sc_bo_key.
ls_error_location-node_key = zif_d_sales_quote_c=>sc_node-item.
ls_error_location-key = lr_item->key.
INSERT zif_d_sales_quote_c=>sc_node_attribute-item-discount
INTO TABLE ls_error_location-attributes.
" 2.) Create message
CREATE OBJECT lo_message
EXPORTING
textid
= /bobf/cm_sepm_soq_messages=>gc_invalid_discount_rate
severity = /bobf/cm_sepm_soq_messages=>co_severity_error
symptom = /bobf/if_frw_message_symptoms=>co_bo_inconsistency
ms_origin_location = ls_error_location.
" 3.) Add message to message object
if eo_message is NOT BOUND.
eo_message = /bobf/cl_frw_factory=>get_message( ).
ENDIF.
eo_message->add_cm( lo_message ).
ENDLOOP.
endmethod.
Hint: if you copy the source code directly into the ABAP editor, the formatting will be lost. Copying it into WordPad, on the
other hand, preserves at least the line breaks. This format can then be copied into the ABAP editor.
In most cases, the first step is to read the data of those node instances that need to be checked. For this
purpose, the RETRIEVE method of the Internal Access Object IO_READ can be used; it is provided by the
framework as an importing parameter. The keys of the node instances that need to be processed and thus
need to be read are handed over by another parameter called IT_KEY. To define the node from where we
want to read data, we use the appropriate constant from the Constants Interface of the BO. (Remember that
you have to use the specific Constance Interface of your business object that you created in the first task).
The result of the call is stored in a variable, typed with the Combined Table Type of the read node.
We execute the check for each item. If an item fails, we create a message and add it to the message object
EO_MESSAGE. As message class you can use the sample class of EPM
/BOBF/CM_SEPM_SOQ_MESSAGES. If the class does not exist in your system, create a new one, with
/BOBF/CM_FRW as the superclass.
13
When instantiating the message we provide not only a text and severity level, but also further information
about the actual error location. This data can be used by the UI to indicate the actual position of the
inconsistency to the user.
Activate your code and navigate back to the configuration view of the Business Object Builder.
Result
You are now finished with the implementation of the validation. The validation is always executed when the
user changes the items. It checks if the discount entered is between 0 and 100. The user cannot save the
item if the check fails.
TESTING THE VALIDATION
We are now going to test the validation.
Procedure
Start Business Object Test Environment
In the configuration view of BOB, choose the Test button in the toolbar.
Create an instance of ZD_SALES_QUOTE
In the Node Instance Table pane, select Add Node Instance from the toolbar and enter the header data.
Then navigate to the item node as described in the previous chapter. If you now enter invalid data, you get
an error message.
If you want to save the data, the framework prevents the save and sends an error message.
14
If you correct the data, the application message disappears and the framework saves the data.
Result
Within a few minutes we have verified that we have implemented the validation correctly, without writing any
test code. Of course, this does not replace an automated test, but with the help of the Business Object Test
Environment you get direct feedback as to whether your Business Object works correctly or not.
We have now gone through the fundamentals of creating and testing validations. I hope you are motivated to
try out more things with BOPF, as there is much more that can be discovered. Stay tuned for further articles
about our framework.
15
www.sap.com