SAP LSMW step by step tutorial (Upload Vendor Master)
Author: Jaya Prakash
In this example, I will take you through a simple example using a custom development approach to upload data into SAP for Vendor Master.
For example – You have a file(or multiple files) from legacy system and you need to change the Vendor Name and contact persons in SAP using XK02 transaction. This is just an example and may not be a use case.
You need to define some rules for Legacy system download for example – a record identifier, a unique link.
Record Identifier – STYPE – This will let us know what kind of data we have in that line. In this case 1 means its the line is for header information and is of following format - STYPE, REC_LINK, Vendor Number, Vendor Name
STYPE = 2, means we have Vendor CONTACT information and in following format - STYPE, REC_LINK, First Name, Last Name.
AND we get a single file as above.
In this step, we configure the basic attributes of data load – e.g. one time or periodic data transfer, which object to load (Vendor Master, Material Master) and its method. SAP provides all the fundamental objects and their associated programs either Direct Input or Batch Input. In few objects, one or more options may be available and you may need to choose which one to go for. Further, ‘Intermediate Document’ (Idoc) may also be used. If the R/3 System does not provide any suitable batch or direct input program, you can use the batch input recorder to create a user-specific class of migration objects. We will show this with a custom program using Direct Input method.
Pre-steps to be performed before
Create Structures as the flat file
Table - SXDA0 – Create a record like this
SAP has comprehensive documentation of the load programs (Direct or Batch Input) how the information is to be structured in one or more levels for the business object under consideration. The Legacy data must be organised in similar fashion in the input file.
Data for more that one structures may be available in a single file. Before completing the step, it is recommended to go through the SAP documentation. The source/legacy structures to be mentioned here are only identifiers and do not need to reside in the database dictionary.
In this step, the source/legacy fields must be entered in each structures. You may use any of the following copy facility, highlighted to import the structures.
In case of multiple structures in the same file, you need to set the value of record type identifier (STYPE) in this step.
The source field may also be added individually or Maintain Source Fields in Table Form by clicking the TABLE Maintenance button.
You should automatically find the target structures with the configuration we have done in SXDA tables. Use the button highlighted to assign source structures to Target.
In this step, source/legacy fields in each structure are assigned to R/3 fields. It may be done individually for each R/3 field, which being time-consuming for complex structures, you may use automatic mapping utility (Extras > Auto-Field mapping).
You specify the conversion rule to be used to convert a field into the corresponding R/3 field. For this, you can use predefined conversion rules or create your own conversion rules in the editor. You may use the buttons ‘Initial’, ‘Constant’, ‘Move’ & ‘Fixed Value’ to assign different values. You may also assign different rules by using the button ‘Rule’.
On completion of this step, SAP generates the conversion program. Menu Extras > Hide and Display Fields/Code will give further details of the generated programs in the LSMW screens. For further flexibility, you can create global data if you need R/3 table contents and/or variables for the conversion rules and create your own routines. Its always good to keep all the options checked.
__GLOBAL_DATA__ Global Data Definitions and Declarations - Allows you to declare global variables that can be used across all the forms within the object
__BEGIN_OF_PROCESSING__ Before Data Processing Starts - These code areas allow for writing custom code before and after the migration of the data. Each of these blocks are called once during the program run.
__BEGIN_OF_TRANSACTION__ Before Transaction Processing Starts - These code areas allow for writing custom code before and after the migration of each object. For eg, for each Vendor that is updated, you can update to a z table in the End_of_transaction.
Begin_of_record, End_of_record – These are for the custom routines 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.
If it is needed to convert a number of legacy fields in a particular way before assigning it to the R/3 field, the conversion rule may be written in the form of a routine and may be re-used.
It will ask for number of parameters:
You can write some code which this routine does.
I will create one more routine which will convert the Name to upper case.
You may now go back to the previous step and assign these routines to the appropriate fields. I have now assigned the routine to NAME field to have it in UPPER CASE.
Here the file name for the legacy data is to be specified. The file can reside in either user PC or in the Application Server. For background processing, the input file must be located in the Application Server. Access to presentation server files is only possible when you are working online.
For last two (Read & Convert Data), file names are defaulted by SAP and are respectively <Project>_<SubProject>_<Object>.lsmw.read and <Project>_<SubProject>_<Object>.lsmw.conv. You may however change them but it is not recommended.
Here the source structures are mapped to the files.
Execute this step and run the program.
Display Read Data
Optional - In this step, you can display all or a part of the read data in table form. Clicking on a line displays all information for this line in a clear way. The same happens when you click on Field contents. Change display allows to select either a one-line or multi-line view.
Display color palette displays the colors for the individual hierarchy levels. You can check here if all your records are uploaded.
Here you can specify the transaction number which implies the record numbers.
Remarks: If you marked one or several source fields as selection parameters when defining the source fields, these fields are also offered as selection parameters.
Additional Function for BAPI/IDoc
Display Converted Data
Optional – Here you can see how the data looks like before you perform the actual upload. Here I see the user routine to translate to upper case has worked. But if you see some issues, then you can change the conversion rules and execute the Convert Data step again.
Start Direct Input Program
Here I use SHDB recording and create a program to upload data into SAP. For Debugging LSMW, you can put break points in the program which uploads the data for any troubleshooting.
You can also activate the user menu - Display Read Program and Display Conversion Program and put break points to understand any issues during data upload and conversion.
*&---------------------------------------------------------------------* *& Report ZSTP_XK02_PRG *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT zstp_xk02_prg. TABLES: /sapdmc/lsmemory. INCLUDE bdcrecx1. TYPES: BEGIN OF ty_infile, text TYPE c LENGTH 999, END OF ty_infile. DATA: lv_filename TYPE tumls_filename, ls_xk02_hdr TYPE zstp_xk02_hdr, i_hdr TYPE STANDARD TABLE OF zstp_xk02_hdr, ls_xk02_contacts TYPE zstp_xk02_contacts, i_contacts TYPE STANDARD TABLE OF zstp_xk02_contacts. START-OF-SELECTION. * Get the LSMW project information IMPORT /sapdmc/lsmemory FROM MEMORY ID '/SAPDMC/LSMW'. * Get the lsmw file which is written in Convert Data Step CALL FUNCTION '/SAPDMC/LSM_FILE_INFO_GET' EXPORTING project = /sapdmc/lsmemory-project subproj = /sapdmc/lsmemory-subproj object = /sapdmc/lsmemory-object IMPORTING file_conv = lv_filename EXCEPTIONS no_such_object = 1 OTHERS = 2. END-OF-SELECTION. DATA : l_rec_infile TYPE ty_infile. OPEN DATASET lv_filename FOR INPUT IN TEXT MODE ENCODING DEFAULT. IF sy-subrc <> 0. * MESSAGE e000(38)." Data upload failed. ENDIF. DO. READ DATASET lv_filename INTO l_rec_infile-text. IF sy-subrc <> 0. EXIT. ENDIF. * Check the record identifier of each new record CASE l_rec_infile(1). * Specification header data WHEN '1'. MOVE l_rec_infile-text TO ls_xk02_hdr. APPEND ls_xk02_hdr TO i_hdr. CLEAR ls_xk02_hdr. * Contact Information WHEN '2'. MOVE l_rec_infile-text TO ls_xk02_contacts. APPEND ls_xk02_contacts TO i_contacts. CLEAR ls_xk02_contacts. ENDCASE. ENDDO. CLOSE DATASET lv_filename. PERFORM open_group. LOOP AT i_hdr ASSIGNING FIELD-SYMBOL(). LOOP AT i_contacts ASSIGNING FIELD-SYMBOL() WHERE rec_link = -rec_link. PERFORM bdc_dynpro USING 'SAPMF02K' '0101'. PERFORM bdc_field USING 'BDC_CURSOR' 'WRF02K-D0380'. PERFORM bdc_field USING 'BDC_OKCODE' '/00'. PERFORM bdc_field USING 'RF02K-LIFNR' -lifnr. PERFORM bdc_field USING 'RF02K-D0110' 'X'. PERFORM bdc_field USING 'WRF02K-D0380' 'X'. PERFORM bdc_dynpro USING 'SAPMF02K' '0110'. PERFORM bdc_field USING 'BDC_OKCODE' '/00'. PERFORM bdc_field USING 'LFA1-NAME1' -name. PERFORM bdc_dynpro USING 'SAPMF02K' '0380'. PERFORM bdc_field USING 'BDC_CURSOR' 'KNVK-NAME1(02)'. PERFORM bdc_field USING 'BDC_OKCODE' '=UPDA'. PERFORM bdc_field USING 'KNVK-NAMEV(01)' -first_name. PERFORM bdc_field USING 'KNVK-NAMEV(02)' -last_name. PERFORM bdc_field USING 'KNVK-NAME1(01)' -first_name. PERFORM bdc_field USING 'KNVK-NAME1(02)' -last_name. PERFORM bdc_transaction USING 'XK02'. ENDLOOP. ENDLOOP. PERFORM close_group.
You can now see that the description of Vendor BLUE is updated to BLUE_UPDATED and translated to upper case.
Transaction - XK03
Extra notes -
Periodic Data Transfer- If the object attribute is periodic, it can be run in a periodic schedule. The underlying R/3 program is /SAPDMC/SAP_LSMW_INTERFACE. Only point to note here is that the input legacy file must reside in the Application Server.