（初）第六弾　ABAPー訓練「汎用等」

:writing_hand_tone1:[第五弾](https://qiita.com/luyu1567/items/11abae036e5f3cb70d4b)
:writing_hand_tone1:[SAP 訓練目次](https://qiita.com/items/dd1fdf83a42f90a78c6e)
#1.概要(例：品目登録)

- 汎用モジュール定義:Z_BATCH_
![image.png](https://qiita-image-store.s3.amazonaws.com/0/185058/3e60d660-2fe2-77e3-d7f8-3f87cca2a525.png)

- 汎用グループ:ZMM
![1.PNG](https://qiita-image-store.s3.amazonaws.com/0/185058/541b2427-a7db-997e-d200-901ce22d0756.png)

- RFC機能？
![image.png](https://qiita-image-store.s3.amazonaws.com/0/185058/ed7af418-5fc3-ea07-561e-8875c7444ea2.png)
- バリアント変数を付けば...
- 実装方法(BatchInput/BAPI)

#2. バリアント変数
Tr-CD:STVARVC/STVARV
項目定義：Z_MARA_ADD
#2.1 自動更新バリアント
```ruby:ABAP

  DATA:
    GDF_STR TYPE CHAR14.

  CONSTANTS:
    CNS_VARVC_NM TYPE CHAR15 VALUE 'Z_MARA_ADD01'.

  MOVE I_ADD_CNT TO GDF_STR.

  CONDENSE GDF_STR.

  UPDATE TVARVC SET LOW = GDF_STR
                    HIGH = SPACE WHERE NAME = CNS_VARVC_NM.

```
#2.2応用パターン
![image.png](https://qiita-image-store.s3.amazonaws.com/0/185058/f0dea97a-6ca3-1cbe-d624-3b14d3b3648f.png)

```ruby:ABAP
REPORT  Z_BATCH_ADD01.

DATA:
  GD_CNT_FOR_SCR   TYPE INT4.

SELECT-OPTIONS:

  S_CNT        FOR GD_CNT_FOR_SCR OBLIGATORY NO-EXTENSION NO INTERVALS .
```
#3. [BatchInput](https://www.sapnuts.com/courses/core-abap/bdc/recording-a-transaction-S.html)

#3.1 Tr-CD SHDB
![image.png](https://qiita-image-store.s3.amazonaws.com/0/185058/6efb1599-d7cb-2eb8-a6ac-4c004b0c693b.png)

#3.2 newRecorder
![image.png](https://qiita-image-store.s3.amazonaws.com/0/185058/11b6fc81-39e1-0210-5e52-e7c3940bed12.png)

#3.3 startRecorder

- record:recorder20190110
- Transation code:mm01

#3.4 入力画面

- 品目:
- 産業コード：
- 品目タイプ：

#3.5 基本データ１画面
![image.png](https://qiita-image-store.s3.amazonaws.com/0/185058/c103ecc2-34d3-b525-4cf0-412bcb113837.png)

#3.6 保存&BDC画面
![image.png](https://qiita-image-store.s3.amazonaws.com/0/185058/5c7a2f0a-bcac-4ebb-d349-a0f5bcf47e2d.png)
![image.png](https://qiita-image-store.s3.amazonaws.com/0/185058/f0cfa2e4-7bf1-47b4-7a9b-7d30b3dd59a3.png)
![image.png](https://qiita-image-store.s3.amazonaws.com/0/185058/c8c7a86f-4311-9255-fd90-01146d7a18ac.png)

![image.png](https://qiita-image-store.s3.amazonaws.com/0/185058/21954cb1-4cc1-ef9d-d1ea-aa9256f9733a.png)

![image.png](https://qiita-image-store.s3.amazonaws.com/0/185058/dfd8fa9b-4fe0-3fa3-82cd-0edfbfc3ff4a.png)

```ruby:ABAP
report ZSAPN_MM01
       no standard page heading line-size 255.

include bdcrecx1.

parameters: dataset(132) lower case.
***    DO NOT CHANGE - the generated data section - DO NOT CHANGE    ***
*
*   If it is nessesary to change the data section use the rules:
*   1.) Each definition of a field exists of two lines
*   2.) The first line shows exactly the comment
*       '* data element: ' followed with the data element
*       which describes the field.
*       If you don't have a data element use the
*       comment without a data element name
*   3.) The second line shows the fieldname of the
*       structure, the fieldname must consist of
*       a fieldname and optional the character '_' and
*       three numbers and the field length in brackets
*   4.) Each field must be type C.
*
*** Generated data section with specific formatting - DO NOT CHANGE  ***
data: begin of record,
* data element: MATNR
        MATNR_001(018),
* data element: MBRSH
        MBRSH_002(001),
* data element: MTART
        MTART_003(004),
* data element: XFELD
        KZSEL_01_004(001),
* data element: MAKTX
        MAKTX_005(040),
* data element: MEINS
        MEINS_006(003),
* data element: MTPOS_MARA
        MTPOS_MARA_007(004),
      end of record.

*** End generated data section ***

start-of-selection.

perform open_dataset using dataset.
perform open_group.

do.

read dataset dataset into record.
if sy-subrc <> 0. exit. endif.

perform bdc_dynpro      using 'SAPLMGMM' '0060'.
perform bdc_field       using 'BDC_CURSOR'
                              'RMMG1-MTART'.
perform bdc_field       using 'BDC_OKCODE'
                              '=ENTR'.
perform bdc_field       using 'RMMG1-MATNR'
                              record-MATNR_001.
perform bdc_field       using 'RMMG1-MBRSH'
                              record-MBRSH_002.
perform bdc_field       using 'RMMG1-MTART'
                              record-MTART_003.
perform bdc_dynpro      using 'SAPLMGMM' '0070'.
perform bdc_field       using 'BDC_CURSOR'
                              'MSICHTAUSW-DYTXT(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'MSICHTAUSW-KZSEL(01)'
                              record-KZSEL_01_004.
perform bdc_dynpro      using 'SAPLMGMM' '0070'.
perform bdc_field       using 'BDC_CURSOR'
                              'MSICHTAUSW-DYTXT(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_dynpro      using 'SAPLMGMM' '0070'.
perform bdc_field       using 'BDC_CURSOR'
                              'MSICHTAUSW-DYTXT(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              '=ENTR'.
perform bdc_dynpro      using 'SAPLMGMM' '4004'.
perform bdc_field       using 'BDC_OKCODE'
                              '=BU'.
perform bdc_field       using 'MAKT-MAKTX'
                              record-MAKTX_005.
perform bdc_field       using 'BDC_CURSOR'
                              'MARA-MEINS'.
perform bdc_field       using 'MARA-MEINS'
                              record-MEINS_006.
perform bdc_field       using 'MARA-MTPOS_MARA'
                              record-MTPOS_MARA_007.
perform bdc_transaction using 'MM01'.

enddo.

perform close_group.
perform close_dataset using dataset.
```

#4. [BAPI【品目登録】](https://www.sapnuts.com/courses/SAP-Cross-Applications/bapi-s-in-sap/using-bapi-material.html)

```ruby:ABAP
REPORT ZSAPN_MATERIAL_BAPI.
*BAPI declarations
DATA : WA_HEADDATA TYPE BAPIMATHEAD,
       WA_CLIENTDATA TYPE BAPI_MARA,
       WA_CLIENTDATAX TYPE BAPI_MARAX.
DATA : IT_MATERIALDESCRIPTION TYPE TABLE OF BAPI_MAKT,
       WA_MATERIALDESCRIPTION TYPE BAPI_MAKT.
DATA : IT_RETURN TYPE TABLE OF BAPIRET2,
       WA_RETURN TYPE BAPIRET2.
TYPES: BEGIN OF TY_MARA, "internal table as per flat file structure
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MBRSH TYPE MARA-MBRSH,
        MATKL TYPE MARA-MATKL,
        MEINS TYPE MARA-MEINS,
        MAKTX TYPE MAKT-MAKTX,
      END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
      WA_MARA TYPE TY_MARA.

**selection screen
PARAMETERS: P_FILE TYPE RLGRAP-FILENAME. "file input

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE. "f4 helop for file input
  PERFORM FILE_HELP.

AT SELECTION-SCREEN ON HELP-REQUEST FOR P_FILE. "F1 help for file input
  MESSAGE 'Please press f4 to select file' TYPE 'I'.

START-OF-SELECTION.
  DATA : LV_FILE TYPE STRING.
  LV_FILE = P_FILE. "to avoid type conflict
  CALL FUNCTION 'GUI_UPLOAD' "upload flat file
    EXPORTING
      FILENAME            = LV_FILE
      FILETYPE            = 'ASC'
      HAS_FIELD_SEPARATOR = 'X'
*     HEADER_LENGTH       = 0
*     READ_BY_LINE        = 'X'
*     DAT_MODE            = ' '
*     CODEPAGE            = ' '
*     IGNORE_CERR         = ABAP_TRUE
*     REPLACEMENT         = '#'
*     CHECK_BOM           = ' '
*     VIRUS_SCAN_PROFILE  =
*     NO_AUTH_CHECK       = ' '
* IMPORTING
*     FILELENGTH          =
*     HEADER              =
    TABLES
      DATA_TAB            = IT_MARA "internal table
* CHANGING
*     ISSCANPERFORMED     = ' '
* EXCEPTIONS
*     FILE_OPEN_ERROR     = 1
*     FILE_READ_ERROR     = 2
*     NO_BATCH            = 3
*     GUI_REFUSE_FILETRANSFER       = 4
*     INVALID_TYPE        = 5
*     NO_AUTHORITY        = 6
*     UNKNOWN_ERROR       = 7
*     BAD_DATA_FORMAT     = 8
*     HEADER_NOT_ALLOWED  = 9
*     SEPARATOR_NOT_ALLOWED         = 10
*     HEADER_TOO_LONG     = 11
*     UNKNOWN_DP_ERROR    = 12
*     ACCESS_DENIED       = 13
*     DP_OUT_OF_MEMORY    = 14
*     DISK_FULL           = 15
*     DP_TIMEOUT          = 16
*     OTHERS              = 17
    .
IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.
  LOOP AT IT_MARA INTO WA_MARA.
    WA_HEADDATA-MATERIAL = WA_MARA-MATNR. "material no
    WA_HEADDATA-IND_SECTOR = WA_MARA-MBRSH. "industry sector
    WA_HEADDATA-MATL_TYPE = WA_MARA-MTART. "material type
    WA_HEADDATA-BASIC_VIEW = 'X'. "basic view
    WA_CLIENTDATA-MATL_GROUP = WA_MARA-MATKL. "material group
    WA_CLIENTDATA-BASE_UOM = WA_MARA-MEINS. "base Unit Of measure
    WA_CLIENTDATAX-MATL_GROUP = 'X'. "passing material group
    WA_CLIENTDATAX-BASE_UOM = 'X'. "passing base unit of measure

    WA_MATERIALDESCRIPTION-LANGU = SY-LANGU. "english language
    WA_MATERIALDESCRIPTION-LANGU_ISO = 'JA'. "ISO language
    WA_MATERIALDESCRIPTION-MATL_DESC = WA_MARA-MAKTX. "material descriptions
    APPEND WA_MATERIALDESCRIPTION TO IT_MATERIALDESCRIPTION.
    CLEAR WA_MATERIALDESCRIPTION.
**Create/update materials using BAPI_MATERIAL_SAVEDATA
    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
      EXPORTING
        HEADDATA            = WA_HEADDATA
        CLIENTDATA          = WA_CLIENTDATA
        CLIENTDATAX         = WA_CLIENTDATAX
*       PLANTDATA           = WA_PLANTDATA
*       PLANTDATAX          = WA_PLANTDATAX
*       FORECASTPARAMETERS  =
*       FORECASTPARAMETERSX =
*       PLANNINGDATA        =
*       PLANNINGDATAX       =
*       STORAGELOCATIONDATA = WA_STORAGELOCATIONDATA
*       STORAGELOCATIONDATAX = WA_STORAGELOCATIONDATAX
*       VALUATIONDATA       =
*       VALUATIONDATAX      =
*       WAREHOUSENUMBERDATA =
*       WAREHOUSENUMBERDATAX =
*       SALESDATA           = WA_SALESDATA
*       SALESDATAX          = WA_SALESDATAX
*       STORAGETYPEDATA     =
*       STORAGETYPEDATAX    =
*       FLAG_ONLINE         = ' '
*       FLAG_CAD_CALL       = ' '
*       NO_DEQUEUE          = ' '
*       NO_ROLLBACK_WORK    = ' '
      IMPORTING
        RETURN              = WA_RETURN
      TABLES
        MATERIALDESCRIPTION = IT_MATERIALDESCRIPTION
*       UNITSOFMEASURE      = IT_UNITSOFMEASURE
*       UNITSOFMEASUREX     = IT_UNITSOFMEASUREX
*       INTERNATIONALARTNOS =
*       MATERIALLONGTEXT    = IT_MATERIALLONGTEXT
*       TAXCLASSIFICATIONS  = IT_TAXCLASSIFICATIONS
*       RETURNMESSAGES      = IT_RETURN
*       PRTDATA             =
*       PRTDATAX            =
*       EXTENSIONIN         =
*       EXTENSIONINX        =
      .
IF WA_RETURN-TYPE = 'E'.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      WRITE:/ WA_HEADDATA-MATERIAL, 'is not created' COLOR 3.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          WAIT = 'X'.
      IF SY-SUBRC EQ 0.
        WRITE:/ WA_HEADDATA-MATERIAL, 'is created' COLOR 5.
      ENDIF.
    ENDIF.
  ENDLOOP.
FORM FILE_HELP .
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4' "get file name on F4 help
* EXPORTING
*   PROGRAM_NAME        = SYST-REPID
*   DYNPRO_NUMBER       = SYST-DYNNR
*   FIELD_NAME          = ' '
*   STATIC              = ' '
*   MASK                = ' '
*   FILEOPERATION       = 'R'
*   PATH                =
    CHANGING
      FILE_NAME = P_FILE
*     LOCATION_FLAG       = 'P'
* EXCEPTIONS
*     MASK_TOO_LONG       = 1
*     OTHERS    = 2
    .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.
ENDFORM.                    " FILE_HELP
```
```ruby:mara.txt
SAPNMAT001	MODE	W	016	EA	Material description1
SAPNMAT002	MODE	W	016	EA	Material description2
SAPNMAT003	MODE	W	016	EA	Material description3
SAPNMAT004	MODE	W	016	EA	 Material description4

```

:writing_hand_tone1:[第七弾](https://qiita.com/luyu1567/items/d724ca99a263d0ae2442)
:writing_hand_tone1:[SAP 訓練目次](https://qiita.com/items/dd1fdf83a42f90a78c6e)
[参考英語リンク](https://www.sapnuts.com/)