Friday 8 May 2020

Migrating user object using Your own Functional Module in S/4HANA Migration Cockpit

SAP ABAP Tutorial and Materials, SAP ABAP Certifications, SAP ABAP Guides, SAP HANA Learning, SAP ABAP Exam Prep

In order to migrate user defined S/4HANA object new functional module should be created. In this blog post you will learn how to do it and use your own API in Migration Cockpit object.

SAP Note 2590165 – SAP S/4HANA Migration Cockpit – Creating Your own Function Modules could be good starting point in this journey. Main requirements for module are as follow:

◉ The function module must not execute the COMMIT WORK command.
◉ ABAP Dictionary structure BAPIRET2 must be used as a structure to return messages
◉ Test run flag should be implemented

As example we will use database table ZSD_HISTCOND Price History

Field Key  Data element   Data Type  Length  Decimals  Short Description 
MANDT X MANDT CLNT 3 0 Client
MATNR  MATNR  CHAR  40 Material Number 
DATBI  KODATBI  DATS  Validity end date of the condition record
DATAB  KODATAB   DATS  Validity start date of the condition record 
KBETR  KBETR_KOND  CURR  11  Condition amount or percentage where no scale exists 

Functional Module will be an interface to Migration Cockpit. Business logic, input data validation, warning and error message collecting is implemented in Class.

1. Create Table Type ZSDHISTZPR0_TT


Run SE11 t-code. Chose Data type option and Table type.  Set Line Type ZSD_HIST as your database table. Save and activate.

SAP ABAP Tutorial and Materials, SAP ABAP Certifications, SAP ABAP Guides, SAP HANA Learning, SAP ABAP Exam Prep

2. Create Class


Run SE80 t-code.

Create Class ZCL_SD_HISTZPR_GEN

Description   Creation Price History

Instance Generation Public

Define methods

Method Level  Visibility  Description 
CREATE  Static Method Public Create History of pricing records
SET_RETURN_MESSAGE   Static Method   Private  Add return message 

Define Parameters

Parameter Type  Typing Method   Associated Type 
CREATE
IT_ZSDHISTCOND Importing Type ZSD_HISTCOND_TT
ET_MESSAGES  Exporting  Type   BAPIRET2_T 
SET_RETURN_MESSAGE
IV_TYPE Importing Type BAPI_MTYPE
IV_ID  Importing  Type  SYMSGID 
IV_NUMBER  Importing  Type  SYMSGNO 
IV_MESSAGE_V1   Importing  Type  SYMSGV 
IV_MESSAGE_V2  Importing  Type  SYMSGV 
IV_MESSAGE_V3  Importing  Type  SYMSGV 
IV_MESSAGE_V4 Importing  Type  SYMSGV 
ET_MESSAGES   Importing  Type  BAPIRET2_T 

Define Text Symbols

Sym Text
001  Populate mandatory data
002  Record was created 

Set source code

METHOD create.
    DATA: ls_hist   TYPE zsd_histcond,
          lt_hist       TYPE TABLE OF zsd_histcond,
          ls_messages   TYPE bapiret2.
    LOOP AT it_zsdhistcond ASSIGNING FIELD-SYMBOL(<ls_zsdhistcond>).
      IF <ls_zsdhistcond>-matnr IS INITIAL OR
         <ls_zsdhistcond>-datbi IS INITIAL OR
         <ls_zsdhistcond>-datab IS INITIAL.

        set_return_message(
          EXPORTING
             iv_type = 'E'
             iv_id = 'SD024'
             iv_number = 000
             iv_message_v1 = TEXT-001
             iv_message_v2 = space
             iv_message_v3 = space
             iv_message_v4 = space
           IMPORTING
             et_messages = et_messages ).
        RETURN.
      ENDIF.

      ls_hist-matnr = <ls_zsdhistcond>-matnr.
      ls_hist-datbi = <ls_zsdhistcond>-datbi.
      ls_hist-datab = <ls_zsdhistcond>-datab.
      ls_hist-kbetr = <ls_zsdhistcond>-kbetr.
      APPEND ls_hist TO lt_hist.
    ENDLOOP.

    MODIFY zsd_histcond FROM TABLE lt_hist.

            set_return_message(
          EXPORTING
             iv_type = 'I'
             iv_id = 'SD024'
             iv_number = 000
             iv_message_v1 = TEXT-002
             iv_message_v2 = space
             iv_message_v3 = space
             iv_message_v4 = space
           IMPORTING
             et_messages = et_messages ).
  ENDMETHOD.

  METHOD SET_RETURN_MESSAGE.
  DATA: ls_return TYPE bapiret2.
    ls_return-type = iv_type.
    ls_return-id = iv_id.
    ls_return-number = iv_number.
    ls_return-message_v1 = iv_message_v1.
    ls_return-message_v1 = iv_message_v1.
    ls_return-message_v1 = iv_message_v1.
    ls_return-message_v1 = iv_message_v1.
    APPEND ls_return TO et_messages.
  ENDMETHOD.

SAP ABAP Tutorial and Materials, SAP ABAP Certifications, SAP ABAP Guides, SAP HANA Learning, SAP ABAP Exam Prep

3. Create Functional Module


Create Function Group ZSD_0001.

Create Function Module Z_SD_HISTCOND_CREATE.

Set export\import parameters

Import 
Parameter Name Typing  Associated Type   Short Text 
IV_TEST_RUN TYPE BAPIE1GLOBAL_DATA-TESTRUN Test run
IT_ZSDHISTCOND   TYPE  ZSD_HISTCOND_TT   Price History
Export  
ET_MESSAGES TYPE BAPIRET2_T   Return messages

Populate source code

FUNCTION z_sd_histcond_create.
*"----------------------------------------------------------------------
*"*"Локальный интерфейс:
*"  IMPORTING
*"     REFERENCE(IV_TEST_RUN) TYPE  BAPIE1GLOBAL_DATA-TESTRUN
*"     REFERENCE(IT_ZSDHISTCOND) TYPE  ZSD_HISTCOND_TT
*"  EXPORTING
*"     REFERENCE(ET_MESSAGES) TYPE  BAPIRET2_T
*"----------------------------------------------------------------------
  zcl_sd_histcond_gen=>create(
    EXPORTING
      it_zsdhistcond = it_zsdhistcond
    IMPORTING
      et_messages = et_messages ).

  IF iv_test_run = 'X'.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ENDIF.
ENDFUNCTION.


4. Create Migration Object


Run LTMC t-code. Create new Project

Name – User defined object Price History

Transfer Option – Transfer Data from File

Mass Transfer ID Z24

Run t-code LTMOM. Open created Project Mass Transfer ID Z24. Chose menu:

Project – Create Migration Object – User-Defined – Migrate Data Using Files\Staging Tables as shown on pictures below.

SAP ABAP Tutorial and Materials, SAP ABAP Certifications, SAP ABAP Guides, SAP HANA Learning, SAP ABAP Exam Prep

SAP ABAP Tutorial and Materials, SAP ABAP Certifications, SAP ABAP Guides, SAP HANA Learning, SAP ABAP Exam Prep

4.1 Define Source structure

Create source structure S_HISTCOND Price History

Key Name  Data Type   Length  Decimal Places   Column Header 
X MATNR CHAR 40 Material
DATBI  DATS 8 Valid To Date 
DATAB  DATS  Valid-From Date
KBETR  CURR  11  2 Price

SAP ABAP Tutorial and Materials, SAP ABAP Certifications, SAP ABAP Guides, SAP HANA Learning, SAP ABAP Exam Prep

4.2 Adjust structure mapping

Source structure Target Structure   Access Type   Mapping Type   Source Cardinality   Target Cardinality   Data Type Path
S_HISTCOND HEADER LOOP AT TABLE 1:1 Relation 1 Default Path
S_HISTCOND  R_IT_ZSDHISTCOND Mapping only 1:1 Relation  Not Applicable

SAP ABAP Tutorial and Materials, SAP ABAP Certifications, SAP ABAP Guides, SAP HANA Learning, SAP ABAP Exam Prep

4.3 Set up fields mapping

Parameter Parameter Value Rule  Target Field 
Target Structure R_IT_ZSDHISTCOND
SENDERFIELD   S_HISTCOND-MATNR MOVE MATNR
SENDERFIELD   S_HISTCOND-DATBI   MOVE  DATBI 
SENDERFIELD   S_HISTCOND-DATAB   MOVE  DATAB
SENDERFIELD   S_HISTCOND-KBETR   MOVE  KBETR 

SAP ABAP Tutorial and Materials, SAP ABAP Certifications, SAP ABAP Guides, SAP HANA Learning, SAP ABAP Exam Prep

Save Object (Ctrl+S) and generate run time object.

5. Migrate data


Run LTMC t-code. Chose your Project then Object. Download Template. Populate data as follow.

SAP ABAP Tutorial and Materials, SAP ABAP Certifications, SAP ABAP Guides, SAP HANA Learning, SAP ABAP Exam Prep

Go through migration steps. Data is uploaded into the table.

SAP ABAP Tutorial and Materials, SAP ABAP Certifications, SAP ABAP Guides, SAP HANA Learning, SAP ABAP Exam Prep

Thereby we have created our own Functional Module and corresponding Migration Cockpit object. Now we can migrate custom object data.

No comments:

Post a Comment