BADI using Enhancement Framework

BADI using Enhancement Framework
Neeraj Batra
Business Card
Company: Infosys Technologies Limited
Posted on Mar. 18, 2009 06:27 AM in ABAP

Introduction

During the course of the following document; the reader can understand how the BAdI concept fits in to the new Enhancement Framework. We will create a BAdI definition in the first part and in the later part we will implement the BAdI. Basic knowledge about the new enhancement framework will help in understanding this document better.

Who creates the BAdI definition

Please understand that most of us (Technical SAP – ABAP) will seldom have the requirement to create a BAdI definition. Unless we are working on a ‘product development’ in collaboration with SAP itself; we will be only dealing with the implementation part. Though I mention above that we will seldom be defining a BAdI; it is a very important to understand the BAdI declaration; to appreciate the concept of Enhancement Framework as a whole. 

 --- BAdI definition is part of product development; majorly.

 

The requirement

 

Ok let us assume we are working on a SAP product which has a requirement to perform currency conversion (wrt USD) based on the country provided by the user. 

Let us write a program for it.

image

The program simply has a parameter country. To output the converted value; I would require the conversion rate.  And I leave it to the customer to enhance the application and provide the conversion rate. 

This is where I use a BAdI.

 

First step -> creating the Enhancement Spot!! 

People familiar with source code enhancements can appreciate the fact that the first step for an enhancement implementation is an Enhancement Spot. Same is the case here; we need to create an enhancement spot which will act as a container for the BAdI definition 

So for the time being let us forget the ABAP report and move to SE80; to create the container (Enhancement Spot).

Select Local Objects -> Enhancements -> Enhancement Spots -> Create (right click).  

 --- Enhancement spot acts as a container for a BAdI definition!!   

image"

Note – If you questioned the usage of local objects in the previous step; please be aware that; strangely; the section ‘Enhancement Info Systems’ in SE80 does not let you create a new enhancement spot. The ‘Local’ issue is however taken care of; when upon saving; we are prompted to enter the package. 

Create a new BAdI definition as shown below.

 

image"

image

Multiple Use

Currently; in our requirement we are performing only one operation on the amount based on the conversion rate. However; say in China; we want to perform another operation ‘Enhance Currency’; we would be required to use the option ‘Multiple Use’. Please be aware that the order of occurrence of the various implementations of a multiple use BAdI cannot be ascertained!!

--- Multiple Use -> Series of operations (BAdI implementations); not following specific order of occurrence though!!  

For the current requirement however we will be content without the multiple use option.

Interface

The next step is to define an interface. Just pull the arrow down in the left side section and mention the interface name and press the ‘change interface’ button.

image

 --- ‘Interface’ differentiates a BAdI from explicit  enhancement option!!

image

The screen above is what differentiates an explicit enhancement option from a BAdI. BAdI is like a controlled explicit enhancement point. Controlled; why? Because; the developer decides the parameters that the implementer will have access to.

 

Whereas; in explicit enhancement option the implementer can play around with all the variables of the source code; often inviting unplanned behavior of the system. 

 

In other words BAdI is definitely less dangerous than explicit enhancement option!! 

 

The above might lead you in to thinking that the developer should always follow the best path and create an option for a BAdI; why give the option of explicit enhancement points/sections. BUT; there are numerous cases wherein the developer does not know which variable the user might want to use and in what way (yes, it happens!!) This is where explicit enhancement options are inevitable. 

 

Also, of course there are cases wherein there are no parameters required and no operations to be performed on them; but simply a piece of code needs to be added to the source code -> Explicit Enhancement Option is the answer; no use of BAdI here !! 

 

Create a method ‘Conversion’ as shown below.

image

Filter -> Remember the ‘country-specific’ functionality!!

The conversion factor would vary with the country the user enters; therefore we require different implementations to be called based on the ‘filter’ country. The option of providing the scope of a filter lies with the developer defining the BAdI. (Then only it will be available to the implementer!!) The following screenshot shows how to do the same; go to BAdI Definition -> Right Click -> Create filter

image

Enter the Filter Name and the Filter Type. In this case; the filter type would be Character. 

--- Filter facilitates usage of multiple implementations in a single usage  BAdI (Only one is executed though!!)

image

This is all that is required to provide the option of a filter!!

Summary till now!!

We have created an Enhancement Spot which holds the BAdI definition. Our BAdI definition defines the parameter that the implementers can pass/retrieve/change to the method in the BAdI. Also, we have left an option of a Character-Type filter.

Back to the report… 

We still do not have the link to the BAdI in the source code of the report. The following coding does just that!!

 

image

What happened? 

If you thought the job was over for the developer; please look at the following dump. The system raises an exception ‘CX_BADI_NOT_IMPLEMENTED’ if it does not find any implementations for the BAdI. In our case; we are still developing the application; hence the obvious dump.

image

 --- Fallback Class is useful in case no active implementation exists!!

The solution for the Dump -> Fallback Class

Fallback Class is the savior when there is no active implementation for the BAdI definition. It the current scenario; it can be imagined as the case wherein the user wants to convert USD to USD itself!! To create the fallback class; just got to the BAdI definition screen and click check-box ‘Call fallback if no implementation is executed’. Enter the class name and press enter.

image

In the method of the fallback class we will pass the amount as the answer itself.

That is it!!

 

image

 

This completes the BAdI Definition part of the document!! 

And now the implementation… 

Well the product development is over; now I am the customer of SAP; and want to use the hook provided above to fulfill my specific requirement. So; let us implement the BAdI now!!  

--- Implementation is post product development; of course!! 

Enter the Enhancement Spot via SE80; of course in Display mode (SAP created it!!). Right click on the BAdI definition and click Create BAdI definition. 

image

You will be prompted to enter the implementing class for the BAdI implementation created in the previous step. Here; you can use the example class to make life easier (The fallback class we created is an example of ‘Example Class’).

image

 image

Enter the desired code in the method of the implementing class; for our requirement; I am using conversion factor as 49 (Rupee depreciation / appreciation a separate issue!!)

Activate.

image

 

Filter value: We are still to add the filter value to this implementation. Go to the implementation and add the filter.

 

image

 

 

Similarly, create n implementations for n countries; giving the filter value as desired. Here; I created one for UK.

 

Let us test the report… 

Case 1 Data: -          Country IN; Amount 100

image

 

Case 2   Data:-           Country UK, Amount 100

 

image

 

A freebie in The End… 

Though not related; I will demonstrate creating an explicit enhancement point in the source code as a freebie for this document; yipee!! 

Simply go to the report Right Click -> Enhancements  -> Create

image

Enter the Enhancement Point and Enhancement Spot.

image

That is all. Yes; easier than BAdI definition but definitely more dangerous!!

Conclusion 

Hopefully the document through the simple example clears the BAI concept; both from definition and implementation point of view.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值