127
Data Interface Programming Author: Subhendu Majumdar 1 Subhendu Majumdar Technical Consultant, IBM

Data Transfer

Embed Size (px)

DESCRIPTION

Data Transfer

Citation preview

Page 1: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

1

Subhendu MajumdarTechnical Consultant, IBM

Page 2: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Introduction..............................................................................................................................4Common jobs to be done for any kind of Data transfer...........................................................4Commonly occurring errors during data transfer: -..................................................................6Direct Input: -...........................................................................................................................6CALL TRANSACTION and BATCH INPUT methods: -...........................................................7

Overview..............................................................................................................................7Authorizations for Batch Input Processing.........................................................................10Standard Utility Reports for Batch Input.............................................................................10Batch Input/Call Transaction- Summary............................................................................11Points to remember: -........................................................................................................11

SHDB Transaction Recorder.......................................12

Recording New Transactions.............................................................................................12Significance of the entries shown above:-.........................................................................19Recording another transaction...........................................................................................20Processing a Recording:....................................................................................................21Editing the recording:.........................................................................................................27Saving the Recording in Local PC:....................................................................................28Creating a Session from BDC Recording:.........................................................................29Creating Test Data from Recording:-.................................................................................30What to do if the test file is wanted in your local PC?........................................................31Generating Function Modules from Recording:.................................................................33Generating Data Transfer Programs..................................................................................37

Prerequisites...................................................................................................................37Procedure.......................................................................................................................37

Special features of SHDB..................................................................................................37

LSMW.......................................................................................................................38

Introduction........................................................................................................................39A little bit theory about LSMW............................................................................................40

LSMW FOR MATERIAL MASTER LOADING................................................42

USING DIRECT INPUT...........................................................................................42Maintain Source Attributes.............................................................................................43Maintain Source Structure..............................................................................................43Maintain Source Fields...................................................................................................43Maintain Structure Relations..........................................................................................44Maintain Field Mappings and Conversion Rules............................................................45Fixed Values, Translations, User Defined Routines.......................................................48Specify Files...................................................................................................................50Assign Files....................................................................................................................52Read Data......................................................................................................................52Display Read Data.........................................................................................................54Convert Data..................................................................................................................56Display Converted Data.................................................................................................56Start Direct Input Program..............................................................................................56

Material Consumption Loading by.......................................................................59

Batch Input Session Using LSMW......................................................................59Maintain Object Attributes..............................................................................................61

2

Page 3: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Maintain source structures.............................................................................................64Maintain Source Fields...................................................................................................64Maintain Structure Relations..........................................................................................65Maintain Field Mapping and Conversion Rules..............................................................65Specify Files...................................................................................................................66Assign Files....................................................................................................................68Read File........................................................................................................................68Convert Data..................................................................................................................68Display Converted Data.................................................................................................69Create Batch Input Session............................................................................................71Run Batch Input Session:...............................................................................................71

Programming Data Transfer Using SE38..72

Basic logic: -.......................................................................................................................72Information on BDCDATA structure: -................................................................................72Populating internal table for bdcdata: -..............................................................................72Using CALL TRANSACTION for data transfer...................................................................74

Syntax:............................................................................................................................74Error Analysis and Restart Capability.............................................................................75

BATCH INPUT SESSIONS (SM35 /SM37).......................................................................76EXAMPLE PROGRAM ON CALL TRANSACTION...............................................................78EXAMPLE PROGRAM CREATING BATCH INPUT SESSION............................................83

3

Page 4: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Data Transfer

Introduction Important for implementation of any system. Every software system provides some utility for data upload. Becomes a nightmare when it becomes the job of data entry operators from the hard

copy of data. Becomes relatively easy when it can be done by programming if the data to be

uploaded is available in the form of soft copy. Manual data entry is preferred if the master data to be uploaded is of relatively small

volume (5 to 150). Larger number of data requires interface programming. Data transfer jobs account for 15-30% of the overall cost of any software

implementation.

Common jobs to be done for any kind of Data transfer

Identify the destination structure and its fields/columns to be populated. Identify the source structure in legacy and detect the fields/columns whose

values will be used for data upload. Plan the conversion rules, if any. Download the data from the legacy in some suitably formatted flat file. Data is uploaded into some structure /variable of current system. Destination structure is populated from legacy data using the conversion rule.

Or, one can format or arrange the legacy data after filtering them by conversion rule.

One example: -

Say, you are planning for the material master upload for SAP R/3. You have found out what are your information requirements in SAP R/3 and what information from legacy will be required for that. Let, the information required for SAP R/3 is: -

Material code Industry sector Old material code Material type Plant Description for the material Unit Material group Division Valuation Category for batch management Price Indicator Account class for material MRP Type

4

Page 5: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

The information from legacy system to be utilized is: -

Material code Type Source Class Description Unit

Now, you have planned and designed a mapping conversion rule for data transfer. It may be as follows: -

Fields in SAP R/3 Way of PopulationMaterial code To be generated by R/3 by internal number assignmentIndustry Sector Hard coded for ‘Retail Industry’Ole material code Legacy material codeMaterial type ‘ROH’ when Class of legacy data is other than P

‘ERSA’ when it is ‘P’Plant Hard coded as ‘SDC1’Description Legacy DescriptionUnit ‘TO’ for legacy unit = TON

‘PCS’ for legacy unit = ‘PIECES’‘BT’ for legacy unit = ‘BOTTLE’.

Material group ‘NF01’ when legacy: class = N, type = G, source = F‘NF02’ when legacy: class = N, type = S, source = F‘NI01’ when legacy: class = N, type = G, source = I‘NI02’ when legacy: class = N, type = S, source = I‘SPARE’ when legacy: class = P

Division ‘01’ to be hard codedValuation Catg ‘X’ when legacy: source = FPrice Indicator ‘V’ for legacy source = F

‘S’ for legacy source = IAccount class ‘3000’ when legacy class is other than P

‘3100’ when legacy class is PMRP type ‘ND’ to be hard coded

You can apply the conversion rules in the legacy itself and prepare the filtered data or can upload raw data from legacy and filter it in R/3.

Start uploading the data from legacy into R/3 system.

SAP provides facilities to re-process those data for which errors occurred while posting them into database.

5

Page 6: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Commonly occurring errors during data transfer: -

1. Mandatory fields are not filled with values.2. Values in R/3 fields do not obey the validity checks.

Data Transfer Techniques provided by SAP

Following methods are available in SAP R/3 for Data Upload: -

1. Direct Input2. Batch Input3. Call transaction4. Function modules and Basis

Direct Input: -

Data from the data file is first checked to see if it meets various criteria. If satisfied, data is transferred directly into the SAP system.

Two options are available: -

Starting the program Directly: - System does not generate error log, nor is it possible to re-start the system if an error occurs.To enter the data into corresponding database tables directly, system calls a number of function modules that execute the necessary checks.

Direct Input in the Background: - One can re-start processing if the program terminates or logical error occurs. The ability to re-start the system ensures that data cannot be posted twice to the database, as the program can be reset where it terminates. Using the trace one can correct any error that occurs.

Example of Data Transfer objects using direct input methods: -

Object Program Significance0020 RMDATIND Material master upload0010 RFBISA00 G/L account master0030 RCSBI010 Create Material BOM0040 RFBIKR00 Vendor master0050 RFBIDE00 Customer Master0060 RM06IBI0 Purchasing Info Record0070 RV14BTCI Purchasing Condition Record0080 RM06BBI0 Purchase Requisition

For background processing, data file cannot be stored in presentation server.

6

Page 7: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

CALL TRANSACTION and BATCH INPUT methods: -

Overview

In call transaction and batch input methods, data are fed into screens of some transaction and data are posted via the fields of the screens.

One has to know the transaction codes for data transfer for that. Some examples are as below: -

Transaction Code SignificanceMM01 Material master creationXK01 Vendor master creationVA01 Sales order creationME21 Purchase Order creationMB1C Stock load for materials

The programs assigned to a transaction code may contain multiple screens and each screen contains multiple fields. Program name and screen number identifies each screen and each field has got its own technical name. All these information are used to fill an internal table of structure: BDCDATA and is used to call a transaction or creating a batch input session. The CALL TRANSACTION posts the records at the time of running the program and BATCH INPUT method creates batch input session which is later processed to save the data in the database.

To find out the program name, screen number and technical name of the fields in the screen, there are basically two ways: -

7

Page 8: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Go to the transaction code and press F1 and then F9 on the fields where U want to place the information. You will get a screen on technical details where all the necessary information is provided.

The following screen appears: -

8

Place your cursor on the field and press F1 key

Press F9 key or click on the Technical Information pushbutton

Page 9: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Record the transaction-by-transaction recorder (SHDB) and get the results of the recording.

9

Program name and screen number information

Technical name of the field

Page 10: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Authorizations for Batch Input Processing

Standard Utility Reports for Batch Input

10

Page 11: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Batch Input/Call Transaction- Summary

Points to remember: -

1. One should start database programming when he is sure that no previous SAP standard/custom programs are not available to do the task.

2. One can use LSMW for data upload as this tool helps automatic creation of data transfer programming.

11

Page 12: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

SHDB Transaction Recorder

Recording New Transactions

1. Go to transaction SHDB. Press the New Recording pushbutton from application Toolbar.

2. Enter the name for the Recording and the transaction code to be used for recording. Then press the Continue pushbutton.

12

Page 13: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

3. The screens for the transaction code will appear one by one. Make a dummy transaction posting by filling in all the information:

13

Page 14: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

14

Page 15: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

15

Page 16: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

16

Page 17: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

17

Page 18: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

After you save the transaction, the recording will be visible to you:-

18

Page 19: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Significance of the entries shown above:-

In the first screen, the program name is SAPMF02K and screen number is 0100.That is a new screen ; hence Start column has a value 0f X.

When U were in this screen, U entered:‘SDC1’ in the company code field (technical name : ‘RF02K-BUKRS’)‘SDC1’ in the purchasing organization field (RF02K-EKORG)

19

Page 20: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

‘KRED’ in the Vendor account group (RF02K-KTOKK).

The cursor was initially in the Vendor Account group field. The field where the cursor is initially placed is signified by: BDC_CURSOR entry.

When U filled in the screen, U pressed Enter to navigate to the next screen. The functional code for this Enter is /00 for this screen. That action is captured by the ‘BDC_OKCODE’ entry.

In this way, all your entries and actions in different screens are captured in this recording.

Recording another transaction

Once you have recorded a transaction, you can record for another transaction on the same recording. For that, you have to press the Recording pushbutton from the application toolbar.

A screen for new transaction code will appear. Fill it with the transaction you want to record next and proceed as instructed above.

20

Page 21: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Processing a Recording:

U can now process this recording to view a sample posting. That is to say, the system will process the entries found in the recording to post a transaction in the database.

For that, press the pushbutton from the Application Toolbar.

A selection-screen appears as follows: -

The processing mode can be:

Update session can be either of:

(Details about these two selections will be discussed later on)

Fill in the selection-screen and press enter.

Now, the transaction XK01( for which U recorded) will be used with the values for the fields and okcodes and a transaction will be posted.

21

Page 22: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

22

Page 23: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

23

Page 24: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

24

Page 25: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

25

Page 26: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

At last, you get a report on transaction posting:

So, you see that your recording is used to post a transaction utilizing XK01.Now, if we can utilize the technical information from this recording and instruct the program to take the values for the fields from some other sources, then we shall be able to post large number of transactions by this method ; instead of using XK01 for each posting !!!!!

That is the idea of interface programming.

26

Page 27: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Editing the recording:

Notice that those fields have appeared in the recording for which U placed some entries. The other fields, where you have not given any values are not present over here.

Now, it may so happen, that at the time of recording, U forgot to fill in some fields in some screens,Which U want to fill now. For e.g., let us assume that U need to populate the Street field in Address screen. So, instead of re-recording, you can add one line in the recording. For that, U have to go to the appropriate section of the recording for the screen and add a line in the recording for the field.

Place the cursor on a row before which U want to insert the line and press Insert Line pushbutton from the Application Toolbar: -

A blank line opens up for entry:-

Write the technical name of the field and give some value:-

Then Save the recording and Process it to have the effect on the Address screen:

27

Your cursor position

Page 28: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

In this way, one can add specifications for additional fields after initial recording of a transaction.

Similarly, one can delete specifications for existing field(s) by choosing a line and pressing Delete Line pushbutton from Application Toolbar.

Saving the Recording in Local PC:

One can save the result of the recording from R/3 into Local PC, maybe in form of some excel file.This saved information can be utilized for development of interface programming as a quick reference.Press Import pushbutton from the Application Toolbar.

A dialog box will appear. Enter the name of the file in the PC where U want to save the recording:-

Then press Enter. The file will be created in your PC. Open and see it.

28

Page 29: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Creating a Session from BDC Recording:

Go to the initial screen of SHDB. Select your recording from the list and press the Session pushbutton from the Application Toolbar. A session will be created for the recording, which can be processed from SM35 or SM37.

29

Page 30: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Creating Test Data from Recording:-

To test the data transfer program that you have created, you can create a data record in a sequential file. This data record contains all the field contents from the recording that are relevant to the data transfer in the format required by the data transfer program. It is therefore useful if you align the format of your conversion program data file with the format of the test file.

Procedure

1. Display the initial screen of the batch input recorder (Transaction SHDB). 2. Choose Overview.

The system displays an overview of all recordings. 3. Position the cursor on the relevant recording and choose Create test data from application

Toolbar.

4. Enter a test file and exit the dialog box by choosing Continue.You have now created a test file.

If the test file you have specified already exists, the system appends the new data record.

30

Page 31: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

If you do not specify the path, the system archives the test file in the working directory of the current application server.

 What to do if the test file is wanted in your local PC?

The test file is created in the presentation server. But, U want to create an excel file for the test data. For that, first create the test data in the application server and then download it in your local Pc and format it:

Creating test data file in application server: -

Do as instructed above and enter the name of the test file in the dialog box appearing:-

Run the following program to download it to local PC as a text file:-

Report ysub_transfer_appserv_PC.

data : begin of i_jump occurs 0, string(2000), end of i_jump.data : jump(2000).

selection-screen begin of block B1000 with frame. parameters : p_aps(20) type c. parameters : p_file like rlgrap-filename.selection-screen end of block B1000.

start-of-selection. open dataset p_aps for input . if sy-subrc ne 0. message i398(00) with 'Opening of file failed for' p_aps ' in application server'. leave list-processing. endif.end-of-selection. do. read dataset p_aps into i_jump. append i_jump. clear i_jump. if sy-subrc ne 0. exit.

31

Page 32: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

endif. enddo. close dataset p_aps.

if not i_jump[] is initial.

CALL FUNCTION 'WS_DOWNLOAD' EXPORTING FILENAME = p_file FILETYPE = 'ASC' TABLES data_tab = i_jump EXCEPTIONS INVALID_FILESIZE = 1 INVALID_TABLE_WIDTH = 2 INVALID_TYPE = 3 NO_BATCH = 4 UNKNOWN_ERROR = 5 GUI_REFUSE_FILETRANSFER = 6 CUSTOMER_ERROR = 7 OTHERS = 8 .IF sy-subrc <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.

message i398(00) with 'File successfully downloaded'.

endif.

Run the program. Enter the name of the test data in application server and name of the file in your PC where the test data is to be downloaded.

Execute the program.The file will be downloaded as text file in your PC.

Go to Microsoft Excel and open the file:

32

Page 33: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Open it as delimited file. Place the column separators properly and format the file properly.Insert column headers and save it as an excel file.

Generating Function Modules from Recording:

Display the initial screen of the batch input recorder (Transaction SHDB). Choose Overview.

The system displays an overview of all recordings. Position the cursor on the relevant recording and choose Create function module. On the subsequent dialog box, enter a function module name, a function group and a short

text for the function module. Exit the dialog box by choosing Continue.The system automatically creates the function module.

You have now generated a function module that you can use as an interface for your R/3 System. As well as information relevant for the data transfer, the function module's import interface has a parameter for each input field of the transaction recorded.

33

Page 34: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Using Function Modules PrerequisitesThe function module was generated from a recording made using the batch input recorder.ProcedureCall the function module. Supply the generic interface of the function module:

CTU : Flag whether the data is to be transferred using batch input method CALL TRANSACTION USING. The system generates a batch input session if this flag is not set.

MODE : Processing mode:

A Display all

E Display only errors

N No display

UPDATE : Update mode:

S Synchronous

A Asynchronous

L Local update

GROUP : (If CTU is already specified): Name of the batch input session to be generated

USER : (If CTU is already specified): User with whose authorizations the session is to be processed

KEEP : Specifies whether this session is to be deleted once it has been processed

HOLDDATE : Specifies the earliest processing date for the error session

NODATA : Defines the NODATA characterSupply the function module's special interface.For each input field that was filled when you recorded the transactions, the system creates an import parameter. The recorded value is used as the default value for this import parameter.  

Example scenario:

34

Page 35: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

A function module is created, which on called in ABAP Editor looks as follows:-

CALL FUNCTION 'YVENDLOAD'* EXPORTING* CTU = 'X'* MODE = 'N'* UPDATE = 'L'* GROUP =* USER =* KEEP =* HOLDDATE =* NODATA = '/'* BUKRS_001 = 'sdc1'* EKORG_002 = 'sdc1'* KTOKK_003 = 'kred'* ANRED_004 = 'Mr.'* NAME1_005 = 'Amartya Sengupta'* SORTL_006 = 'AMSE'* STRAS_007 = '2/3, Albert Road'* ORT01_008 = 'Calcutta'* PSTLZ_009 = '700037'* LAND1_010 = 'IN'

35

Page 36: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

* SPRAS_011 = 'EN'* AKONT_012 = '160000'* ZTERM_013 = '0001'* ZWELS_014 = 'C'* WAERS_015 = 'inr'* ZTERM_016 = '0001'* WEBRE_017 = 'X'* EKGRP_018 = '001'* PLIFZ_019 = '2'* IMPORTING* SUBRC =* TABLES* MESSTAB = .One can now use this function module.

36

Page 37: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Generating Data Transfer Programs

 PrerequisitesBefore you can generate a data transfer program, you must record the transactions using which the data is imported into the R/3 System. You use the batch input recorder to do this.ProcedureDisplay the initial screen of the batch input recorder (Transaction SHDB). Choose Overview.The system displays an overview of all recordings. Position the cursor on the relevant recording and choose Create program. On the following screen, specify a program name. You can also choose how the maintained field contents of the recorded screens are to be filled: · Transfer the values that were used during the recording. If you require a flexible data transfer, you must modify this program.· Set the parameters of the input values to be maintained and import these values from a data file. To set the parameters, the system creates a data structure and imports the data records from an external file into this data structure. The program assumes that the external file has been adapted to this data structure.If you have decided to set parameters for the input values to be maintained, it is useful if you create a test file when you generate the program. To do this, flag the checkbox and enter a name for the test file. For more information, see creating a test file. Choose Continue. The system displays the attribute screen of the program editor. Choose the relevant attributes here and save the program.

ResultYou have now generated a data transfer program that you can use to import data into the R/3 System. The program can execute the data transfer using batch input or CALL TRANSACTION.

Special features of SHDB F1 -, F4 - and self-programmed F1 - and F4 help ( PROCESS ON HELP-REQUEST,

PROCESS ON VALUE-REQUEST ) are not recorded. The same applies to all commands in the System and Help menus.

Error and warning dialogs are not recorded. This means that only the OK code field and the field contents that lead to successful further processing in the current screen.

" COMMIT WORK " in a transaction flow indicates the successful end of the transaction as regards batch input. The recording also ends successfully.

" LEAVE TO TRANSACTION " in the course of a transaction indicates that it is not suitable for batch input. The recording is terminated.

In Screen Painter screens, movements in the scrollbar are not recorded. Use the function keys F21-F24 for positioning.

 

37

Page 38: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

LSMW

38

Page 39: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Introduction

Data interface is a serious and complicated issue in implementation and maintenance of any SAP project, requiring considerable effort for interface programming. LSMW, abbreviation of Legacy System Migration Workbench is a tool provided by SAP to perform data upload or data transfer into SAP within a very short time. It breaks down the data transfer process into various sections, viz. mode of transfer, determination of structure and the fields required, mapping various fields into target structures, hard coding or programming values for the fields/taking them from flat file, reading the data from the flat file, check the read data, convert the data to get the assignment for all the fields required and finally posting the transaction or creating a BDC session for it. The whole process is accomplished with the click of radio buttons and push buttons, with a little bit of ABAP programming(where necessary). Other important transactions very closely related to interfacing are SHDB(Recording of transactions), SM35(Processing of BDC sessions) and so on. This material assumes that a programmer is using the utilities of LSMW for the first time and thus provides step-by-step guidance to use it.Material master loading is a test scenario over here, which shows how to proceed in the various sections of LSMW for successful data upload.

For any data upload/interface project, following factors need to be decided first:

Object to be transferred: Material master, vendor master, pricing info master, open purchase orders etc.

Transaction to be used for data upload, viz., MM01 for Material master, XK01 for Vendor Master, ME21N for purchase order and so on.

1. Necessary fields to be populated (including mandatory ones), their technical name and types along with length.

2. The mode of transfer, by batch input session or Direct input or Call transaction.

It is desirable that a programming should do a little bit of interface programming before starting LSMW, so that he/she can understand the sections of LSMW very easily.

39

Page 40: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

A little bit theory about LSMW

Data interfacing/uploading is a methodical approach that needs proper execution of the following steps:

1. Deciding the object to be transferred: viz., Material master, Vendor Master etc.2. Deciding the order of transfer ; for e.g., if you want to load the open purchase orders

from legacy to SAP, U have to follow the sequential steps for loading: Material MasterVendor MasterInfo RecordsPurchase Requisitions(if any)Open purchase orders.

(I assume that all your basic settings, viz. plant, storage locations, purchasing organizations and groups, etc are already loaded).

3. Deciding the transaction by which loading is to be performed.4. Deciding the fields to be populated and mapping them with various legacy data

fields.5. Deciding the conversion routines to process the legacy data so that various fields in

SAP R/3, which cannot be mapped directly with legacy, are properly populated.6. Considering the bulk of data to be loaded. For e.g., if there are only 5 materials in

legacy, better enter them manually into SAP R/3 system. If there are 100-50,000 records, try interface programming.

7. Extracting the data from the legacy system into a tab delimited flat file.8. Deciding the mode of transfer: whether to use batch input sessions/direct input.9. Examining SAP system to find out any pre-existing program to meet the

requirement. If absent, develop a new program/use LSMW.

LSMW logically breaks down the data transfer process as follows: -

40

Project

Subproject1 Subproject2 Subproject3

Object1 Object2 Object3

Page 41: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

A project can be anything, say, loading data from legacy to SAP R/3 for an organization can be considered as a single project. Sub-projects are further classifications, viz., loading materials related information can be a sub-project, loading FI related data can be another one and so on. Objects are the actual programs(automatically developed by SAP when LSMW is used) that need to be done; say, loading material master can be an object, loading vendor master can be another object and so on.

Without going into further details, I start describing the process of using LSMW. As we will be proceeding further, gradually the concept will become more clear.

Note :

While creating any object (say a field in a structure) / coding for a field / deleting a field etc., first go into Change mode by pressing the Change pushbutton from the Application Toolbar and then proceed. Otherwise, you won’t find any appropriate pushbutton to perform the tasks.

41

Page 42: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

LSMW FOR MATERIAL MASTER LOADING USING DIRECT INPUT

Follow the instructions to load material master into SAP R/3 from legacy.

Go to transaction : LSMW, create new object in existing project or create a new project, subproject and object and after that, press Execute pushbutton from the Application Toolbar to execute the object.

42

Case Scenario :

We shall be creating one raw material by transaction MM01 through LSMW.The fields we would like to populate are:

Material type (= ROH), hardcoded for all materialsIndustry sector (=1), hardcodedCompany Code(=SDC1), hardcoded for all materials Material Description, from flat fileMaterial group from flat fileUnit from flat file.GR Processing time and planned delivery time from flat file.MRP type(=ND), hardcodedValuation class from material group.Valuation catg = V(moving avg price)Batch control indicator from material group.

Page 43: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Maintain Source Attributes

1. Maintain the following settings in Maintain Source Attributes section (select the radiobutton and press Execute to enter into the section. Press Change key from Application Toolbar to enter your settings )

2. Save and come out.

Maintain Source Structure

3. Go to Maintain Source Structures Section.Create a structure of your own( if U are creating for the first time, click on Structures node and press Create pushbutton from the application toolbar In Change mode.). Save the structure and come out.

Maintain Source Fields

4. In the Maintain Source Fields section, create the columns which U will take from flat file( it is unnecessary to take those fields for which U will hardcode the value or your program will take some decision). You may or may not name them as they are in DDIC(determined by pressing F1 and then F9 in the field in the transaction). But, take care of their length.Their length should be at least

43

Page 44: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

minimum to contain the complete value from the flat file and their length should not exceed the field length in DDIC.

Note : If U are using LSMW for the first time, at first, click on the structure node and press Create button from the Application Toolbar to create the fields. To create a field below another field, click on the field and then press Create button to place a new field after the field in which U have positioned your cursor.Try to maintain the order of the fields by which they appear in the screens of the transaction.

Save and come out.

Maintain Structure Relations

5. Enter into Maintain Structure Relations, where U will basically assign your structure into the structure for the program. It may be that there are more than one structures for the transaction postings(decided by SAP standard program) and U will assign one or more of your structures(those U have created) to them. So, assigning is very important.

44

Page 45: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Click on a target structure.Click the Relationship pushbutton from the application toolbar to assign one of your structure to it. Sometimes, double clicking on the structure automatically gets the name of your structure assigned to it, particularly, if U have created a single structure. So, assign all the target structures in this way by your structures, save and come out.

Maintain Field Mappings and Conversion Rules

Enter into Maintain Field Mappings and Conversion Rules where U will assign field-by-field target structures with those of your structure. Actually, there are various options available over here:(a) You can assign field of the target structure with a field of your structure.

Click on a field of target structure and press the Source Field pushbutton from Application Toolbar. A list of the fields of your structure will appear. Double click on a field to assign it.

You can also get into the program mode for the field of the target structure(Double click on the field name and U will have the ABAP editor mode for the field. U can also make the assignment over there.

(b) You can hardcode some value for the field.

Enter into the ABAP editor mode for the field and assign the fixed value.Say, for this case we have fixed the field BUKRS(company code) with the value SDC1.

45

Page 46: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

© U can decide the value in the field by your program. For that, U have to write ABAP code for the field.

Say, for this case, we have decided that we shall put valuation class = ‘3000’ if the material group is for foreign newsprints(starts with NF).

46

Page 47: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

In this way, assign or hardcode or program for all the necessary fields for the transaction.Do not skip mandatory fields, particularly.Save and come out.

47

Page 48: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Fixed Values, Translations, User Defined Routines

In this section, U basically declare constants, subroutines etc, which U can use in the previous section in the codes for the fields. Say, U want to use a subroutine which U will use for some field, for eg., material group. For the simplest case, I have created a subroutine which will flash an information message : Foreign Newsprint if the material group starts with NF. So,I have created a subroutine :

The code I have written:

48

Page 49: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

I have utilized this subroutine in the code for material group in the Field mapping section:

49

Page 50: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

What will be the result ?

Whenever, system will convert the data, if any record in my flat file contains a material group starting with ‘NF’, the message will be shown.

Specify Files

In the Specify Files section, U specify the name and destination for the flat file. Say, in this case, I have placed the flat file(which is tab delimited, containing the columns for information as is specified in my structure. I have created an excel file first and then saved it as tab delimited text file).

I clicked on the node : Legacy Data and pressed the Create pushbutton from the application toolbar. A screen came where I filled in the informations about my flat file:

Check the Tabulator radio button if your file is tab delimited.Check the two checkboxes under heading : File structure. Click on the Record End indicator for text file. Check the radiobutton for ASCII file(which is true for most of the cases).

Press enter.

50

Page 51: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Now, when the system will read the data or convert the read data, it will need some file in your application server. System automatically creates the files and defaults their name in this section. You can keep the system defaulted files or delete them and assign a name of your own.

Save and come out of the section.

51

Page 52: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Assign Files

Basically, system assigns the file. All U have to do is to enter into the section:

If U want to re-assign a different flat file for source, U can delete the present assignment and re-assign a new name, save and come out.

(In most of the cases, enter into this section, press the Save key and come out).

Read Data

Here, system reads the data from the flat file into a file in the application server. The name of the file is the same as mentioned in the Specify Files section.When U enter into this section, the following selection-screen appears before U:

52

Page 53: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

In the Transaction number select-option, U can specify which records are to be read and processed. If U want all the records in your flat file to be processed, leave this blank.Check or uncheck the checkboxes for amount and date fields as necessary(say, your date field in the flat file is in the format : dd.mm.yyyy. In that case, uncheck the checkbox for date). Press the Execute pushbutton(F8).

A report will appear showing U the number of records read:

53

Page 54: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Display Read Data

In this section, U get a scope to view how the data are read, ( particularly U may be interested to see how data read are mapped in different fields ). For that, enter into this section, execute itAnd U will get a summary report showing the data read. U can click on a line to see how the data read are mapped into different fields.

Screenshots are given below for your convenience:

54

Page 55: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

55

Click on a line to get the field mappings

Here, U can see how the dataread from the flat file are mapped into different fields of your structure

Page 56: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Convert Data

In this section, the data read is converted into another file with the values for the fields read (from the read file) and taking the hardcoded values/programmed values of other fields(Done in Field Mappings sections).

Mode of usage is similar to that of Reading Data.(Try it by yourself).

Display Converted Data

Mode is similar to displaying Read Data. Here, you get the values assigned to other fields also(hardcoded or programmed). See the converted data very carefully to trace the errors and remove them.

Start Direct Input Program

This is basically posting the transaction with the data read.

Select the Program radio button.

56

Page 57: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Click on the Physical File name radiobutton. Set the lock mode to E or N.Execute it.

Then a status report will be issued by the system, informing about the result of the postings-whether postings are successful or not. Error and warning messages are also displayed.

57

Page 58: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

58

Page 59: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Material Consumption Loading byBatch Input Session Using LSMW

1.Go to transaction LSMW. Write the name of the project, subproject and object and click on Create pushbutton at the application toolbar.

2. Enter the descriptions and press enter.

3. Click on the Execute pushbutton at the application toolbar.A series of job list will appear before you, which U have to execute serially.

59

Page 60: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

60

Page 61: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Maintain Object Attributes

Select the radiobutton and press on the Execute pushbutton from Application Toolbar.

Click on the Change pushbutton from application toolbar. Click on Batch Input recordingAnd enter the name of the recording if it exists. If not, click on the Recording Overview in the body.

Select the Recordings node and press Create pushbutton from the application toolbar.

61

Page 62: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

A popup screen will appear. Enter the name of the recording and description. Press enter.

62

Page 63: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Another popup screen will appear. Enter the transaction code which U want to record.

Record the transaction and save the recordings.After saving the recording, a screen showing the recording will come. Press the Default All pushbutton from Application Toolbar.

Save and press F3 to come out.

Then, write the name of the recording U created in the Recording field in the screen for attributes, save it and come out.

63

Page 64: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Maintain source structures

Click on the radio button and press Execute pushbutton.

Choose the Structure node and press the Create pushbutton from the application toolbar (go to change mode first by clicking the Change pushbutton from the application toolbar first). Write the name of the structure U want to create and give the descriptions.Create sub structures under the structure if U want.

Press the Save key and come out by pressing F3.

Maintain Source Fields

Click on the radio button and press Execute pushbutton.

Choose the Structure node and press the Create pushbutton from the application toolbar(go to change mode first by clicking the Change pushbutton from the application toolbar first) to create the fields of the source structure. First create a field, place the cursor over it and then press Create pushbutton to create further fields.Mention data types and data length and description along with the name of the fields.

Press the Save key and come out by pressing F3.

64

Page 65: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Maintain Structure Relations

Here, basically one have to maintain relations between source and target structures.The system may default it for U or U may have to create it.

Press the Save key and come out by pressing F3.

Maintain Field Mapping and Conversion Rules

Click on the radio button and press Execute pushbutton.

Here, U will see the list of fields of ysubcons1 (structure name is the name of the BDC recording). Select a field and click on the Source field pushbutton from application toolbar and choose from the fields in your structure.

Assign for all the fields. Save and come out.

65

Page 66: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Specify Files

Here you have to mention the flat file and its address. Select the radiobutton and press Execute pushbutton.

Choose the legacy data node and the appropriate one depending on the address of the file ; whether in PC or in Application server.

Press Create pushbutton from the Application Toolbar.

A property sheet for file will come. In this case, my flat file is a tab delimited file residing under C:. So, I have populated the property sheet of the file as: -

66

Page 67: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Save and come out.

67

Page 68: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Assign Files

Here, basically U have to assign the files. In this case, it is automatically assigned as there is a single structure of simple type and only one flat file.

Read File

The system reads the data from the flat file to the file in the application server specified in the Assign files section (system defaults the name of files for reading and converting data in the application server).

Click on the radio button and press Execute pushbutton from application toolbar. A selection screen will come. Check off the checkboxes for amount and date if they are not in that format.

Then, press the Execute pushbutton from Application Toolbar.The system will show U the number of records read.

Convert Data

Here, the data read will be converted into a file by proper mapping.Select the appropriate radio button and press the Execute pushbutton.

68

Page 69: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Press the Execute pushbutton when the system will furnish a report showing the number of records read and converted.

Come out after seeing.

Display Converted Data

Here, U can see the field mappings. Select the radiobutton and execute. Fill/not fill in the selection screen and execute the report that will show converted data in list form:

69

Page 70: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Click/double click on any line to see the mappings:

70

Page 71: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Create Batch Input Session

Here, U basically write the name of the batch input session and the number of transactions to be recorded per batch input session in the selection screen which appears immediately after clicking the appropriate radiobutton and pressing Execute pushbutton from Application Toolbar.

Then press the Execute pushbutton for creating the batch input session.

Run Batch Input Session:

U can run/process the batch input session from here or from transaction SM35.

71

Page 72: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Programming Data Transfer Using SE38

Basic logic: -

1. Upload the data from a file in the presentation server (using function module: UPLOAD or WS_UPLOAD), preferable tab-delimited into an internal table of same structure of the file format.

2. Populate an internal table of structure BDCDATA and use it to post transactions by CALL TRANSACTION or create a batch input session using function modules: BDC_OPEN_GROUP, BDC_INSERT and finally BDC_CLOSE_GROUP.

Information on BDCDATA structure: -

An internal table of structure BDCDATA is to be populated with data. It has five columns: -

PROGRAM: name of the program DYNPRO: screen number DYNBEGIN: ‘X’ for start of a new screen FNAM: Technical name of the field FVAL: Field value

Populating internal table for bdcdata: -

Declare an internal table of structure BDCDATA: -

DATA: I_bdcdata like BDCDATA INITIAL SIZE 0 WITH HEADER LINE.

Initialize it before U populates it with values for a transaction.

Before populating the internal table for fields of a screen, one must maintain the module pool name, screen number and flag = ‘X’.

I_bdcdata-program = <program>.I_bdcdata-dynpro = <screen number>.I_bdcdata-dynbegin = ‘X’.Append I_bdcdata.Clear I_bdcdata.

For each field where values are to be assigned, insert an entry to the internal table.

I_bdcdata-fnam = <technical name of the field>.I_bdcdata-fval = <value in the field>.Append I_bdcdata.Clear I_bdcdata.

If the field is in a step loop or a table control, one must also specify the lines in which the input is to be entered. The field name extension displays the line number.

72

Page 73: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

I_bdcdata-fnam = ‘mseg-matnr (1)’.

To place the cursor in a particular field, enter the cursor position by filling field FNAM with value BDC_CURSOR and transferring into the field FVAL the technical name of the field.

I_bdcdata-fnam = ‘BDC_CURSOR’.I_BDCDATA-FVAL = <TECHNICAL NAME OF THE FIELD>.APPEND I_BDCDATA.

To place the cursor on a field in a step loop or table control, one must specify the line: -

I_bdcdata-fval = ‘fieldx (5)’.

Specify which action is to be executed. One must determine the function code for the action to be triggered and assign this to the field FVAL; FNAM should be ‘BDC_OKCODE’.

I_bdcdata-fnam = ‘BDC_OKCODE’.I_bdcdata-fval = ‘=INS’.Append I_bdcdata.

Execute 3rd to 6th step mentioned above for each screen in the transaction.

After the last screen in the transaction, internal table (of structure BDCDATA) is filled with all the values required. Developer can now use this table to create an entry in the batch input session or can submit a transaction using CALL TRANSACTION.

73

Page 74: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Using CALL TRANSACTION for data transfer

Syntax:

CALL TRANSACTION <transaction code>USING <internal table of structure BDCDATA filled with values>MODE < mode>UPDATE <update>MESSAGES INTO <msg-tab>.

The mode can be: -

A Display all screensE Display errors onlyN No display of screens

Update Mode can be: -

S SynchronousAn AsynchronousL Local update

‘A’: - Called transaction does not wait for any updates it produces to be completed. It simply passes the updates to SAP Update service.

Not recommended for processing large amount of data. This is because the called transaction does not receive any completion message from the update module in asynchronous updating. The calling data transfer program, in turn, cannot determine whether a called transaction ended with successful update of the database or not.

IF asynchronous updating is used, one needs to use the update management facility (SM12) to check whether updates have been terminated abnormally during session processing.

S: - Called transaction waits for any update that it produces to be completed. Execution is slower than asynchronous one as the called transaction waits for updates to be completed. However, the called transaction is able to return any update error message that occurs in the program. It is much easier for developer to analyze and recover the errors

L: - If local updating is done, the update of the database will not be done in a separate process, but in the process of the calling program.

MESSAGES parameter: -.All system messages issued during a CALL TRANSACTION USING are written into an internal table <msg-tab> of structure BDCMSGCOLL.

Return codes (sy-subrc): -

0 Successful<=1000 Error in dialog program>1000 Batch input error

74

Page 75: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

System fields: -

Sy-msgid message idSy-msgty message type (E/I/W/S/A/X)Sy-msgno message numberSy-msgv1 Message variable 1Sy-msgv2 Message variable 2Sy-msgv3 Message variable 3Sy-msgv4 Message variable 4

Structure of BDCMSGCOLL structure: -

Error Analysis and Restart Capability

CALL TRANSACTION USING does not provide any special handling for incorrect transactions. There is no restart capability for transactions that contains error or produce update failures.

Using update mode S and checking sy-subrc can handle incorrect transactions. If it is other than 0, do the following: -

Write or save the message table.

75

Page 76: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

Use the BDCDATA table to generate a batch input session for the faulty transaction. Then analyze the faulty transaction to correct and post it.

BATCH INPUT SESSIONS (SM35 /SM37)

Batch input sessions contain transaction code with screens. They are generated from transaction recording or from programs.

Users can process batch input sessions in foreground/background/display on error mode.If errors occur during processing a batch input session, they can be processed later.

Batch input sessions can be processed again.

Creating Batch Input Sessions

To create a batch input session, function module: BDC_OPEN_GROUP is used.

CALL FUNCTION 'BDC_OPEN_GROUP' EXPORTING CLIENT = SY-MANDT GROUP = FILLER12 HOLDDATE = FILLER8 KEEP = FILLER1 EXCEPTIONS CLIENT_INVALID = 1 DESTINATION_INVALID = 2 GROUP_INVALID = 3 GROUP_IS_LOCKED = 4 HOLDDATE_INVALID = 5 INTERNAL_ERROR = 6 QUEUE_ERROR = 7 RUNNING = 8 SYSTEM_LOCK_ERROR = 9 USER_INVALID = 10 OTHERS = 11 .IF sy-subrc <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.

Import Parameters:-

CLIENT Client for processing itGROUP Session nameHOLDDATE Lock DateKEEP Delete session after processingUSER BDC user name

To insert records in a batch input session, BDC_INSERT function module is used:

76

Page 77: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

CALL FUNCTION 'BDC_INSERT' EXPORTING TCODE = <transaction code> dynprotab = <internal table for BDC> EXCEPTIONS INTERNAL_ERROR = 1 NOT_OPEN = 2 QUEUE_ERROR = 3 TCODE_INVALID = 4 PRINTING_INVALID = 5 POSTING_INVALID = 6 OTHERS = 7 .IF sy-subrc <> 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.

Import parameters:

TCODE Transaction codeDYNPROTAB BDC Table

To close the session, function module BDC_CLOSE_GROUP is used:-

CALL FUNCTION 'BDC_CLOSE_GROUP' EXCEPTIONS NOT_OPEN = 1 QUEUE_ERROR = 2 OTHERS = 3 .IF sy-subrc <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.

77

Page 78: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

EXAMPLE PROGRAM ON CALL TRANSACTION

REPORT YSUBBDCVENDORUPLOAD.

tables : t100.

Define : fill.

i_bdcdata-program = &1.i_bdcdata-dynpro = &2.i_bdcdata-dynbegin = &3.i_bdcdata-fnam = &4.i_bdcdata-fval = &5.

append i_bdcdata.clear i_bdcdata.

end-of-definition.

Data: begin of i_flatfile occurs 0,Bukrs like RF02K-BUKRS,Ekorg like RF02K-EKORG,Ktokk like RF02K-KTOKK,Anred like LFA1-ANRED,Name1 like LFA1-NAME1,Sortl like LFA1-SORTL,Stras like LFA1-STRAS,Ort01 like LFA1-ORT01,Pstlz like LFA1-PSTLZ,Land1 like LFA1-LAND1,Spras like LFA1-SPRAS,akont like LFB1-AKONT,zwels like LFB1-ZWELS,zterm like LFM1-ZTERM,waers like LFM1-WAERS,webre like LFM1-WEBRE,ekgrp like LFM1-EKGRP,plifz like LFM1-PLIFZ,

end of i_flatfile.

data : i_bdcdata like bdcdata occurs 0 with header line, messtab like bdcmsgcoll occurs 0 with header line.

DATA : begin of i_mode occurs 0, mode(2) type c, text(18) type c, end of i_mode.

data : g_choice like sy-tabix, g_subrc like sy-subrc, g_string(300) type c .

**********************************************************

78

Page 79: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

* Selection-screen design***********************************************************selection-screen begin of block B1000 with frame title text-001.

parameters : p_file like rlgrap-filename obligatory memory id MEM1, P_MODE like ctu_params-dismode obligatory, P_UPDATE like ctu_params-updmode obligatory.

selection-screen skip 1. parameters : p_log as checkbox default 'X'.

selection-screen end of block B1000.

at selection-screen on value-request for p_file.

CALL FUNCTION 'F4_FILENAME' EXPORTING PROGRAM_NAME = SYST-CPROG DYNPRO_NUMBER = SYST-DYNNR* FIELD_NAME = IMPORTING FILE_NAME = p_file .

START-OF-SELECTION.

perform file_upload.

loop at i_flatfile. PERFORM POPULATE_BDCTABLE. perform call_transaction.

if g_subrc ne 0.

write: / 'CALL_TRANSACTION', 'XK01', 'returncode:', g_subrc, 'RECORD:', sy-index.

loop at messtab. select single * from t100 where sprsl = messtab-msgspra and arbgb = messtab-msgid and msgnr = messtab-msgnr.

if sy-subrc = 0. g_string = t100-text. if g_string cs '&1'. replace '&1' with messtab-msgv1 into g_string. replace '&2' with messtab-msgv2 into g_string. replace '&3' with messtab-msgv3 into g_string. replace '&4' with messtab-msgv4 into g_string. else. replace '&' with messtab-msgv1 into g_string.

79

Page 80: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

replace '&' with messtab-msgv2 into g_string. replace '&' with messtab-msgv3 into g_string. replace '&' with messtab-msgv4 into g_string. endif. condense g_string. write: / messtab-msgtyp, g_string(250). else. write: / messtab. endif. endloop.

else. describe table messtab lines g_subrc. read table messtab index g_subrc. concatenate messtab-msgv1 'Created in database' into g_string separated by space. write:/ g_string(250). endif.

endloop.*&---------------------------------------------------------------------**& Form POPULATE_BDCTABLE*&---------------------------------------------------------------------** text*----------------------------------------------------------------------** --> p1 text* <-- p2 text*----------------------------------------------------------------------*form POPULATE_BDCTABLE.fill : 'SAPMF02K' '0100' 'X' '' '', '' '' '' 'RF02K-BUKRS' i_flatfile-bukrs, '' '' '' 'RF02K-EKORG' i_flatfile-ekorg, '' '' '' 'RF02K-KTOKK' i_flatfile-ktokk, '' '' '' 'bdc_okcode' '/00', 'SAPMF02K' '0110' 'X' '' '', '' '' '' 'LFA1-NAME1' i_flatfile-name1, '' '' '' 'LFA1-SORTL' i_flatfile-sortl, '' '' '' 'LFA1-STRAS' i_flatfile-stras, '' '' '' 'LFA1-ORT01' i_flatfile-ort01, '' '' '' 'LFA1-PSTLZ' i_flatfile-pstlz, '' '' '' 'LFA1-LAND1' i_flatfile-land1, '' '' '' 'LFA1-SPRAS' i_flatfile-spras, '' '' '' 'bdc_okcode' '/00', 'SAPMF02K' '0120' 'X' '' '', '' '' '' 'BDC_OKCODE' '/00', 'SAPMF02K' '0130' 'X' '' '', '' '' '' 'BDC_OKCODE' '=ENTR', 'SAPMF02K' '0210' 'X' '' '', '' '' '' 'LFB1-AKONT' i_flatfile-AKONT, '' '' '' 'BDC_OKCODE' '/00', 'SAPMF02K' '0215' 'X' '' '', '' '' '' 'LFB1-ZTERM' i_flatfile-ZTERM, '' '' '' 'LFB1-ZWELS' i_flatfile-ZWELS, '' '' '' 'BDC_OKCODE' '/00',

80

Page 81: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

'SAPMF02K' '0220' 'X' '' '', '' '' '' 'BDC_OKCODE' '/00', 'SAPMF02K' '0310' 'X' '' '', '' '' '' 'LFM1-WAERS' i_flatfile-waers, '' '' '' 'LFM1-ZTERM' i_flatfile-zterm, '' '' '' 'LFM1-WEBRE' i_flatfile-webre, '' '' '' 'LFM1-EKGRP' i_flatfile-ekgrp, '' '' '' 'LFM1-PLIFZ' i_flatfile-plifz, '' '' '' 'BDC_OKCODE' '/00', 'SAPMF02K' '0320' 'X' '' '', '' '' '' 'BDC_OKCODE' '=ENTR', 'SAPMF02K' '0300' 'X' '' '', '' '' '' 'BDC_OKCODE' '=YES'.

endform. " POPULATE_BDCTABLE*&---------------------------------------------------------------------**& Form call_transaction*&---------------------------------------------------------------------** text*----------------------------------------------------------------------** --> p1 text* <-- p2 text*----------------------------------------------------------------------*form call_transaction.call transaction 'XK01' using i_bdcdata mode p_mode update p_update messages into messtab. g_subrc = sy-subrc.

endform. " call_transaction*&---------------------------------------------------------------------**& Form file_upload*&---------------------------------------------------------------------** text*----------------------------------------------------------------------** --> p1 text* <-- p2 text*----------------------------------------------------------------------*form file_upload.CALL FUNCTION 'UPLOAD' EXPORTING FILENAME = p_file FILETYPE = 'DAT' TABLES data_tab = i_flatfile EXCEPTIONS CONVERSION_ERROR = 1 INVALID_TABLE_WIDTH = 2 INVALID_TYPE = 3 NO_BATCH = 4 UNKNOWN_ERROR = 5 GUI_REFUSE_FILETRANSFER = 6 OTHERS = 7 .IF sy-subrc <> 0.

81

Page 82: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.

if i_flatfile[] is initial. leave list-processing. endif.

endform. " file_upload

82

Page 83: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

EXAMPLE PROGRAM CREATING BATCH INPUT SESSION

REPORT /tpisp/ypcxxxo_hr_master_load NO STANDARD PAGE HEADING LINE-SIZE 132 LINE-COUNT 65 MESSAGE-ID /tpisp/ygenlsdc.*----------------------------------------------------------------------** Program Description: This program uploads a tab delimited file from* the presentation server, creates a BDC session* for the transaction PA30 (Maintain HR Master* Data). A series of infotypes can be populated* for an employee and each infotype contains separa* te screens with different program, screen number* and okcodes. Screen number and okcode for an* infotype varies with country, action and other* factors. Similarly, list of fields to be popu* lated for an infotype also varies with change in* different conditions, common being country and* action type.** This program is totally dynamic and is totally* dependent on three custom developed tables:-** YPHR_INFO : List of infotypes for a country and* action with relevant grouping of the infotypes* to be posted together(under same info group)* Default country is indicated by 00** YPHR_PROG : Program, screen number and okcodes* of the infotype screens mentioned in YPHR_INFO** YPHR_FIELDS : List of fields in an infotype* screen with their order of population as menti-* oned in tab-delimited data file** TRANSPORT NR: DG2K919705* DEVELOPER: Subhendu Majumdar* CREATION DATE: 2002-10-30* SCR Number: SCR20020920094133R561* DER Number: DER20020627154213R415 (GGSC017)* PGM Number:**----------------------------------------------------------------------** - REVISION HISTORY -------------------------------------------------*** TRANSPORT NR:* DEVELOPER:* DATE APPLIED: YYYY-MM-DD* SCR NUMBER: <ToolBox object ID>* DER NUMBER: <ToolBox object ID>* DESCRIPTION:* <Please include the DER description>* <and if relevant Tivoli number, TDR number, etc.>**

83

Page 84: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

*----------------------------------------------------------------------**eject* Constants------------------------------------------------------------*CONSTANTS : c_nodata(1) TYPE c VALUE space , "No data indicator c_flexst(2) TYPE c VALUE 'FE' , "To check existence of data file c_fltyp LIKE rlgrap-filetype VALUE 'ASC', "File type of data file c_default(2) TYPE c VALUE '00' , "Code for default country c_tcode LIKE tstc-tcode VALUE 'PA30', "Transaction used in BDC session c_initial(2) TYPE c VALUE '01' , "Initial line number c_start(1) TYPE c VALUE 'X' , "New screen ind. for BDC session c_pct1 TYPE i VALUE 10 , " % expressing current exec.status c_pct2 TYPE i VALUE 20 , " % expressing current exec.status c_pct3 TYPE i VALUE 30 , " % expressing current exec.status c_pct4 TYPE i VALUE 100 , " % expressing current exec.status c_sep TYPE c VALUE '0' , "Separator in BDC session name c_tab TYPE x VALUE '09' , "Tab character c_first LIKE yphr_fields-fldseq VALUE '0' , "Column order for infotype c_okcode(10) TYPE c VALUE 'BDC_OKCODE'.

*eject* Types----------------------------------------------------------------*

TYPES :* Type for internal table to contain records from flat file BEGIN OF ty_flatfile, text(2500) TYPE c, "Column with record of data file END OF ty_flatfile,

* Type for internal table to contain flat file records after* extracting country and action information BEGIN OF ty_data, country LIKE yphr_info-country, "Country code action LIKE yphr_info-action, "Action type text(2500) TYPE c, "Column with record of data file recno LIKE sy-tabix, "Record no. from data file END OF ty_data,

* Type for internal table to contain field related information BEGIN OF ty_fields, action LIKE yphr_fields-action, "Action type country LIKE yphr_fields-country, "Country code infogrp LIKE yphr_fields-infogrp, "Info group scrseq LIKE yphr_fields-scrseq, "Sequence of screen appearance fldseq LIKE yphr_fields-fldseq, "Sequence of fields in data file scrfld LIKE

84

Page 85: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

yphr_fields-scrfld, "Technical name of screen field fldval LIKE yphr_fields-fldval, "Default value for the field value LIKE yphr_fields-fldval, "Value from data file END OF ty_fields,

* Type for error reporting BEGIN OF ty_error, recno(6) TYPE c, "Record number as in data file action(7) TYPE c, "Action type country(8) TYPE c, "Country code reason(70) TYPE c, "Reason for rejection END OF ty_error,

* Type for internal table to be used for BDC session reporting BEGIN OF ty_session, session(20) TYPE c, "BDC Session name cnttrans(11) TYPE c, "No. of transactions in session END OF ty_session.

*eject* INTERNAL TABLES------------------------------------------------------*

DATA :* Internal table to contain records from flat file i_flatfile TYPE STANDARD TABLE OF ty_flatfile INITIAL SIZE 0 WITH HEADER LINE,

* Internal table to contain flat file records after* extracting country and action information i_data TYPE STANDARD TABLE OF ty_data INITIAL SIZE 0 WITH HEADER LINE,

* Internal table to contain infotype related information* for all the countries and actions found in the flat file* and extracted in the internal table I_DATA i_info LIKE STANDARD TABLE OF yphr_info INITIAL SIZE 0 WITH HEADER LINE,

* Internal table to contain program related information* for all the countries and actions found in the flat file* and extracted in the internal table I_DATA i_prog LIKE STANDARD TABLE OF yphr_prog INITIAL SIZE 0 WITH HEADER LINE,

* Internal table to contain field related information* for all the countries and actions found in the flat file* and extracted in the internal table I_DATA i_fields TYPE STANDARD TABLE OF ty_fields INITIAL SIZE 0 WITH HEADER LINE,

* To be used to fetch infotypes for individual row of flat* file from master internal table i_info i_info_indv LIKE STANDARD TABLE OF yphr_info INITIAL SIZE 0 WITH HEADER LINE,

85

Page 86: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

* To be used to fetch programs for individual row of flat* file from master internal table i_prog i_prog_indv LIKE STANDARD TABLE OF yphr_prog INITIAL SIZE 0 WITH HEADER LINE,

* To be used to fetch fields for individual row of flat* file from master internal table i_fields i_fields_indv TYPE STANDARD TABLE OF ty_fields INITIAL SIZE 0 WITH HEADER LINE,

* Internal table for error reporting i_error TYPE STANDARD TABLE OF ty_error INITIAL SIZE 0 WITH HEADER LINE,

* Internal table for BDC Session reporting i_session TYPE STANDARD TABLE OF ty_session INITIAL SIZE 0 WITH HEADER LINE,

* Internal table like BDCDATA structure to be used to populate* BDC session i_bdcdata LIKE STANDARD TABLE OF bdcdata INITIAL SIZE 0 WITH HEADER LINE.

*eject* VARIABLES------------------------------------------------------------*

DATA : g_status(80) TYPE c, "Status information g_pct TYPE i, " Percentage of completion g_curr TYPE i, "Current record no being processed g_sess LIKE apqi-groupid. "Contains modified BDC Session name

*CNT-------------------------------------------------------------------*DATA : cnt_process TYPE i, "Determines population of infotypes "for a record cnt_lines TYPE i, "Total records to be processed cnt_trans TYPE i VALUE 0, "No of transactions per BDC session cnt_session TYPE i. "Counts no. of session prepared

*FLG-------------------------------------------------------------------*DATA : flg_open TYPE i VALUE 0, "Informs about open BDC session flg_found TYPE i. "Flag indicating population for"an infogrp

*eject* SELECTION SCREEN-----------------------------------------------------*

************************BLOCK B1000************************************SELECTION-SCREEN BEGIN OF BLOCK b1000 WITH FRAME TITLE text-001."Information for Data FilePARAMETERS : p_file LIKE rlgrap-filename OBLIGATORY."Name of the Data file

86

Page 87: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

SELECTION-SCREEN END OF BLOCK b1000.

************************BLOCK B2000*********************************SELECTION-SCREEN BEGIN OF BLOCK b2000 WITH FRAME TITLE text-002."Information for BDC SessionPARAMETERS : p_sess(10) TYPE c OBLIGATORY, "Name of the BDC session p_max(4) TYPE n OBLIGATORY, "Max no of transactions/session p_keep AS CHECKBOX. "To keep session after process

SELECTION-SCREEN END OF BLOCK b2000.

*eject* INITIALIZATION-------------------------------------------------------*INITIALIZATION.* Clearing and refreshing internal tables and variables to be used PERFORM f1000_refresh_variables.

*eject* AT SELECTION SCREEN--------------------------------------------------*AT SELECTION-SCREEN.* Validates the existence of the flat file PERFORM f2000_check_file.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.* User can select the flat file on pressing F4 help in the* name of the flat file field PERFORM f3000_provide_values.

*eject* START OF SELECTION---------------------------------------------------*START-OF-SELECTION.* Upload of records into internal table from tab-delimited flat file PERFORM f4000_load_data.

* Extracting the country and actions from records in flat file PERFORM f5000_extract_country_action.

* Retrieving records from YPHR_INFO, YPHR_PROG AND YPHR_FIELDS based* on the country and actions found for all records in data file PERFORM f6000_master_program_field_dtl.

*eject*END-OF-SELECTION------------------------------------------------------*END-OF-SELECTION.* Processes individual records of flat file and populates BDC table PERFORM f7000_indv_record_process.* Display information on BDC session and discarded records PERFORM f8000_display_reports.

*eject*-----------------------FORM DECLARATIONS------------------------------*

87

Page 88: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

*&---------------------------------------------------------------------**& Form F1000_REFRESH_VARIABLES*&---------------------------------------------------------------------** Clearing and refreshing internal tables and variables to be used.*----------------------------------------------------------------------*FORM f1000_refresh_variables.

* Clearing and refreshing internal tables to be used CLEAR : i_flatfile , i_prog , i_info , i_fields , i_prog_indv , i_data , i_fields_indv, i_info_indv , i_error , i_session , i_bdcdata .

REFRESH : i_flatfile , i_prog , i_info , i_fields , i_prog_indv , i_data , i_info_indv , i_fields_indv, i_error , i_session , i_bdcdata .

* Clearing other global variables CLEAR : flg_open , flg_found , g_pct , g_curr , g_status , g_sess , cnt_process , cnt_trans , cnt_lines , cnt_session .

ENDFORM. " F1000_REFRESH_VARIABLES

*&---------------------------------------------------------------------**& Form F2000_CHECK_FILE*&---------------------------------------------------------------------** Validates the existence of the data file*----------------------------------------------------------------------*FORM f2000_check_file.

DATA : l_retval TYPE i. "To check existence of data file

88

Page 89: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

CALL FUNCTION 'WS_QUERY' EXPORTING filename = p_file "Name of flat file query = c_flexst "FE : file existence check IMPORTING return = l_retval "Retval justifying existence EXCEPTIONS inv_query = 1 no_batch = 2 frontend_error = 3 OTHERS = 4.

IF sy-subrc <> 0. MESSAGE e018 WITH 'Error in querying the flat file'(003). ENDIF.

IF l_retval NE 1. " Flat file does not exist MESSAGE e018 WITH 'No such file exists'(004). ENDIF.

ENDFORM. " F2000_CHECK_FILE

*&---------------------------------------------------------------------**& Form F3000_PROVIDE_VALUES*&---------------------------------------------------------------------** User can select the flat file from local PC on pressing F4 help in the* name of the flat file field*----------------------------------------------------------------------*FORM f3000_provide_values.

CALL FUNCTION 'F4_FILENAME' EXPORTING program_name = sy-cprog "Current Program dynpro_number = sy-dynnr "Current screen IMPORTING file_name = p_file. "Parameter for name of flat file

* No sy-subrc check is required after call to this function module

ENDFORM. " F3000_PROVIDE_VALUES

*&---------------------------------------------------------------------**& Form F4000_LOAD_DATA*&---------------------------------------------------------------------** Upload of records into internal table from tab-delimited flat file*----------------------------------------------------------------------*FORM f4000_load_data.

* Displays current execution stage of the program g_status = 'Uploading records from data file...'(005). g_pct = c_pct1. "10% PERFORM f4100_inform_status.

89

Page 90: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

* Loading tab delimited text file from local PC CALL FUNCTION 'WS_UPLOAD' EXPORTING filename = p_file "Name of flat file filetype = c_fltyp "ASC TABLES data_tab = i_flatfile EXCEPTIONS conversion_error = 1 file_open_error = 2 file_read_error = 3 invalid_type = 4 no_batch = 5 unknown_error = 6 invalid_table_width = 7 gui_refuse_filetransfer = 8 customer_error = 9 OTHERS = 10.

* If error arises in loading records from data file, program will* flash an information message and go back to selection-screen IF sy-subrc <> 0. MESSAGE i018 WITH 'Error in uploading the data file'(006). LEAVE LIST-PROCESSING. ENDIF.

* If flat file is found with nil records, program will flash an* information message and go back to selection-screen IF i_flatfile[] IS INITIAL. MESSAGE i018 WITH 'Data file is empty'(007). LEAVE LIST-PROCESSING. ENDIF.

ENDFORM. " F4000_LOAD_DATA

*&---------------------------------------------------------------------**& Form F4100_INFORM_STATUS*&---------------------------------------------------------------------**Informs about the current progress status of the program*----------------------------------------------------------------------*FORM f4100_inform_status. CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' EXPORTING percentage = g_pct text = g_status.

ENDFORM. " F4100_INFORM_STATUS

*&---------------------------------------------------------------------**& Form F5000_EXTRACT_COUNTRY_ACTION*&---------------------------------------------------------------------** Extracting the country and actions from records in flat file.* First column contains the country code and the second one contains* action type*----------------------------------------------------------------------*

90

Page 91: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

FORM f5000_extract_country_action.

DATA : l_fldval LIKE bdcdata-fval, "Field value l_stpos TYPE i, "Starting posn l_pos LIKE sy-fdpos, "Matched posn l_text(2500) TYPE c. "To contain each record "of data file

* Informs about the current execution stage g_status = 'Extracting country and actions...'(008). g_pct = c_pct2. "20% PERFORM f4100_inform_status.

* Each record of data file is processed to get country and action LOOP AT i_flatfile.

CLEAR : l_fldval, l_stpos, l_pos.

l_text = i_flatfile-text. IF NOT L_TEXT IS INITIAL. SEARCH l_text FOR c_tab. "Search for first tab

IF sy-subrc = 0. "If tab is found l_pos = sy-fdpos. IF l_pos gt 0.* Value in 1st column of flat file l_fldval = l_text+l_stpos(l_pos) .* First column contains country code i_data-country = l_fldval. ENDIF. ENDIF.

ADD 1 TO l_pos. SHIFT l_text LEFT BY l_pos PLACES.

SEARCH l_text FOR c_tab. "Search for second tab IF sy-subrc = 0. l_pos = sy-fdpos. IF l_pos gt 0.* Value in 2nd column of flat file l_fldval = l_text+l_stpos(l_pos) .* Second column contains action i_data-action = l_fldval. ENDIF. ENDIF.* Take the full information in a record i_data-text = i_flatfile-text.

* Load the extracted information of flat file into internal* table I_DATA if country and action are found IF NOT ( I_DATA-COUNTRY EQ C_NODATA AND I_DATA-ACTION EQ C_NODATA ) .

i_data-recno = sy-tabix. "Note record no in data file

91

Page 92: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

APPEND i_data. CLEAR i_data.

ELSE.* If both country and action fields are not populated, the record* is not considered for processing and is included in error reporting i_error-recno = sy-tabix. i_error-reason = 'Country and action not properly filled'(009). APPEND i_error. CLEAR i_error. ENDIF. ENDIF. ENDLOOP.

DESCRIBE TABLE i_data LINES cnt_lines.

ENDFORM. " F5000_EXTRACT_COUNTRY_ACTION

*&---------------------------------------------------------------------**& Form F6000_MASTER_PROGRAM_FIELD_DTL*&---------------------------------------------------------------------** Based on country and action types extracted from flat file* records, information are retrieved from YPHR_INFO,YPHR_PROG* AND YPHR_FIELDS, which will be utilised for individual record* processing of the flat file later on*----------------------------------------------------------------------*FORM f6000_master_program_field_dtl.

IF NOT i_data[] IS INITIAL.* Display program execution status g_status = 'Retrieving master infogrp/program/fields'(010). g_pct = c_pct3. "30% PERFORM f4100_inform_status.

* Fetching of infotype related information* for the actions and countries in the flat file* Records of default country is also taken SELECT * FROM yphr_info INTO TABLE i_info FOR ALL ENTRIES IN i_data WHERE action = i_data-action "Action AND ( country = i_data-country "Country OR country = c_default ). "00 ELSE.* If no suitable dataset are extracted from flat file, program* flashes an information message and returns to selection-screen MESSAGE i018 WITH 'No records found for processing from data file'(011). LEAVE LIST-PROCESSING. ENDIF.

IF NOT i_info[] IS INITIAL. SORT i_info BY action country infogrp seqno.* Fetching information about programs,screens and okcodes* of infotypes

92

Page 93: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

SELECT * INTO TABLE i_prog FROM yphr_prog FOR ALL ENTRIES IN i_info WHERE action = i_info-action "Action AND country = i_info-country. "Country

* If program related information are not found,program will flash* an information message and return to selection-screen

IF i_prog[] IS INITIAL. MESSAGE i018 WITH 'Programs not maintained in table YPHR_PROG'(012). LEAVE LIST-PROCESSING. ENDIF.

SORT i_prog BY action country infogrp scrseq.* Fetching information about the fields and their sequence SELECT action "Action country "Country infogrp "Info grp scrseq "Screen sequence fldseq "Field order scrfld "Tech.name :field fldval "Default value INTO TABLE i_fields FROM yphr_fields FOR ALL ENTRIES IN i_prog WHERE action = i_prog-action AND country = i_prog-country AND infogrp = i_prog-infogrp AND scrseq = i_prog-scrseq.

* If field related information are not found,program will flash* an information message and return to selection-screen IF i_fields[] IS INITIAL. MESSAGE i018 WITH 'Fields not maintained in table YPHR_FIELDS'(013). LEAVE LIST-PROCESSING. ENDIF. ELSE.* If no infotype related information are found, program will flash* an information message and return to selection-screen MESSAGE i018 WITH 'Infotypes not maintained in table YPHR_INFO'(014). LEAVE LIST-PROCESSING. ENDIF.

ENDFORM. " F6000_MASTER_PROGRAM_FIELD_DTL

*&---------------------------------------------------------------------**& Form F7000_INDV_RECORD_PROCESS*&---------------------------------------------------------------------** Processes individual records of flat file and populates BDC table*----------------------------------------------------------------------*FORM f7000_indv_record_process.

93

Page 94: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

SORT i_prog BY action country infogrp. g_curr = 0.

* Start processing for each and every record of flat file LOOP AT i_data. g_curr = g_curr + 1.

* Fetch the INFOTYPE related information from internal table* of infotype for the country of the record in flat file PERFORM f7100_get_info.

* Skip the record in the loop if no infotype related information* are found from YPHR_INFO IF i_info_indv[] IS INITIAL. CONTINUE. ENDIF.

* Fetch the program related information from internal table* of programs for the country of the record in flat file PERFORM f7200_get_programs.

* Skip the record in the loop if no program related information* are found from YPHR_INFO IF i_prog_indv[] IS INITIAL. CONTINUE. ENDIF.

* Now, from the records of programs selected, find out the* fields and their sequential order matching country,action,* info group and screen sequence PERFORM f7300_get_fields.

* Skip the record in the loop if no field related information* are found from YPHR_FIELDS

IF i_fields_indv[] IS INITIAL. CONTINUE. ENDIF.

* Updating internal table for fields with value from data file* matching the order PERFORM f7400_get_values_for_fields.

* If no of transactions inserted into BDC session is greater* than the maximum limit specified by the user, then close* the BDC session. PERFORM f7500_close_bdc_session.

AT LAST.* If any session is open, then close it PERFORM f7600_close_session_if_open. ENDAT.

ENDLOOP.

94

Page 95: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

ENDFORM. " F7000_INDV_RECORD_PROCESS

*&---------------------------------------------------------------------**& Form F7100_GET_INFO*&---------------------------------------------------------------------** Retrieve infotype related records from master internal table for* programs matching the action and country in the flat file.* If action is matched, but no record exists for the country,* take default country(00) into consideration

* Here, basically we are selecting infotype related information* for the country and action of a record in the data file.* To avoid database access for each and every record, at first,* infotype related information for all actions and countries are* captured in internal table i_info. Now, this table is read to* populate i_info_indv for each row in the data file*----------------------------------------------------------------------*FORM f7100_get_info.

REFRESH i_info_indv. CLEAR i_info_indv.

* Records in i_info matching the country and action* with that in data file are taken into consideration LOOP AT i_info WHERE action = i_data-action AND country = i_data-country . i_info_indv = i_info. APPEND i_info_indv. CLEAR i_info_indv. ENDLOOP.

SORT i_info_indv BY action country infogrp.

* Records in i_info matching the action with that in data* file and default country are taken into consideration* only when entries with the country in data file are not found LOOP AT i_info WHERE action = i_data-action AND country = c_default .

READ TABLE i_info_indv WITH KEY action = i_data-action country = i_data-country infogrp = i_info-infogrp BINARY SEARCH. IF sy-subrc NE 0. i_info_indv = i_info. APPEND i_info_indv. CLEAR i_info_indv. SORT i_info_indv BY action country infogrp. ENDIF. ENDLOOP.

*If no infotype related information are not found for a row in data*file it is not processed and is included in error reporting IF i_info_indv[] IS INITIAL. i_error-recno = i_data-recno.

95

Page 96: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

i_error-action = i_data-action. i_error-country = i_data-country. i_error-reason = 'Infotypes not maintained in table YPHR_INFO'(014). APPEND i_error. CLEAR i_error. ENDIF.

ENDFORM. " F7100_GET_INFO

*&---------------------------------------------------------------------**& Form F7200_GET_PROGRAMS*&---------------------------------------------------------------------** Fetch the program related information from internal table* of programs based on entries in i_info_indv*----------------------------------------------------------------------*FORM f7200_get_programs.

CLEAR : i_prog_indv, i_fields_indv. REFRESH : i_prog_indv, i_fields_indv.

SORT i_prog BY action country infogrp scrseq. SORT i_info_indv BY action country infogrp.

LOOP AT i_info_indv. AT NEW infogrp. LOOP AT i_prog WHERE action = i_info_indv-action AND country = i_info_indv-country AND infogrp = i_info_indv-infogrp .

i_prog_indv = i_prog. APPEND i_prog_indv. CLEAR i_prog_indv. ENDLOOP. ENDAT. ENDLOOP.*If no program related information are not found for a row in data file*it is not processed and is included in error reporting IF i_prog_indv[] IS INITIAL. i_error-recno = i_data-recno. i_error-action = i_data-action. i_error-country = i_data-country. i_error-reason = 'Programs not maintained in table YPHR_PROG'(012). APPEND i_error. CLEAR i_error. ENDIF.

ENDFORM. " F7200_GET_PROGRAMS

*&---------------------------------------------------------------------**& Form F7300_GET_FIELDS

96

Page 97: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

*&---------------------------------------------------------------------** Now, from the records of programs selected, find out the* fields and their sequential order matching country,action,* info group and screen sequence*----------------------------------------------------------------------*FORM f7300_get_fields.

SORT i_prog_indv BY action country infogrp scrseq.

* Based on the program related information for a row in data file,* search i_fields ( which is the master internal table containing field* records for all the actions and countries in data file ) and populate* i_fields_indv. LOOP AT i_prog_indv. LOOP AT i_fields WHERE action = i_prog_indv-action AND country = i_prog_indv-country AND infogrp = i_prog_indv-infogrp AND scrseq = i_prog_indv-scrseq.

i_fields_indv = i_fields. APPEND i_fields_indv. CLEAR i_fields_indv. ENDLOOP. ENDLOOP.* If table YPHR_FIELDS is not properly maintained IF i_fields_indv[] IS INITIAL. i_error-recno = i_data-recno. i_error-action = i_data-action. i_error-country = i_data-country. i_error-reason = 'Fields not maintained in table YPHR_FIELDS'(013). APPEND i_error. CLEAR i_error. ENDIF.

ENDFORM. " F7300_GET_FIELDS

*&---------------------------------------------------------------------**& Form F7400_GET_VALUES_FOR_FIELDS*&---------------------------------------------------------------------** Reading the flat file and getting values for fields matching* the order*----------------------------------------------------------------------*FORM f7400_get_values_for_fields.

* Find out the values of the columns in the flat file by cutting the* row at tab positions and populate i_fields_indv table PERFORM f7410_populate_fields_by_value.

IF NOT ( i_prog_indv[] IS INITIAL AND i_fields_indv[] IS INITIAL ).* Start of BDC table population cnt_process = 0. LOOP AT i_prog_indv.

97

Page 98: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

AT NEW infogrp.* At the start of new info group processing, the program* will check whether the initial screen for the info group* is populated or not. If it is populated, it will look for* values in the other screens for the info groups. If records* are found, then population for that info group will commence,* else, that info group will be skipped. PERFORM f7420_decide_for_infogrp. ENDAT.

IF flg_found = 1. cnt_process = cnt_process + 1. PERFORM f7430_load_initials.* Populates the fields for the info type PERFORM f7440_populate_bdc_for_fields. ENDIF.

AT END OF infogrp.* Insert into BDC session PERFORM f7450_insert_into_bdc_session. ENDAT. ENDLOOP.

IF cnt_process EQ 0. i_error-recno = i_data-recno. i_error-action = i_data-action. i_error-country = i_data-country. i_error-reason = 'No infotype information found in flat file'(016). APPEND i_error. CLEAR i_error. ENDIF.

ENDIF.ENDFORM. " F7400_GET_VALUES_FOR_FIELDS

*&---------------------------------------------------------------------**& Form F7410_POPULATE_FIELDS_BY_VALUE*&---------------------------------------------------------------------** Find out the values of the columns in the flat file by cutting the* row at tab positions and populate i_fields_indv table*----------------------------------------------------------------------*FORM f7410_populate_fields_by_value.

DATA : l_fldval LIKE bdcdata-fval, " BDC field value l_stpos TYPE i, " Starting posn l_pos LIKE sy-fdpos, " Posn of search found l_text(2500) TYPE c, " Contents of record l_counter TYPE i. " Finds field order

IF NOT i_fields_indv[] IS INITIAL. CLEAR : l_counter, l_stpos , l_pos , l_counter.

98

Page 99: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

l_text = i_data-text. flg_found = 1.

* Start reading the flat file. g_counter maintains the order* of the field in the flat file WHILE flg_found EQ 1. ADD 1 TO l_counter. SEARCH l_text FOR c_tab.

IF sy-subrc EQ 0. l_pos = sy-fdpos. IF l_pos NE l_stpos. " If value is found l_fldval = l_text+l_stpos(l_pos) . ELSE.* If no value is found, use space as nodata l_fldval = c_nodata. ENDIF.

* Modify the values from data file matching the order i_fields_indv-value = l_fldval. MODIFY i_fields_indv TRANSPORTING value WHERE fldseq = l_counter.

ADD 1 TO l_pos. SHIFT l_text LEFT BY l_pos PLACES. ELSE.* For the last column, take the value as such l_fldval = l_text. i_fields_indv-value = l_fldval. MODIFY i_fields_indv TRANSPORTING value WHERE fldseq = l_counter. flg_found = 0. ENDIF. ENDWHILE.

SORT i_fields_indv BY infogrp scrseq fldseq. SORT i_prog_indv BY action infogrp scrseq. ENDIF.ENDFORM. " F7410_POPULATE_FIELDS_BY_VALUE

*&---------------------------------------------------------------------**& Form F7420_DECIDE_FOR_INFOGRP*&---------------------------------------------------------------------** At the start of new info group processing, the program* will check whether the initial screen for the info group* is populated or not. If it is populated, it will look for* values in the other screens for the info groups. If records* are found, then population for that info group will commence,* else, that info group will be skipped.*----------------------------------------------------------------------*FORM f7420_decide_for_infogrp. LOOP AT i_fields_indv WHERE infogrp = i_prog_indv-infogrp AND action = i_prog_indv-action AND country = i_prog_indv-country

99

Page 100: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

AND scrfld CP '*BEGDA*' AND value NE c_nodata. ENDLOOP.

IF sy-subrc EQ 0. "If value is found in Starting "Date field, start processing flg_found = 1.* Opens BDC session PERFORM f7421_bdc_open_group. ELSE. flg_found = 0. ENDIF.ENDFORM. " F7420_DECIDE_FOR_INFOGRP

*&---------------------------------------------------------------------**& Form f7421_BDC_OPEN_GROUP*&---------------------------------------------------------------------** If no BDC session is opened, it opens a BDC session.* The name of the session and decision about keeping the session after* successful processing, as is decided by the user in the selection* screen, is obeyed while opening the session*----------------------------------------------------------------------*FORM f7421_bdc_open_group. DATA : l_cntsess(2) TYPE c, " Counts session l_user LIKE apqi-userid. " Userid

IF flg_open = 0.* Count the number of session and use the number as suffix in the* BDC session name ADD 1 TO cnt_session. l_cntsess = cnt_session.

* If session counter is le 9, pad a '0' before it IF l_cntsess LE '9'. CONCATENATE c_sep "0 as separator l_cntsess "Number of session INTO l_cntsess. CONDENSE l_cntsess NO-GAPS. ENDIF.

* Prepare the name of the BDC session by padding user entry for the* BDC session name and session counter CONCATENATE p_sess "Param. for session name l_cntsess "No, of session INTO g_sess. CONDENSE g_sess NO-GAPS. l_user = sy-uname.

CALL FUNCTION 'BDC_OPEN_GROUP' EXPORTING client = sy-mandt "Current client group = g_sess "Session name keep = p_keep "To keep or not after proc user = l_user "Current user EXCEPTIONS

100

Page 101: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

client_invalid = 1 destination_invalid = 2 group_invalid = 3 group_is_locked = 4 holddate_invalid = 5 internal_error = 6 queue_error = 7 running = 8 system_lock_error = 9 user_invalid = 10 OTHERS = 11. IF sy-subrc <> 0. MESSAGE i018 WITH 'Error in opening BDC session'(018). LEAVE LIST-PROCESSING. ENDIF. flg_open = 1. ENDIF.ENDFORM. " f7421_BDC_OPEN_GROUP

*&---------------------------------------------------------------------**& Form F7430_LOAD_INITIALS*&---------------------------------------------------------------------** Loads information about program,screen number, okcode* of an info type in bdc table*----------------------------------------------------------------------*FORM f7430_load_initials. PERFORM f7431_load_bdcdata USING : i_prog_indv-programm i_prog_indv-dynpro c_start " X '' ''.

* Loads information about the okcode for the screen PERFORM f7431_load_bdcdata USING : '' '' '' c_okcode i_prog_indv-okcode .ENDFORM. " F7430_LOAD_INITIALS

*&---------------------------------------------------------------------**& Form F7431_LOAD_BDCDATA*&---------------------------------------------------------------------** Loads the bdc table*----------------------------------------------------------------------*FORM f7431_load_bdcdata USING l_programm l_dynpro l_start l_fnam l_fval.

i_bdcdata-program = l_programm. i_bdcdata-dynpro = l_dynpro.

101

Page 102: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

i_bdcdata-dynbegin = l_start. i_bdcdata-fnam = l_fnam. i_bdcdata-fval = l_fval. APPEND i_bdcdata. CLEAR i_bdcdata.

ENDFORM. " F7431_LOAD_BDCDATA

*&---------------------------------------------------------------------**& Form F7440_POPULATE_BDC_FOR_FIELDS*&---------------------------------------------------------------------** Populates the fields for the info type*----------------------------------------------------------------------*FORM f7440_populate_bdc_for_fields.

SORT i_fields_indv BY scrseq fldseq. SORT i_info BY action country infogrp seqno.

LOOP AT i_fields_indv WHERE infogrp = i_prog_indv-infogrp AND action = i_prog_indv-action AND country = i_prog_indv-country AND scrseq = i_prog_indv-scrseq.

* Get the infotype for initial screen of infr group from the* first line of yphr_info matching action country and info group

IF i_fields_indv-scrseq = c_initial "01 AND i_fields_indv-fldseq = c_first. "0

READ TABLE i_info WITH KEY action = i_fields_indv-action country = i_fields_indv-country infogrp = i_fields_indv-infogrp BINARY SEARCH.

IF sy-subrc EQ 0. i_fields_indv-value = i_info-infty. ENDIF. ENDIF.

IF i_fields_indv-value NE c_nodata. PERFORM f7431_load_bdcdata USING : '' '' '' i_fields_indv-scrfld i_fields_indv-value .

ELSEIF i_fields_indv-fldval NE c_nodata. PERFORM f7431_load_bdcdata USING : '' '' '' i_fields_indv-scrfld i_fields_indv-fldval .

ENDIF.

102

Page 103: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

ENDLOOP.ENDFORM. " F7440_POPULATE_BDC_FOR_FIELDS

*&---------------------------------------------------------------------**& Form F7450_INSERT_INTO_BDC_SESSION*&---------------------------------------------------------------------** Internal table populated with records for BDC are inserted into* batch input session*----------------------------------------------------------------------*FORM f7450_insert_into_bdc_session. IF flg_found = 1. cnt_trans = cnt_trans + 1. CALL FUNCTION 'BDC_INSERT' EXPORTING tcode = c_tcode "PA30 TABLES dynprotab = i_bdcdata EXCEPTIONS internal_error = 1 not_open = 2 queue_error = 3 tcode_invalid = 4 printing_invalid = 5 posting_invalid = 6 OTHERS = 7.

IF sy-subrc <> 0. MESSAGE i018 WITH 'Error in inserting records into BDC session'(019). LEAVE LIST-PROCESSING. ENDIF. ENDIF. REFRESH i_bdcdata.

ENDFORM. " F7450_INSERT_INTO_BDC_SESSION*&---------------------------------------------------------------------**& Form F7500_CLOSE_BDC_SESSION*&---------------------------------------------------------------------** If no of transactions inserted into BDC session is greater* than the maximum limit specified by the user, then close* the BDC session if it is open.*----------------------------------------------------------------------*FORM f7500_close_bdc_session. IF cnt_trans GT p_max. " if exceeds maxm limit IF flg_open = 1. " if open CALL FUNCTION 'BDC_CLOSE_GROUP' EXCEPTIONS not_open = 1 queue_error = 2 OTHERS = 3. IF sy-subrc <> 0. MESSAGE i018 WITH 'Error in closing BDC session'(017). LEAVE LIST-PROCESSING. ENDIF.* Record the session information for reporting i_session-session = g_sess.

103

Page 104: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

i_session-cnttrans = cnt_trans. APPEND i_session. CLEAR i_session.* Initialize flag open indicator and transaction counter variables flg_open = 0. cnt_trans = 0. ENDIF. ENDIF.

ENDFORM. " F7500_CLOSE_BDC_SESSION*&---------------------------------------------------------------------**& Form F7600_CLOSE_SESSION_IF_OPEN*&---------------------------------------------------------------------** If any session is open, then close it*----------------------------------------------------------------------*FORM f7600_close_session_if_open. IF flg_open = 1. CALL FUNCTION 'BDC_CLOSE_GROUP' EXCEPTIONS not_open = 1 queue_error = 2 OTHERS = 3. IF sy-subrc <> 0. MESSAGE i018 WITH 'Error in closing BDC session'(017). LEAVE LIST-PROCESSING. ENDIF.* Record the session information for reporting i_session-session = g_sess. i_session-cnttrans = cnt_trans. APPEND i_session. CLEAR i_session.

flg_open = 0. ENDIF.ENDFORM. " F7600_CLOSE_SESSION_IF_OPEN

*&---------------------------------------------------------------------**& Form f8000_display_reports*&---------------------------------------------------------------------** Display information on BDC session and discarded records*----------------------------------------------------------------------*FORM f8000_display_reports.

* Determination of % completeness of data file record processing g_pct = c_pct4. "100% g_status = 'Preparing log report...'(015).* Display current stage of program execution PERFORM f4100_inform_status.

IF NOT i_session[] IS INITIAL.* Display BDC session summary PERFORM f8100_display_session_report. ENDIF.

IF NOT i_error[] IS INITIAL.* Display details of discarded records

104

Page 105: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

PERFORM f8200_display_error_report. ENDIF.ENDFORM. " f8000_display_reports

*&---------------------------------------------------------------------**& Form F8100_DISPLAY_SESSION_REPORT*&---------------------------------------------------------------------** Displays a brief on BDC Sessions populated*----------------------------------------------------------------------*FORM f8100_display_session_report. DATA : l_totlines TYPE i, l_toterrors TYPE i, l_success TYPE i.

WRITE:/20 'SYNOPSIS OF POPULATING BATCH INPUT SESSION'(031) NO-GAP, ' WITH INFOTYPE INFORMATIONS OF EMPLOYEES'(032). SKIP 2. WRITE:/2 sy-datum, /2 sy-uzeit. SKIP 2.

DESCRIBE TABLE i_flatfile LINES l_totlines. DESCRIBE TABLE i_error LINES l_toterrors.

l_success = l_totlines - l_toterrors. WRITE:/5 'Total number of records in Data file'(020) COLOR 3, 50 l_totlines LEFT-JUSTIFIED COLOR 2. WRITE:/5 'Records passed to BDC Session '(021) COLOR 3, 50 l_success LEFT-JUSTIFIED COLOR 2. WRITE:/5 'Records discarded'(022) COLOR 3, 50 l_toterrors LEFT-JUSTIFIED COLOR 2.

SKIP 3. WRITE:/2 'List of Sessions:-'(030). SKIP 1. WRITE:/25 'Session'(023) COLOR 2, 45 'Transaction'(024) COLOR 2. LOOP AT i_session. WRITE :/25 i_session-session LEFT-JUSTIFIED COLOR 4, 45 i_session-cnttrans LEFT-JUSTIFIED COLOR 5. ENDLOOP. SKIP 5.ENDFORM. " F8100_DISPLAY_SESSION_REPORT

*&---------------------------------------------------------------------**& Form F8200_DISPLAY_ERROR_REPORT*&---------------------------------------------------------------------** Shows a report on the records not processed*----------------------------------------------------------------------*FORM f8200_display_error_report.

WRITE:/2 'Details of Records Discarded:-'(025). SKIP 1.

WRITE:/3 'Recno'(026) COLOR 2 NO-GAP , 'Action'(027) COLOR 3 NO-GAP ,

105

Page 106: Data Transfer

Data Interface Programming Author: Subhendu Majumdar

'Country'(028) COLOR 4 NO-GAP , 'Reason'(029) COLOR 6. SORT i_error BY recno. LOOP AT i_error. WRITE:/3 i_error-recno LEFT-JUSTIFIED COLOR 2, 9 i_error-action LEFT-JUSTIFIED COLOR 3, 16 i_error-country LEFT-JUSTIFIED COLOR 4, 24 i_error-reason LEFT-JUSTIFIED COLOR 5. ENDLOOP.

ENDFORM. " f8200_DISPLAY_ERROR_REPORT

106