Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Creating Data for Customer defined fields using Extension BAPI:

What is an Extension BAPI?

Standard tables might not have all the fields that customer required. So, customer will be adding his
own set of fields to meet his requirement. These fields are technically called Customer-defined fields.

Standard BAPI’s hold only standard fields at compile time and we can only supply data for standard
fields. To fill the customer-defined fields using standard BAPI’s we go for Extension BAPI’s.

How this Extension BAPI works?

Every BAPI which is having EXTENSIONIN, EXTENSIONINX parameters is eligible for extension BAPI
category. We will work with these EXTENSIONIN structures to fill the data for custom fields &
EXTENSIONINX for update flags. Before proceeding further, we must ensure the below important steps
are implemented to achieve the appropriate result.

Consider ZDOC_FIELD1, ZDOC_FIELD2, ZDOC_FIELD3 are three custom fields which are appended to
standard table MARA.

Steps to be performed for Extension BAPI’s :

1. Append these 3 custom fields to BAPI_TE_MARA structure.

(The field names must be same as the custom field names in standard table MARA and the type of these
fields should be of type character in the structure)

2. Append these 3 custom fields to BAPI_TE_MARAX structure.

These structures are specified under EXTENSIONIN parameter of the BAPI_MATERIAL_SAVEDATA


documentation.

(Again, the field names must be same as the custom field names in standard table MARA and the type of
these fields should be of type BAPIUPDATE in the structure)

3. Implement logic to populate values into these custom fields or BAPI_TE_MARA,


BAPI_TE_MARAX with the key field value.
Finally append these structures BAPI_TE_MARA, BAPI_TE_MARAX to EXTENSIONIN, EXTENSIONINX
internal table respectively and pass them to BAPI.

An Extension structures only holds structure name and valueparts. In our scenario, the structure names
will be BAPI_TE_MARA, BAPI_TE_MARAX for EXTENSIONIN & EXTENSIONINX respectively. While coming
to valueparts, we need to populate our custom field values into valueparts based on the field length of
the fields which we are going to update based on offset.

So here, there will be four valueparts each having 240 characters of length. Once we fill the valuepart1
completely, we need to move on to valuepart2 and so on like that.

How to fill data into this valuepart?

If we consider the structure BAPI_TE_MARA, it has a total of 6 fields including custom fields and key
field.

Field name Field length

MATNR 18

COBJID 8

COTYPE2

ZDOC_FIELD1 10

ZDOC_FIELD2 5

ZDOC_FIELD3 8

So, as we are assigning our structure BAPI_TE_MARA to Extensionin Structure, the value part of the
extensionin structure should be filled with values of BAPI_TE_MARA structure. Since valuepart contains
240 characters’ length, the data is filled in the form of offset based on length of each field in
BAPI_TE_MARA.

With respect to our structure BAPI_TE_MARA, the field MATNR has 18 character’s length. So the first 18
characters of valuepart1 is reserved for MATNR. Starting from 0 to 18 places is reserved for MATNR.

gs_extensionin-valuepart1+0(18) = gs_bapi_te_mara-material.
Since we need to fill data for ZDOC_FIELD1, I won’t consider passing data for the next two fields (COBJID,
COTYPE) but their places are reserved in valuepart1 (We shouldn’t fill anything in their respective places
if we don’t want to update, they should be left blank). From 18th character in valuepart1, next 10 (8+2)
characters places are reserved for those two fields (COBJID, COTYPE).

So, 18 + 10 = 28

We are now at 28th position and as our next field is ZZFIELD1 & its length is 10 characters.

gs_extensionin-valuepart1+28(10) = gs_bapi_te_mara-zzfield1.

Like these we need to fill data for all the custom fields which need to updated in data base table.

We need to fill the EXTENSIONINX structure in the same manner as explained above.

And finally pass it to BAPI.

Source Code:

REPORT ZJP_BAPI_EXT.

* Data declarations

DATA : gs_headdata TYPE bapimathead,

gs_clientdata TYPE bapi_mara,

gs_clientdatax TYPE bapi_marax,

gs_return TYPE bapiret2.


DATA : gt_materialdescription TYPE TABLE OF bapi_makt,

gs_materialdescription TYPE bapi_makt.

DATA : gs_bapi_te_mara TYPE bapi_te_mara,

gs_bapi_te_marax TYPE bapi_te_marax,

gt_extensionin TYPE TABLE OF bapiparex,

gs_extensionin TYPE bapiparex,

gt_extensioninx TYPE TABLE OF bapiparexx,

gs_extensioninx TYPE bapiparexx.

* Populate the data

gs_headdata-material = '000000000000001592'. " Material number

gs_headdata-matl_type = 'FGTR'. " Material type

gs_headdata-ind_sector = 'B'. " Industry sector

gs_headdata-basic_view = 'X'. " Basic View

gs_clientdata-base_uom = 'BT'. " Base unit of measure

gs_clientdatax-base_uom = 'X'. " Indicator:Base UOM

gs_materialdescription-matl_desc ='000000000000001592'.

"Description for Material

gs_materialdescription-langu = 'EN'. " Language

APPEND gs_materialdescription TO gt_materialdescription.

CLEAR : gs_materialdescription.

* Populate Customer defined fields

gs_bapi_te_mara-material = '000000000000001592'.

gs_bapi_te_mara-ZDOC_FIELD1 = 'Field1'.

gs_bapi_te_mara-ZDOC_FIELD2 = 'F2'.
gs_bapi_te_mara-ZDOC_FIELD3 = 'F3'.

gs_extensionin-STRUCTURE = 'BAPI_TE_MARA'.

gs_extensionin-valuepart1+0(18) = gs_bapi_te_mara-material.

gs_extensionin-valuepart1+28(10) = gs_bapi_te_mara-ZDOC_FIELD1.

gs_extensionin-valuepart1+38(5) = gs_bapi_te_mara-ZDOC_FIELD2.

gs_extensionin-valuepart1+43(8) = gs_bapi_te_mara-ZDOC_FIELD3.

APPEND gs_extensionin TO gt_extensionin.

gs_bapi_te_marax-material = '000000000000001592'.

gs_bapi_te_marax-ZDOC_FIELD1 = 'X'.

gs_bapi_te_marax-ZDOC_FIELD2 = 'X'.

gs_bapi_te_marax-ZDOC_FIELD3 = 'X'.

gs_extensioninx-STRUCTURE = 'BAPI_TE_MARAX'.

gs_extensioninx-valuepart1+0(18) = gs_bapi_te_marax-material.

gs_extensioninx-valuepart1+20(1) = gs_bapi_te_marax-ZDOC_FIELD1.

gs_extensioninx-valuepart1+21(1) = gs_bapi_te_marax-ZDOC_FIELD2.

gs_extensioninx-valuepart1+22(1) = gs_bapi_te_marax-ZDOC_FIELD3.

APPEND gs_extensioninx TO gt_extensioninx.

* Call BAPI

CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'

EXPORTING

headdata = gs_headdata

clientdata = gs_clientdata

clientdatax = gs_clientdatax

IMPORTING

RETURN = gs_return
TABLES

materialdescription = gt_materialdescription

extensionin = gt_extensionin

extensioninx = gt_extensioninx.

IF gs_return-TYPE EQ 'S'.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

WAIT = 'X'.

ELSE.

CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

ENDIF.

You might also like