segunda-feira, 16 de janeiro de 2017

Enhance LSMW Effectiveness using ABAP code

This document explains, how to apply the ABAP code to LSMW objects to meet the business requirements, Data Validations, Error Summary Report and to navigate through the LSMW transaction, and find the areas where we can write our own ABAP code detailed by an example.

1. Introduction

The LSM Workbench is an SAP tool that supports single or periodic data transfer from non-SAP to SAP system. The tool supports conversion of data of the legacy system in a convenient way. The data can then be imported into the SAP system via Standard Batch / Direct Input, Batch Input Recording, BAPI or IDOC Methods.

Many times there could be an instance to validate migrating data through coding.  This can be achieved through ABAP code in LSMW to enhance the functionality. This document explains how to apply the ABAP code to LSMW objects to meet the business requirements, Data Validations, Error summary report and how to navigate through the LSMW transaction to find the areas where we can write our own ABAP code by detailed example.
2. ABAP Coding areas inside LSMW

ABAP code can be inserted in two areas in the LSMW transaction.
  1. User-Defined Routines
  2. Field Mapping and Conversion Rules
The first area is the administration screen for user-defined routines. ABAP code that is written here is done so in the form of a subroutine, and can be reused by all LSMW objects within the project in which the user-defined routine was created. This is ideal for creating code that has the potential for being used by other objects within a project. For example
The second area in which we can insert ABAP code is in the maintain field mapping and conversion rules step of the LSMW. When ABAP code is inserted there, it applies only to objects in which it was inserted and cannot be reused by other objects. An example of this would be writing code that performs a specific data check applicable only to the Infotype for which the object was created.
2.1 View Processing areas
The processing areas (in Field Mapping and Conversion rules step) are not visible by default. We need to activate these areas by selecting the required processing area from Extras -> Layout option as shown below.
Now all the processing areas will available as shown below.

2.2 Events / Processing Areas

ABAP coding can be inserted at specific processing times.
Processing Area
Processing Time
Default setting
1
__GLOBAL_DATA__
like TOP INCLUDE, Here we can declare the variables, internal tables, constants etc.
(blank)
2
__BEGIN_OF_PROCESSING_
Before the beginning of data processing                             
(blank)
3
__BEGIN_OF_TRANSACTION_
Before the beginning of transaction data processing                       
(blank)
  1. 3.1
__BEGIN_OF_RECORD__Before applying the conversion rules for a source structureInitialize the structure  <segment> (Name of target structure)Batch Input, Direct Input:<segment> = init_<segment>.BAPI, IDoc:g_edidd_segnam = ‘…’.g_edidd_segnum = ‘….’.g_edidd_psgnum = ‘……’.g_edidd_hlevel = ‘..’.Clear <segment>.
  1. 3.2
__END_OF_RECORD_After applying the conversion rules for a source structure
Transfer_record.
4
__END_OF_TRANSACTION__
After finishing transaction processing                       
Transfer_transaction.
5
__END_OF_PROCESSING__
After finishing data processing                             
(blank)
2.3 Global Functions

The LSM Workbench provides a series of functions that can be used in any position of the ABAP coding.
Global function
Description
transfer_record.
Transfers the current record (i.e. for the current target structure) to the output buffer.
transfer_transaction.
Writes the current transaction to an output file. All records of the output buffer are transferred to the output file.
skip_record.
The current record is not transferred to the output buffer.
skip_transaction.
The current transaction is not written to the output file.
To insert Global function in coding, select Insert -> Global Function as shown below.
3. Example with Sample Code

Let’s take a simple example to change the employee First and Last Name. This is not a business requirement indeed, but, taking into consideration a simple scenario, for a better understanding, is explained in the following section. This example mainly will help you to write the custom code in LSMW.
This LSMW is to change the Employee’s First and Last Name in Infotype 0002 (Personal Data). Based on the Person ID, the program picks up the PERNR from the Infotype 0709 (considers multiple personnel numbers of the same employee, irrespective of the statuses i.e. Active, Inactive, Withdrawn and so on) and updates all PERNR’s in the respective Infotype 0002 i.e. Employee’s First Name and Last Name.
Points to be taken into consideration:
  1. Validate whether the provided Person ID is valid i.e. the Person ID specified in the flat file exists in the system or not. This is handled in __BEGIN_OF_TRANSACTION__ processing block.
  2. If the Person ID is invalid, the record is sent to the error Internal Table and further process is skipped. This is also handled in __BEGIN_OF_TRANSACTION__ processing block. Then the Error Person ID’s will be downloaded to a spread sheet once the conversion is done. This is handled in __END_OF_PROCESSING__ processing block.
  3. Based on the input data file Person ID, the program should pick up the PERNR/PERNR’s from the Infotype 0709 and update all PERNR’s. This is handled in __END_OF_RECORD__ processing block. This processing block is called for all available PERNR for the Person ID and additional records are inserted to output records LSMW buffer.
1. Maintain Object Attributes
2. Maintain Source Structures
3. Maintain Source Fields
Input File:
Assume USER01 exist in the system and valid, XYZXYZ is an invalid Person ID.

4. Maintain Structure Relations

5. Maintain Field Mapping and Conversion Rules
Select Change Mode.
Double click on __GLOBAL_DATA__ block, ABAP Editor will be opened. This is like TOP INCLUDE, declare the required variables, structures and internal tables here.

Double click on __BEGIN_OF_PROCESSING__ block (this block will trigger Convert data step execution once).  Declare Selection Screen related statements like Parameters, Select-options etc here. This will be appeared in Convert Data step selection screen.

Output:

Double click on __BEGIN_OF_TRANSACTION__ block (this block will trigger every new transaction, say for example if the input data file contains 10 records then 10 times this block will trigger).  Declare the Validation logic here.
SC20.jpg
Here the select query checks the Person ID is exist in the system or not. If it is not exist, error table is updated and the particular entry is skipped for further processing using skip_transaction global function. If it is exist, All PERNR’s (Active, Inactive if any) against the Person ID will be stored in LT_PERNR table.
In every source structure __BEGIN_OF_RECORD__ and __END_OF_RECORD__ routines are available. These routines executes before and after the conversion rules at each structure level.  For eg, _end_of_record_ can be used to call ‘transfer_record’ as many times as the particular structure is required to be inserted. This routine is used in many business cases like extend a Material to multiple plants etc.
Double click on __END_OF_RECORD__ block (this block will trigger after applying the conversion rules for a source structure).
The first transfer_record statement is default.  Here additional transfer_record statement is used to insert multiple records for the same Person ID’s PERNR.
Double click on __END_OF_PROCESSING__ block (this block will trigger end of every new transaction). Write the code to download and display the error records as shown below.
6. Specify Files

7. Assign Files
8. Read Data

Execute the step to read the data from flat file.

9. Convert Data

Execute the step, here additional selection parameter ERR_PATH is available to select the error file download path (This is declared in _BEGIN_OF_PROCESSING__ block).

Execute

Screen shot of downloaded Excel file

10. Continue with the processing (Create batch input session and Run batch input Session) to update the valid PERNR’s.
4. Debugging LSMW Code

We can debug LSMW object by putting a hard or soft breakpoint.
In Maintain Field Mapping and Conversion Rules, We can code BREAK-POINT at the very first field / where ever we need to debug. Then the program execution will break when it reaches there.
We can also put soft breakpoints in the system generated code. To view the generated code, select Display Conversion Program step and execute.

If this step is not visible, Go to Extras -> User Menu and activate.
Thanks,
Ramanathan Maniraman
Project Lead, SAP Practice
iGATE Global Solutions Ltd.
Fonte: https://blogs.sap.com/2013/07/11/enhance-lsmw-effectiveness-using-abap-code/

Nenhum comentário:

Postar um comentário