ABAP Development for Materials Management in SAP (第4页)

第 81 页 / 共 275 页

Customizing the Document Overview in Purchase Requisitions or Purchase Orders 3.2

EE To integrate a custom query in EXIT_SAPLMEQUERY_001, you first need to enhance

the META_TABLE table (see Table 3.11) with a custom entry. The field CLASSID is

used to identify the query later. You should begin the name with Z* to avoid

overlaps. You can also choose the name freely. The LABEL field contains the title

of the selection option as it should appear in the document view.

EE EXIT_SAPLMEQUERY_002 is run as soon as the user has made a selection. The

IM_CLASSID field contains the respective ID from the META_TABLE table. There-

fore, check which CLASSID has been chosen. You can then call the respective

query depending on this. Set the parameters according to Table 3.13.

Parameter Content

CH_QUERY_NAME Name of the query that you previously defined in

Transaction SQ01.

CH_USERGROUP Name of the user group assigned to the query.

CH_VARIANT Name of the variant via which the query is to be started.

CH_WORKSPACE The global work area is used with the value ‘X’; the local

work area is used without value.

CH_HANDLE So that the drag-and-drop function for the shopping cart

works, the system needs to know which type of document

is returned. This is indicated by the handle:

EE 0001: Purchase orders

EE 0002: Purchase requisitions

Table 3.13 Parameters for Calling a Query

1. Edit the ZXM02U10 include from EXIT_SAPLMEQUERY_001. Add a custom entry to

the META_TABLE table. You can find the complete coding of the exit following

this change in Listing 3.26.

*&-----------------------------------------------------------*

*& Include ZXM02U10

*&-----------------------------------------------------------*

*”*”Local interface:

*” TABLES

*” META_TABLE TYPE MMPUR_DATABL_META_TABLE

*”------------------------------------------------------------

DATA: ls_meta_table LIKE LINE OF meta_table.

AUTHORITY-CHECK OBJECT ‘S_TCODE’

81

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

Parameter Content
CH_QUERY_NAME Name of the query that you previously defined in Transaction SQ01.
CH_USERGROUP Name of the user group assigned to the query.
CH_VARIANT Name of the variant via which the query is to be started.
CH_WORKSPACE The global work area is used with the value ‘X’; the local work area is used without value.
CH_HANDLE So that the drag-and-drop function for the shopping cart works, the system needs to know which type of document is returned. This is indicated by the handle: EE 0001: Purchase orders EE 0002: Purchase requisitions

第 82 页 / 共 275 页

3 User Exits and BAdIs in Purchasing

ID ‘TCD’ FIELD ‘ME21N’.

IF sy-subrc NE 0.

* No authorization for purchase order, remove all

* selection variants except REQ_QUERY and REQ_BY_USER_QUERY

delete meta_table where classid ne ‘REQ_QUERY’ AND

classid ne ‘REQ_BY_USER_QUERY’.

ELSE.

* Insert new variant

ls_meta_table-classid = ‘Z_NEW_VAR’.

ls_meta_table-label = ‘Bestellungen Neu’.

append ls_meta_table to meta_table.

ENDIF.

Listing 3.26 Adding New Selection Variants

2. Now that you’ve included the new selection variant in the selection list, you

need to make sure that the associated query is also called when the user has

chosen the entry. Navigate to the EXIT_SAPLMEQUERY_002 user exit, and double-

click the ZXM02U11 include integrated there. Create the include if it doesn’t yet

exist.

3. If the IM_CLASSID parameter contains the ID Z_NEW_VAR, call your query by

transferring all of the parameters according to Table 3.13. Listing 3.27 contains

the coding corresponding to the sample data.

*&-----------------------------------------------------*

*& Include ZXM02U11

*&-----------------------------------------------------*

IF im_classid = ‘Z_NEW_VAR’.

* Name of query

ch_query_name = ‘ZME_BEST’.

* Name of the user group

ch_usergroup = ‘ZME_QUERY’.

* Variant for selection screen

ch_variant = ‘ZME_BEST_STD’.

* Handle: 0001 purchase orders

* 0002 – Purchase requisitions

ch_handle = ‘0001’.

* Global work area

ch_workspace = ‘X’.

ENDIF.

Listing 3.27 Calling the Query

82

© 2013 by Galileo Press Inc., Boston (MA)

第 83 页 / 共 275 页

4 User Exits and BAdIs in External

Services Management

External Services Management is in its broadest sense an enhancement to the typi-

cal purchase order to order services, to be rendered in the form of services and to

enter and accept services rendered in a service entry sheet. There are numerous

enhancements in this area; however, most only offer limited scope and functional-

ity and seldom require comprehensive descriptions. This chapter covers the more

extensive enhancements using simple examples — particularly those that make it

easier for the user to enter data.

4.1 Prepopulating Account Assignment for Service Lines

When service lines from assigned purchasing documents are entered, the standard

version displays a dialog window to the user (see Figure 4.1) to enter an account

assignment. If this account assignment only seldom or never changes for individ-

ual users, a prepopulation can be carried out via the user exit EXIT_SAPLMLSK_001

from the enhancement SRVESKN. If this account assignment is clearly specified, the

dialog window is no longer shown. This can save you a lot of time with document

entry.

Figure 4.1 Querying the Account Assignment for Service Lines

83

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

第 84 页 / 共 275 页

4 User Exits and BAdIs in External Services Management

In the following example, the user master should be read for the user entering the

data. If the key “ABT20” has been maintained as the department there, the account

assignment is specified according to fixed characteristics. Because this department

mainly uses a free service entry that is assigned to the cost center, the cost center

“1000” and the general ledger (GL) account “400000” are specified. The business

area is also fixed and is therefore always included.

If the account assignment isn’t complete, for example, because the user occasion-

ally deviates from the normal procedure by choosing a project account assignment,

causing the entry of a WBS element (work breakdown structure) to be necessary,

the dialog window on the account assignment entry appears for the user to use.

You can now implement this example using the following step-by-step

procedure:

1. Create a new project in Transaction CMOD. You can find details on this in Sec-

tion 2.1.2, Creating a Project and Assigning Enhancements, in Chapter 2.

2. Switch to the user exit EXIT_SAPLMLSK_001, and generate the include ZXMLUU20

by double-clicking.

3. The user master is read first. This is possible via the function module BAPI_

USER_GET_DETAIL. You must only supply the parameter USERNAME with the cur-

rent user (from the system field sy-uname). The department is located in the

structure ADDRESS. Ignore all other return structures.

As with many other BAPIs, this BAPI checks whether or not a specific return

structure has been requested when called, and only then does it organize the

necessary content. Therefore, if you only supply the parameters that you really

need, the runtime of the BAPI is also reduced to a minimum.

4. If the department that is specified in the field ADDRESS-DEPARTMENT has the con-

tent “ABT20”, the header structure T_ESKN is filled and appended to the internal

table of the same name via the APPEND command. The most important fields

of this structure are listed in Table 4.1, and the corresponding coding can be

found in Listing 4.1.

Field Description

WPROZ Percentage of account assignment lines with regard to

distribution in percentage terms

MENGE Quantity reference of the current account assignment

line with regard to distribution by quantity

Table 4.1 Important Fields of the Table T_ESKN (Excerpt)

84

© 2013 by Galileo Press Inc., Boston (MA)

Field Description
WPROZ Percentage of account assignment lines with regard to distribution in percentage terms
MENGE Quantity reference of the current account assignment line with regard to distribution by quantity

第 85 页 / 共 275 页

Prepopulating Account Assignment for Service Lines 4.1

Field Description

SAKTO GL account

GSBER Business area

KOSTL Cost center

AUFNR Order number

PS_PSP_PNR Work breakdown structure element (WBS element)

XBKST Posting in cost center

XBAUF Posting to order

XBPRO Posting to project

Table 4.1 Important Fields of the Table T_ESKN (Excerpt) (Cont.)

*&-----------------------------------------------------------*

*& Include ZXMLUU20

*&-----------------------------------------------------------*

*“*“Local interface:

*“ IMPORTING

*“ VALUE(I_ESLL) LIKE ESLL STRUCTURE ESLL

*“ TABLES

*“ T_ESKN STRUCTURE SRVESKN

*“------------------------------------------------------------

DATA ls_address TYPE bapiaddr3.

DATA lt_return TYPE bapiret2_t.

DATA ls_return TYPE bapiret2.

DATA lv_subrc TYPE sy-subrc.

* Retrieve access to user master, address data

CALL FUNCTION ‘BAPI_USER_GET_DETAIL‘

EXPORTING

username = sy-uname

IMPORTING

address = ls_address

TABLES

return = lt_return.

* Has an error occurred?

LOOP AT lt_return INTO ls_return.

IF ls_return-type NA ‘SIW‘.

lv_subrc = 4.

85

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

Field Description
SAKTO GL account
GSBER Business area
KOSTL Cost center
AUFNR Order number
PS_PSP_PNR Work breakdown structure element (WBS element)
XBKST Posting in cost center
XBAUF Posting to order
XBPRO Posting to project

第 86 页 / 共 275 页

4 User Exits and BAdIs in External Services Management

EXIT.

ENDIF.

ENDLOOP.

* Always prepopulate department ABT20

IF lv_subrc = 0 AND

ls_address-department = ‘ABT20‘.

t_eskn-wproz = 100.

t_eskn-kostl = ‘1000’.

t_eskn-sakto = ‘400000’.

t_eskn-gsber = ‘9900‘.

APPEND t_eskn.

ENDIF.

Listing 4.1 Example for EXIT_SAPLMLSK_001

5. You also optionally have access to the service line itself (structure I_ESLL). You

can influence the account assignment based on specific characteristics of the

service line.

The I_ESLL-VRTKZ field contains the distribution indicator, if a multiple account

assignment is preset. You can define the settings for the account assignment

screen in the Customizing for Purchasing under Account Assignment • Main-

tain Account Assignment CATEGORIES. The account assignment screen (ID:

AcctAssgt Scrn) indicator must be set to 2 – Multiple Account Assignment,

and the Distribution key must be set to a distribution type. If this indicator is

set to 1 for distribution by quantity or to 2 for distribution in percentage terms,

you can also add several lines to the internal Table T_ESKN to control the distri-

bution. You set the distribution key via the field WPROZ or MENGE.

6. Don’t forget to activate the project so that your user exit will actually run.

4.2 Input Check of the Service Lines

With the enhancement SRVESLL, you can carry out input checks when service lines

are entered. There is also the alternative to prepopulate individual fields of a ser-

vice line or overwrite these with a fixed value.

The enhancement SRVESLL consists of two user exits that are called at different

times.

86

© 2013 by Galileo Press Inc., Boston (MA)

第 87 页 / 共 275 页

Input Check of the Service Lines 4.2

EE EXIT_SAPLMLSP_030

This exit runs before the standard checks are carried out.

EE EXIT_SAPLMLSP_031

This exit runs after all standard checks have been carried out.

Therefore, you’ll normally use the EXIT_SAPLMLSP_031 for your input checks. How-

ever, you can also use the EXIT_SAPLMLSP_030 to prepopulate fields with specific

values. All standard checks will still run after any possible changes.

4.2.1 Prepopulating Fields in EXIT_SAPLMLSP_030

The following brief example shows how you can prepopulate the fields of the

service line from this exit. The USERF1_TXT user field populates the name of the

person who created the service line. This field can be displayed again upon a sub-

sequent acceptance, and therefore offers information to the person who entered

the acceptance so that he doesn’t need to look at the purchase order. This can

facilitate contact when further questions arise.

1. Create a project again in Transaction CMOD. You can find details on this in

Section 2.1.2, Creating a Project and Assigning Enhancements, in Chapter 2.

Include the enhancement SRVESLL in your project.

2. Switch to the user exit EXIT_SAPLMLSP_030, and create the prepopulation of the

field srv_esll-userf1_txt. You must make sure that the exit will run only once

for each service line in the screen, which also applies to empty lines in which

the user hasn’t made any entry. If you were to now overwrite the field in all

lines, the user would receive an error message for these lines because the entry

is incomplete.

To avoid this, you need an additional check to determine whether this line is

relevant at all. Here, you can use the field MENGE — the line will only be used

when a quantity has been entered. A prepopulation will subsequently follow. A

brief example is provided in Listing 4.2.

3. Don’t forget to activate the project so that your enhancement will actually

run.

*&-----------------------------------------------------------*

*& Include ZXMLUU16

*&-----------------------------------------------------------*

*”*”Local interface:

*” CHANGING

87

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

第 88 页 / 共 275 页

4 User Exits and BAdIs in External Services Management

*” VALUE(SRV_ESLL) LIKE ESLL STRUCTURE ESLL

*”------------------------------------------------------------

* Prepopulating the fields of the structure ESLL:

* Remember creator of the service line

IF srv_esll-menge > 0.

srv_esll-userf1_txt = sy-uname.

ENDIF.

Listing 4.2 Example for EXIT_SAPLMLSP_030

4.2.2 Input Check in EXIT_SAPLMLSP_031

As already mentioned, you can use the exit EXIT_SAPLMLSP_031 by carrying out

custom input checks because it will only run after the standard checks are imple-

mented and the service line has its final status for the time being.

To show you an example, we must complete the field USERF2_TXT. Empty values

aren’t allowed.

1. Even here, you must first have a suitable project in Transaction CMOD to which

you have assigned the enhancement SRVESLL.

2. Switch to the user exit EXIT_SAPLMLSP_031, and program your check. This exit

actually only runs in contrast to EXIT_SAPLMLSP_030 when there is an entry

made by the user. However, it doesn’t do any harm to carry out an additional

check on an existing quantity.

3. If the field USERF2_TXT isn’t filled, an error message appears. So that the user is

able to produce a reference, you should also include the number of the service

line to this error message. This can be found in field SRV_ESLL-EXTROW. How-

ever, this field has been formatted so that the item number has been filled with

leading zeros, which doesn’t look nice when displayed on the screen. For this

reason, you should copy the content into a custom field beforehand and then

remove the leading zeros. Use the following command:

SHIFT <fieldname> LEFT DELETING LEADING ‘0’.

4. The service lines can be maintained from various purchasing documents and

different transactions. Therefore, you can’t use any message collector to display

the error message as usual, for example, in the Enjoy purchase order (Transac-

tion ME21N). Display the error message directly using the command MES-

SAGE. The brief example on this exit is given in Listing 4.3.

88

© 2013 by Galileo Press Inc., Boston (MA)

第 89 页 / 共 275 页

Prepopulation of the Header Data in the Data Entry Sheet 4.3

5. Don’t forget to activate the project so that your enhancement will run.

*&-----------------------------------------------------------*

*& Include ZXMLUU17

*&-----------------------------------------------------------*

*”*”Local interface:

*” IMPORTING

*” VALUE(SRV_ESLL) LIKE ESLL STRUCTURE ESLL

*”------------------------------------------------------------

* Temporary field for display in message

DATA lv_extrow TYPE esll-extrow.

* Field USERF2_TXT must be filled:

IF srv_esll-userf2_txt IS INITIAL AND

srv_esll-menge > 0.

* Message 100:

* ‘Line &: Please complete user field 4’

lv_extrow = srv_esll-extrow.

SHIFT lv_extrow LEFT DELETING LEADING ‘0’.

MESSAGE E100(ZSRV) WITH lv_extrow.

ENDIF.

Listing 4.3 Example for EXIT_SAPLMLSP_031

4.3 Prepopulation of the Header Data in the Data Entry

Sheet

When you create a new data entry sheet (Transaction ML81N), some header data

must first be filled out before the services can be entered or copied from the pur-

chase order.

You can also set up prepopulations by using the user exit EXIT_SAPLMLSR_010,

which is beneficial for the user. As an example, the short text on a new data entry

sheet is supposed to be filled out automatically (see Figure 4.2).

89

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

第 90 页 / 共 275 页

4 User Exits and BAdIs in External Services Management

Figure 4.2 Header Data on the Data Entry Sheet

Proceed as follows:

1. Create a new project in Transaction CMOD, and include the enhancement

SRVESSR.

2. Go directly to EXIT_SAPLMLSR_010, and enter the respective coding. All data

for the purchase order is provided via the structures I_EKKO and I_EKPO. In the

example given, the text “Acceptance of Purchase Order <Purchase Order Num-

ber> Item <Item>” is supposed to be created and used as a short text.

The purchase order number is in the field I_EKKO-EBELN, and the item is in field

I_EKPO-EBELP. You should remove the leading zeros again to display the item.

If you use purchase orders with less than the usual 10 digits, you should also

manage zeros for your purchase order number.

3. The example is given in Listing 4.4. Text literals have been used to create the

short text. You can create these texts also as a text symbol (GoTo • Text Ele-

ments • Text Symbols) and use these via the term TEXT-### instead of the lit-

erals in the coding. The advantage of this is that you can also use the texts via

GoTo • Translation in other languages.

*&-----------------------------------------------------------*

*& Include ZXMLUU19

*&-----------------------------------------------------------*

*”*”Local interface:

*” IMPORTING

90

© 2013 by Galileo Press Inc., Boston (MA)

第 91 页 / 共 275 页

Prepopulation of the Header Data in the Data Entry Sheet 4.3

*” VALUE(I_EKKO) LIKE EKKO STRUCTURE EKKO

*” VALUE(I_EKPO) LIKE EKPO STRUCTURE EKPO

*” CHANGING

*” VALUE(C_ESSR) LIKE ESSR STRUCTURE ESSR

*”------------------------------------------------------------

* Local fields for formatting

DATA lv_text TYPE essr-txz01.

DATA lv_ebelp TYPE ekpo-ebelp.

IF c_essr-txz01 IS INITIAL.

* Remove leading zeros of the item

lv_ebelp = i_ekpo-ebelp.

SHIFT lv_ebelp LEFT DELETING LEADING ‘0’.

* Creating and assigning text

CONCATENATE ‘Abnahme zu’

i_ekko-ebeln

‘Position’

lv_ebelp

INTO lv_text

SEPARATED BY space.

c_essr-txz01 = lv_text.

ENDIF.

Listing 4.4 Example for EXIT_SAPLMLSR_010

91

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

第 92 页 / 共 275 页

© 2013 by Galileo Press Inc., Boston (MA)

第 93 页 / 共 275 页

5 User Exits and BAdIs in

Inventory Management

The concept of Inventory Management in SAP Materials Management (MM) basi-

cally involves summarizing the management of all warehouse stocks with regard

to value and quantity as well as the associated goods movements. Because the

conceivable processes in this core area are very comprehensive, there are also

many setting options in the system. This is also reflected in the number of pos-

sible program-related enhancements. In this chapter, you will learn about the most

important enhancements for Inventory Management.

5.1 Custom Fields in Transaction MIGO

With the implementation of the central Transaction MIGO for all goods movements,

a powerful BAdI is provided: MB_MIGO_BADI. With this BAdI, you can integrate and

post custom fields as custom tabstrips at the header and item level. You can also

carry out input checks in your custom fields or populate many standard fields with

default values.

Display of Custom Data

If you execute the following example, you may find that the subsequent display of the

data of a posted material document at item level doesn’t work. This is the case when

SAP Note 1029951 has been manually imported into your system or support packages

have been imported.

As a result of the changes from this note, the method LINE_MODIFY, which is actually

provided for reading the item data from the database, will no longer run when material

documents are displayed. Unfortunately, it isn’t possible to use another method for this

because the associated standard item data (structure GOITEM) isn’t provided in all other

relevant methods, and you therefore cannot assign your data to a specific document

line.

With SAP Note 1477221, the method LINE_MODIFY will run again. If this note hasn’t

yet been released for customers when you read these lines, you can modify the program

location relevant for the call of the method, as described in SAP Note 1136344. The

example given here will be fully functional in both cases.

93

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

Display of Custom Data
If you execute the following example, you may find that the subsequent display of the data of a posted material document at item level doesn’t work. This is the case when SAP Note 1029951 has been manually imported into your system or support packages have been imported. As a result of the changes from this note, the method LINE_MODIFY, which is actually provided for reading the item data from the database, will no longer run when material documents are displayed. Unfortunately, it isn’t possible to use another method for this because the associated standard item data (structure GOITEM) isn’t provided in all other relevant methods, and you therefore cannot assign your data to a specific document line. With SAP Note 1477221, the method LINE_MODIFY will run again. If this note hasn’t yet been released for customers when you read these lines, you can modify the program location relevant for the call of the method, as described in SAP Note 1136344. The example given here will be fully functional in both cases.

第 94 页 / 共 275 页

5 User Exits and BAdIs in Inventory Management

5.1.1 Custom Fields: An Overview

The capability of the BAdI MB_MIGO_BADI results in a certain amount of complex-

ity. There are 17 methods that are called at many different times. Nevertheless, to

make the next example as clear as possible, you’ll only view the implementation

of custom fields at the header and item level (see Figure 5.1). This reduces the

BAdI to 11 methods with which the minimum requirements can be fulfilled. In a

second step, you’ll then learn about the function of the remaining methods in a

brief overview.

Figure 5.1 Custom Fields in Transaction MIGO

Function Group for the Management of Dynpros

Just as with the custom fields in the purchase order (see Section 3.1, Custom Fields

in Purchase Orders, in Chapter 3), you first need a function group to include the

dynpros and some function modules for the exchange of data and for updating

custom fields. However, this function group can be implemented for the BAdI

MB_MIGO_BADI much more easily and must assume fewer tasks.

You have to work with two BAdIs in the purchase order to activate the custom

fields. The function group therefore not only has to assume the communication

with the dynpros but also must map the communication between the BAdIs. You

can, however, execute all the necessary steps directly in the BAdI MB_MIGO_BADI for

94

© 2013 by Galileo Press Inc., Boston (MA)

第 95 页 / 共 275 页

Custom Fields in Transaction MIGO 5.1

Transaction MIGO, so additional communication with another BAdI isn’t needed.

You can create a dynpro in the function group for the header and item data and

create two function modules for each dynpro to store and retrieve the data in the

dynpro. You also need an update module to save your custom data. To post your

custom data, you need to define custom tables in the ABAP Dictionary.

Usage of the BAdI MB_MIGO_BADI

As soon as you’ve created the function group, you can begin with the implementa-

tion of the BAdI. You keep the data on the custom fields for runtime in the attributes

of the BAdI. First, these areas need to be defined.

MB_MIGO_BADI can be implemented many times. However, there are only five

custom tabstrips for the header and item levels. For this reason, precisely five

active implementations are allowed. The method INIT is activated when Transac-

tion MIGO is started, and it’s used to register an implementation and populate a

tabstrip.

With the initialization of a document (new document or display of an existing

document), the methods will run from Table 5.1. The methods MODE_SET and STA-

TUS_AND_HEADER are also triggered when the document status changes.

Method Description

RESET This method is used to initialize all custom data in the

attributes of the BAdI implementation.

MODE_SET In this method, you obtain information on the action

(goods receipt, goods issue, etc.) and the chosen

reference (purchase order, reservation, etc.) chosen by

the user; these are the fields that are always available in

Transaction MIGO in the top-left side. You can evaluate

this information, and store it in the attributes of the

implementation for subsequent use.

STATUS_AND_HEADER This method is mainly used to fill the custom header data

when an existing document is read.

Table 5.1 Initialization of a Document and Status Change

The methods are also executed with each dialog step for header data from Table

5.2 and with each dialog step on item data from Table 5.3. The method LINE_MOD-

IFY is also executed when a line is added either when the user makes an entry or

when an existing material document is read from the database.

95

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

Method Description
RESET This method is used to initialize all custom data in the attributes of the BAdI implementation.
MODE_SET In this method, you obtain information on the action (goods receipt, goods issue, etc.) and the chosen reference (purchase order, reservation, etc.) chosen by the user; these are the fields that are always available in Transaction MIGO in the top-left side. You can evaluate this information, and store it in the attributes of the implementation for subsequent use.
STATUS_AND_HEADER This method is mainly used to fill the custom header data when an existing document is read.

第 96 页 / 共 275 页

5 User Exits and BAdIs in Inventory Management

Method Description

PBO_HEADER This method is called before the header data are displayed.

You must transfer the data to be displayed to the function

group and specify the dynpro to be displayed. The tabstrip

header is also specified here.

PAI_HEADER This method runs as soon as the user has entered the data.

You need to retrieve the possibly changed data from the

function group.

Table 5.2 Methods in the Dialog Step on Header Data

Method Description

PBO_DETAIL This method is called before the item data is displayed. In

the parameter I_LINE_ID, you obtain the current item and

must transfer the associated data in the function group.

You also need to specify the dynpro to be displayed as well

as the header of the tabstrip here.

PAI_DETAIL You retrieve the data from the dynpro after the user has

entered it. First, check whether data has changed, and

if applicable, set the parameter E_FORCE_CHANGE. The

method LINE_MODIFY is activated when the parameter has

been set.

LINE_MODIFY This method is activated when a line has been changed

(see PAI_DETAIL) or when a new line has been inserted.

When adding new lines, you must initialize your custom

fields, or if the document is being read in the display

mode, you must read the data from the database.

If required, you have the option here of writing standard

fields that you’ve also possibly integrated in your dynpro

back to the standard items.

LINE_DELETE This method runs when the user has deleted a line of the

material document. In this case, you also need to delete

the associated custom data.

Table 5.3 Methods in the Dialog Step for Item Data

Finally, you must use the method POST_DOCUMENT to format your custom data and

call the update module from your function group. The final line number for each

document line is set only in this method, as contained in Table MSEG — that is, in

96

© 2013 by Galileo Press Inc., Boston (MA)

Method Description
PBO_HEADER This method is called before the header data are displayed. You must transfer the data to be displayed to the function group and specify the dynpro to be displayed. The tabstrip header is also specified here.
PAI_HEADER This method runs as soon as the user has entered the data. You need to retrieve the possibly changed data from the function group.
Method Description
PBO_DETAIL This method is called before the item data is displayed. In the parameter I_LINE_ID, you obtain the current item and must transfer the associated data in the function group. You also need to specify the dynpro to be displayed as well as the header of the tabstrip here.
PAI_DETAIL You retrieve the data from the dynpro after the user has entered it. First, check whether data has changed, and if applicable, set the parameter E_FORCE_CHANGE. The method LINE_MODIFY is activated when the parameter has been set.
LINE_MODIFY This method is activated when a line has been changed (see PAI_DETAIL) or when a new line has been inserted. When adding new lines, you must initialize your custom fields, or if the document is being read in the display mode, you must read the data from the database. If required, you have the option here of writing standard fields that you’ve also possibly integrated in your dynpro back to the standard items.
LINE_DELETE This method runs when the user has deleted a line of the material document. In this case, you also need to delete the associated custom data.

第 97 页 / 共 275 页

Custom Fields in Transaction MIGO 5.1

the table in which the standard item data is stored. You should convert your cus-

tom data accordingly so that you can assign it more easily later on.

5.1.2 Preparations in the ABAP Dictionary

At this point, it makes sense to refer again to the general data definitions in the

ABAP Dictionary. Both header and item data are implemented in this example,

and two suitable database tables are required to store the data. The following

example again only uses a simple variant for this, and only one text field is used

as a custom field.

1. Switch to the ABAP Dictionary (Transaction SE11), and create the table for the

header data. Besides the actual text field ZK_FELD1, you also need the key fields;

that is, the material document number and posting year. The table name ZMB_

MIGOHEAD has been used in the example. You can view the associated fields in

Table 5.4.

Field Name Key Field Data Element

MANDT Yes MANDT

MBLNR Yes MBLNR

MJAHR Yes MJAHR

ZK_FELD1 CHAR32

Table 5.4 Fields of the Table ZMB_MIGOHEAD

2. Create the table for the item data. A line number is necessary besides the previ-

ously used key fields. The custom field is called ZP_FELD1 here. The table name

ZMB_MIGOITEM is used. The associated fields can be viewed in Table 5.5.

Field Name Key Field Data Element

MANDT Yes MANDT

MBLNR Yes MBLNR

MJAHR Yes MJAHR

ZEILE Yes MBLPO

ZP_FELD1 CHAR32

Table 5.5 Fields of the Table ZMB_MIGOITEM

97

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

Field Name Key Field Data Element
MANDT Yes MANDT
MBLNR Yes MBLNR
MJAHR Yes MJAHR
ZK_FELD1 CHAR32
Field Name Key Field Data Element
MANDT Yes MANDT
MBLNR Yes MBLNR
MJAHR Yes MJAHR
ZEILE Yes MBLPO
ZP_FELD1 CHAR32

第 98 页 / 共 275 页

5 User Exits and BAdIs in Inventory Management

5.1.3 Preparation of the Function Group

You next need to deal with the function group and the dynpro. In this example,

both the header and the item data are implemented. A dynpro is also required

here. Moreover, two function modules for the data communication and one

update module are necessary in each case. Another module informs the function

group on the current status of Transaction MIGO. If this isn’t in the display mode,

the fields in the dynpros aren’t ready for input.

1. Switch to Transaction SE80. Select the Function Group option in the Reposi-

tory Browser, and specify ZMB_MIGO as the name. Press [Enter]. Confirm in the

dialog that this function group should now be generated.

Dynpro Variations

In this example, a dynpro is defined for the header and item data in each case. You

certainly have the option, however, of preparing different situations as well as differ-

ent dynpros. Your requirement can, for example, be different in goods receipt than

in goods issue. You decide which dynpro is actually displayed at runtime via your

programming.

In the method MODE_SET of the BAdI MB_MIGO_BADI, you learn which action (dis-

plays, goods receipt, goods issue, etc.) has just been selected. You can keep this in-

formation in an attribute of the class and then dynamically set the dynpro in method

PBO_HEADER or PBO_DETAIL based on this information.

2. Create the dynpro for the header data. Now right-click ZMB_MIGO in the object

browser, and select Create • Dynpro from the context menu. The dynpro num-

ber should be 0100. In the properties, you must set Subscreen as the Dynpro

Type because other types cannot be integrated with tabstrips, and you would

get a short dump upon implementation.

3. Click the Layout button to switch to the Screen Painter. Here you choose Dic-

tionary/Program Fields in the GoTo • Secondary Window menu, or press

[F6]. In the window displayed, specify ZMB_MIGOHEAD as the table name and

press [Enter]. Mark the field ZK_FELD1, and press [Enter] again (see Figure 5.2).

Using the mouse, now position the field in the top-left corner of the dynpro.

Save and activate the dynpro, and then exit the Screen Painter.

4. Create the dynpro 0200 for the item data. Choose the field ZP_FELD1 from Table

ZMB_MIGOITEM, and then proceed as in the previous step.

98

© 2013 by Galileo Press Inc., Boston (MA)

Dynpro Variations
In this example, a dynpro is defined for the header and item data in each case. You certainly have the option, however, of preparing different situations as well as differ- ent dynpros. Your requirement can, for example, be different in goods receipt than in goods issue. You decide which dynpro is actually displayed at runtime via your programming. In the method MODE_SET of the BAdI MB_MIGO_BADI, you learn which action (dis- plays, goods receipt, goods issue, etc.) has just been selected. You can keep this in- formation in an attribute of the class and then dynamically set the dynpro in method PBO_HEADER or PBO_DETAIL based on this information.

第 99 页 / 共 275 页

Custom Fields in Transaction MIGO 5.1

Figure 5.2 Dict/Program Fields Window

5. So that you are able to subsequently fill the fields in these dynpros with data,

fields with an identical name must be available in the global data. Create these

with the keyword TABLES. Furthermore, the fields are then ready for input only

if the document isn’t in a display mode. The mode is to be subsequently set via

a function module. You can create it now, however, as a flag (gv_outputonly,

data type C) in the global data.

You switch to the global data via the Object Browser by navigating to the include

LZMB_MIGOTOP in the Includes section. An example is given in Listing 5.1.

FUNCTION-POOL ZMB_MIGO. “MESSAGE-ID ..

* Work structures for dynpros

TABLES: zmb_migohead,

zmb_migoitem.

* Display mode?

DATA gv_outputonly TYPE c.

Listing 5.1 Global Data in LZMB_MIGOTOP

6. If the flag gv_outputonly is set, then the fields in the dynpro aren’t ready for

input. Therefore, change to the flow logic of dynpro 0100. Remove the com-

ments in the PROCESS BEFORE OUTPUT time from the STATUS_0100 module. Dou-

ble-click the module name. To generate the module, use the suggested include

name.

Set the value of SCREEN-INPUT based on the field gv_outputonly with a LOOP AT

SCREEN. An example is given in Listing 5.2.

99

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

第 100 页 / 共 275 页

5 User Exits and BAdIs in Inventory Management

*------------------------------------------------------------*

***INCLUDE LZMB_MIGOO01 .

*------------------------------------------------------------*

MODULE status_0100 OUTPUT.

LOOP AT SCREEN.

IF gv_outputonly IS INITIAL.

screen-input = 1.

ELSE.

screen-input = 0.

ENDIF.

MODIFY SCREEN.

ENDLOOP.

ENDMODULE. “ STATUS_0100 OUTPUT

Listing 5.2 Flow Logic on Dynpro 0100

7. Dynpro 0200 is also not ready for input in the display mode. Switch to dynpro

0200, and proceed as in the previous step.

Next you can begin with the function modules:

1. An overview of the required modules is given in Table 5.6. Begin with the mod-

ule ZMB_MIGO_SETSTATUS. You only need a flag as an input parameter; from this,

you overwrite the global field gv_outputonly (see Listing 5.3).

Function Module Description

ZMB_MIGO_SETSTATUS Sets the flag gv_outputonly

ZMB_MIGO_HEAD_SET Sets data for dynpro 100

ZMB_MIGO_HEAD_GET Returns data from dynpro 100

ZMB_MIGO_ITEM_SET Sets data for dynpro 200

ZMB_MIGO_ITEM_GET Returns data from dynpro 200

ZMB_MIGO_POST Updates header and item data

Table 5.6 Function Modules for Data Exchange

FUNCTION ZMB_MIGO_SETSTATUS.

*“------------------------------------------------------------

*“*“Local interface:

*“ IMPORTING

*“ REFERENCE(I_OUTPUTONLY) TYPE C

100

© 2013 by Galileo Press Inc., Boston (MA)

Function Module Description
ZMB_MIGO_SETSTATUS Sets the flag gv_outputonly
ZMB_MIGO_HEAD_SET Sets data for dynpro 100
ZMB_MIGO_HEAD_GET Returns data from dynpro 100
ZMB_MIGO_ITEM_SET Sets data for dynpro 200
ZMB_MIGO_ITEM_GET Returns data from dynpro 200
ZMB_MIGO_POST Updates header and item data

第 101 页 / 共 275 页

Custom Fields in Transaction MIGO 5.1

*“------------------------------------------------------------

* Set input status

gv_outputonly = i_outputonly.

ENDFUNCTION.

Listing 5.3 Coding on ZMB_MIGO_SETSTATUS

2. Create the modules ZMB_MIGO_HEAD_SET and ZMB_MIGO_HEAD_GET. You need a

structure with Table ZMB_MIGOHEAD as a data type to use it as an import or export

parameter. Simply copy the data in the respective direction between the param-

eters and the work structure previously defined with TABLES (see Listing 5.4

and Listing 5.5).

FUNCTION ZMB_MIGO_HEAD_SET.

*”------------------------------------------------------------

*”*”Local interface:

*” IMPORTING

*” REFERENCE(I_HEAD) TYPE ZMB_MIGOHEAD

*”------------------------------------------------------------

* Prepare data for dynpro

zmb_migohead = i_head.

ENDFUNCTION.

Listing 5.4 Coding on ZMB_MIGO_HEAD_SET

FUNCTION zmb_migo_head_get.

*”------------------------------------------------------------

*”*”Local interface:

*” EXPORTING

*” REFERENCE(E_HEAD) TYPE ZMB_MIGOHEAD

*”------------------------------------------------------------

* Return data from dynpro

e_head = zmb_migohead.

ENDFUNCTION.

Listing 5.5 Coding on ZMB_MIGO_HEAD_GET

3. For the function modules ZMB_MIGO_ITEM_SET and ZMB_MIGO_ITEM_GET, proceed

exactly as before. Only use ZMB_MIGOITEM as a reference (see Listing 5.6 and

Listing 5.7).

101

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

第 102 页 / 共 275 页

5 User Exits and BAdIs in Inventory Management

FUNCTION zmb_migo_item_set.

*”------------------------------------------------------------

*”*”Local interface:

*” IMPORTING

*” REFERENCE(I_ITEM) TYPE ZMB_MIGOITEM

*”------------------------------------------------------------

* Prepare data for dynpro

zmb_migoitem = i_item.

ENDFUNCTION.

Listing 5.6 Coding on ZMB_MIGO_ITEM_SET

FUNCTION ZMB_MIGO_ITEM_GET.

*”------------------------------------------------------------

*”*”Local interface:

*” EXPORTING

*” REFERENCE(E_ITEM) TYPE ZMB_MIGOITEM

*”------------------------------------------------------------

* Return data from dynpro

e_item = zmb_migoitem.

ENDFUNCTION.

Listing 5.7 Coding on ZMB_MIGO_ITEM_GET

4. Create the function module ZMB_MIGO_POST. The module must be marked in

the Processing Type section as an Update Module (Start immed.) (see Figure

5.3).

Create an import parameter (suggestion I_HEAD) as a structure for Table ZMB_

MIGOHEAD. Activate the Pass Value option for this parameter because no refer-

ence parameters are allowed in update modules. Then define a table parameter

(suggestion T_ITEMS) with reference to Table ZMB_MIGOITEM.

5. With regard to update modules, you should always integrate several logical

tests to prevent erroneous updates as much as possible. You need to check

whether all key fields of the header and item data are filled. Otherwise, an

exception will be triggered. To do this, create the DATA_ERROR exception in the

Exceptions tabstrip.

If the writing of the data fails, however, an exception will also be triggered. You

create this exception as INSERT_ERROR. Triggering an exception results in an

102

© 2013 by Galileo Press Inc., Boston (MA)

第 103 页 / 共 275 页

Custom Fields in Transaction MIGO 5.1

update termination, and if there’s an error, the complete material document is

not posted.

Figure 5.3 Properties of the Update Module

6. Switch to the Source Text, and begin with the programming. Check whether

the key fields of the header data are filled (parameter I_HEAD, fields MBLNR and

MJAHR). Proceed exactly as before in a loop via the item data (Table T_ITEMS).

Check also the line number (field ZEILE). If an error occurs, trigger the excep-

tion DATA_ERROR by using the command RAISE.

7. If no error occurs, update the data using the command INSERT. If an error occurs

here (SY-SUBRC <> 0), trigger the exception INSERT_ERROR (see Listing 5.8).

FUNCTION zmb_migo_post.

*”------------------------------------------------------------

*”*”Update module:

*”

*”*”Local interface:

*” IMPORTING

*” VALUE(I_HEAD) TYPE ZMB_MIGOHEAD

*” TABLES

103

Personal Copy for Yang Meteor, user id a8103f58-81fe-4f49-acf6-cfb58959c618

返回ABAP参考资料列表