• No results found

define and implement

In document April May Issue 2008 Sap Tips (Page 60-64)

knowledge of the new BAdIs is an absolute must for developers and consultants.

The aim of this article is to discuss the new Business Add-ins (BAdIs) provided by SAP since ECC 6.0. I will begin with an overview of the new features, and then discuss the characteristics of multiple- and single-use BAdIs. The structure of the new BAdIs will be examined and examples will be presented. How to implement the new BAdIs will be explained in detail.

These are some of the questions this article will address:

• What are the benefits of the new Business Add-ins?

How do they differ from classic BAdIs?

• What is the new Enhancement Framework, and what is an Enhancement Spot?

• What would a typical call for the new BAdI look like?

• How do single-use and multiple-use BAdIs differ?

• How are the new BAdIs implemented?

4 On ABAP / J2EE

A Tour of the New Business Add-Ins (BAdIs):

A Guide for SAP

®

Developers and Consultants

By Rehan Zaidi, Siemens Pakistan

These BAdIs are faster in performance and are easy to

define and implement.

SAPtips

Page 61

S AP tip s Journal April/ May 2008 V olume VI Issue 2

4 On ABAP / J2EE

BAdIs may be developed by SAP or may be custom defined. We refer to the developer of the BAdI or any other enhancement option as the Option provider. The methods and their signatures (or the parameters) for each respective method’s imports, exports, changes, or returns, are defined by the BAdI developer. The meth-ods defined in the BAdI interface are implemented by the BAdI implementer. The option provider (BAdI creator) defines what has to be done, but it is the implementer who carries out the actual implementation of the BAdI. Within the new BAdIs, the actions of the implementer are controlled by the option provider, who controls the class upon which the BAdI is based.

A developer must use BAdI when he or she wants a par-ticular procedure to be executed at a parpar-ticular position, but does not want to specify the exact content of that procedure. This might happen if the procedure details used are different among various potential implement-ers. It might also occur if the procedure details are so specific that the developer is not familiar with them, or if adding different implementations at a later stage.

Note: In addition to BAdIs, the new Enhancement Framework provides another option of enhancement known as Source Code Plug-ins. The possibility of attaching a source code plug-in is allowed when an enhancement point in the code is inserted by the option provider. The implementer may add her or his own code at this enhancement point. This has a few dis-advantages. Because the written code

is generally implemented later in the development, the option provider has no control over it. All the variables that are visible in the enhanced modulariza-tion unit may be accessed by the code within this source code plug-in. There-fore, the implementer is able to modify the value of any variables. This may be undesirable in some cases, and cannot be prevented by the option provider.

In this case, there is no control over the implementer. On the other hand, the BAdIs provide more control to the option provider.

A typical call of the new BAdI is shown in Figure 1.

The BAdI method is accessed using two new ABAP commands, namely GET BADI and CALL BADI. Let us take a closer look at the use of these two commands:

• The GET BADI statement is used to generate a new BAdI object, and assigns the reference to this object to the specified variable (in our example, g_BADI_ME59_

EXCLUDE). This is also termed as the handle to the BAdI object.

Prior to the GET BADI statement, the variable used must be defined based on the BAdI that is to be used.

In case you have defined filters for your BAdI, you may include suitable filter values in your GET BADI statement.

The GET BADI statement will be changed as shown below:

GET BADI myobject filter = filters.

• The CALL BADI statement is then used to call the rel-evant BAdI method. This method call may involve importing, exporting, and changing parameters (see Figure 1).

Multiple- and Single-Use Business Add-Ins The new Business Add-ins may either be Single-use or Multiple-use. These two types of BAdIs are quite dif-ferent. In single-use BAdIs, the business logic requires exactly one single calculation result. The program’s next steps may need this result; therefore at least one BAdI implementation is needed. In this case, exactly one result is required because there is no way to handle many return values. The system takes care that there is exactly one active implementation in a single-use BAdI.

Figure 1: A New BAdI Call

SAPtips

Page 62

S AP tip s Journal April/ May 2008 V olume VI Issue 2

4 On ABAP / J2EE

Therefore, the call of the method of a single-use BAdI functions like a method call.

Multiple-use BAdIs oper-ate very differently. Mul-tiple-use BAdIs are suitable for various activities that might even be conducted in different ways, in different implementations, during the execution of a program.

There may be different active implementations that can coexist simultaneously, but it is also possible that no active implementation might be present. The call of a method pertaining to a BAdI of this type may be similar to sending a message. The developer of the BAdI sim-ply does not care how many sets of code are executed.

Creating a New Business Add-In In order to better under-stand the structure of a BAdI, let us consider a simple example of creating a new BAdI. For simplicity’s sake, we define a Single-Use BAdI. The steps needed are shown here:

Step 1 – Creating an Enhancement Spot

As already mentioned, the new BAdIs reside within an Enhancement Spot. In this step, we create the Enhance-ment Spot. Call the transaction SE18. The screen shown in Figure 2 appears.

Enter a suitable name for the Enhancement Spot in the field provided, and click the

Create button. The dialog box appears, as shown in Figure 3.

Enter a description in the Short Text field and press Enter. The maintenance

screen for the Enhancement Spot then appears, as shown in Figure 4.

Step 2 – Creating the BAdI Definition

Click the Create button on the left. The dialog box appears, as shown in Figure 5.

Figure 2: Initial Screen of the BAdI Builder Transaction

Figure 3: Creating the Enhancement Spot

Figure 4: Enhancement Spot Maintenance Screen

Figure 5: Creating the BAdI Definition

SAPtips

Page 63

S AP tip s Journal April/ May 2008 V olume VI Issue 2

4 On ABAP / J2EE

Enter a suitable name and description of the Business Add-in, and press Enter.

This leads you to the screen shown in Figure 6.

In case you need to create a Multiple-Use BAdI, make sure the Multiple-use check-box is “on”.

Step 3 – Creating the Interface (and Methods) of the BAdI

This is an important step.

We need to create the inter-face in which the methods will reside. In order to create the BAdI interface, click the

icon shown in the left pane of the screen in Figure 6 (this is before the BAdI name).

The tree opens, as shown in Figure 7.

Double-click the Interface node. The right pane of the screen changes, as shown Figure 8.

Enter a suitable name for the interface in the field provid-ed, and press Enter. A dialog box appears prompting you for the creation of the inter-face name you have specified (see Figure 9).

Figure 6: BAdI Definition Screen

Figure 7: BAdI Interface Creation

Figure 8: BAdI Interface Screen

Figure 9: Creating an Interface Name

SAPtips

Page 64

S AP tip s Journal April/ May 2008 V olume VI Issue 2

4 On ABAP / J2EE

Click the Yes button. This will lead you to the Interface Detail screen, as shown in Figure 10.

We will now define the method(s) of the BAdI interface.

• Enter the name of the method that you would like to create.

• Also specify whether the method is to be created as an instance method or static, and input the description in the field provided.

• Use the Parameter button to specify the parameters of your BAdI method.

In our example, shown in Figure 10, we created a method CAL_HR_ALLOWANCE, and gave it the Description

“Calculating the HR Allowance”.

After making the necessary steps, Save and Activate the interface by clicking the button, as well as the BAdI definition, and the Enhancement Spot.

The code of the program that calls our BAdI is shown here:

Note: In case no active implementation exists for your single-use BAdI, the above coding will raise an excep-tion, CX_BADI_NOT_IMPLEMENTED, and a runtime error will occur. One way of handling this is through the use of a fallback class. The method of the fallback class is called if no active implementation of the BAdI exists. The fallback class must be specified by using the relevant fields on the BAdI definition screen shown in Figure 6.

Implementing the New Business Add-In In this section, we will see how the Business Add-In cre-ated in the previous section is implemented. As already mentioned, a BAdI definition resides in an Enhancement Spot. Likewise, the implementation of the new BAdIs must reside within containers known as Enhancement Implementation. Enhancement Implementations must be created before implementing BAdIs.

A BAdI definition may have multiple BAdI implementa-tions. On the other hand, a BAdI implementation may relate to a single BAdI.

Figure 10: Interface Detail

data : mybadi type ref to Z_BADI_CAL_HR.

data : hr type betrg.

data : basic type betrg.

start-of-selection.

get badi mybadi.

call badi mybadi->cal_hr_allowance EXPORTING

basic = basic IMPORTING hr = hr.

A BAdI definition resides in

In document April May Issue 2008 Sap Tips (Page 60-64)