Friday 31 March 2023

Wrapper program for BD10 and BD12

If you have a scnerio that you want to call BD10, so that you could create idocs for only those materials which are changed alongwith full details and same for customer master(BD12). Then below code can help you fasten the build process.

1. A class is created with 2 methods, first is to read the change pointers and the other method is to update the BDCP2 Process indicator, so that same records are not processed again with BD10 or BD12.
2. A custom program is created to call these 2 methods with input field as message type.

You can later on alter this program according to your needs.

1. Below is the code for class:

class ZCL_O2C_I_007_CHANGE_POINTER definition
  public
  final
  create public .

public section.

  types:
    begin of ty_material_range,
        sign   type ddsign,
        option type ddoption,
        low    type char18,
        high   type char18,
      end of ty_material_range .
  types:
    tt_range type table of ty_material_range with default key .

  class-methods READ_CHANGE_POINTERS
    importing
      !I_MESTYP type EDI_MESTYP
    returning
      value(R_VALUE) type TT_RANGE .
  class-methods CHANGE_POINTER_STATUS
    importing
      !I_MESTYP type EDI_MESTYP
      !I_KEYS type BDIMARAKEY_TAB optional
      !I_KUNNR_KEYS type ZO2C_I_007_KUNNRKEY optional .
protected section.
private section.
ENDCLASS.

SAP ABAP Central, SAP ABAP Guides, SAP ABAP Tutorial and Materials, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP, SAP ABAP Materials

CLASS ZCL_O2C_I_007_CHANGE_POINTER IMPLEMENTATION.

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_O2C_I_007_CHANGE_POINTER=>CHANGE_POINTER_STATUS
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_MESTYP                       TYPE        EDI_MESTYP
* | [--->] I_KEYS                         TYPE        BDIMARAKEY_TAB(optional)
* | [--->] I_KUNNR_KEYS                   TYPE        ZO2C_I_007_KUNNRKEY(optional)
*  ZO2C_I_007_KUNNRKEY This is custom table type for structure BDIKNA1KEY
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD change_pointer_status.
    CONSTANTS:
            lc_matmas type char6 VALUE 'MATMAS',
            lc_kunnr type char6 VALUE 'DEBMAS'

            .
    DATA: change_pointers    TYPE TABLE OF bdcp WITH DEFAULT KEY,
          change_pointer_ids TYPE TABLE OF bdicpident WITH DEFAULT KEY
          .

    CHECK i_mestyp IS NOT INITIAL.

    CALL FUNCTION 'CHANGE_POINTERS_READ'
      EXPORTING
        message_type    = i_mestyp
      TABLES
        change_pointers = change_pointers.
    IF i_mestyp = lc_matmas.
      LOOP AT i_keys REFERENCE INTO DATA(key).
        LOOP AT change_pointers REFERENCE INTO DATA(cp)
             WHERE cdobjid = key->matnr.
          change_pointer_ids = VALUE #( BASE change_pointer_ids ( cpident = cp->cpident ) ).
        ENDLOOP.
      ENDLOOP.
    ELSEIF i_mestyp = lc_kunnr.
      LOOP AT I_KUNNR_KEYS REFERENCE INTO DATA(key2).
        LOOP AT change_pointers REFERENCE INTO DATA(cp2)
             WHERE cdobjid = key2->kunnr.
          change_pointer_ids = VALUE #( BASE change_pointer_ids ( cpident = cp2->cpident ) ).
        ENDLOOP.
      ENDLOOP.
    ENDIF.

    CALL FUNCTION 'CHANGE_POINTERS_STATUS_WRITE'
      EXPORTING
        message_type           = i_mestyp
      TABLES
        change_pointers_idents = change_pointer_ids.

  ENDMETHOD.

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_O2C_I_007_CHANGE_POINTER=>READ_CHANGE_POINTERS
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_MESTYP                       TYPE        EDI_MESTYP
* | [<-()] R_VALUE                        TYPE        TT_RANGE
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD read_change_pointers.
    DATA change_pointers TYPE TABLE OF bdcp WITH DEFAULT KEY.
    CALL FUNCTION 'CHANGE_POINTERS_READ'
      EXPORTING
        message_type    = i_mestyp
      TABLES
        change_pointers = change_pointers.

    IF lines( change_pointers ) IS NOT INITIAL.
      r_value = VALUE #( FOR cp IN change_pointers
                   ( sign = 'I' option = 'EQ' low = cp-cdobjid ) ).
      DELETE ADJACENT DUPLICATES FROM r_value.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

2) Code for custom program(All the messages are hardcoded, so you need to convert them to text symbols)

REPORT zo2c_i_007_change_pointer_updt.

INCLUDE zo2c_i_007_cp_updt_gd. "Global declrations

INCLUDE zo2c_i_007_cp_updt_sel. "Selection screens

START-OF-SELECTION.
  "Read change pointers
  CALL METHOD zcl_o2c_i_007_change_pointer=>read_change_pointers
    EXPORTING
      i_mestyp = p_msg
    RECEIVING
      r_value  = lt_range.
  "
  IF lt_range IS INITIAL.
    MESSAGE 'No change pointer found !' TYPE 'E'.
  ELSEIF p_msg EQ 'MATMAS' AND lt_range IS NOT INITIAL.
    "Call BD10
    SUBMIT rbdsemat
                    WITH matsel IN lt_range
                    WITH sendall = abap_true
                    EXPORTING LIST TO MEMORY
                    AND RETURN.
    "Build keys table
    LOOP AT lt_range ASSIGNING FIELD-SYMBOL(<fs_matnr>).
      ls_mat_keys-matnr = <fs_matnr>-low.
      APPEND ls_mat_keys TO lt_mat_keys.
      CLEAR ls_mat_keys.
    ENDLOOP.
    "Update the status in BDCP2
    CALL METHOD zcl_o2c_i_007_change_pointer=>change_pointer_status
      EXPORTING
        i_mestyp = p_msg
        i_keys   = lt_mat_keys.
  ELSEIF p_msg EQ 'DEBMAS' AND lt_range IS NOT INITIAL.
*    call BD12
    SUBMIT rbdsedeb WITH selkunnr IN lt_range
                    WITH mestyp = 'DEBMAS'
                    EXPORTING LIST TO MEMORY
                    AND RETURN.
    "Build keys table
    LOOP AT lt_range ASSIGNING FIELD-SYMBOL(<fs_kunnr>).
      ls_kun_keys-kunnr = <fs_kunnr>-low.
      APPEND ls_kun_keys TO lt_kun_keys.
      CLEAR ls_kun_keys.
    ENDLOOP.
    "Update the status in BDCP2
    CALL METHOD zcl_o2c_i_007_change_pointer=>change_pointer_status
      EXPORTING
        i_mestyp = p_msg
        I_KUNNR_KEYS   = lt_kun_keys.
  ELSEIF lt_range IS NOT INITIAL.
    MESSAGE 'Message type not supported !' TYPE 'E'.
  ENDIF.
 
3) All the includes code:

i) Include ZO2C_I_007_CP_UPDT_GD

*&---------------------------------------------------------------------*
*& Include          ZO2C_I_007_CP_UPDT_GD
*&---------------------------------------------------------------------*

  TYPES:
    BEGIN OF ty_material_range,
      sign   TYPE ddsign,
      option TYPE ddoption,
      low    TYPE char18,
      high   TYPE char18,
    END OF ty_material_range .
*  TYPES:
*    tt_range TYPE TABLE OF ty_material_range WITH DEFAULT KEY .
  DATA: lt_range    TYPE STANDARD TABLE OF ty_material_range WITH DEFAULT KEY,
        lt_mat_keys TYPE bdimarakey_tab,
        ls_mat_keys TYPE bdimarakey,
        lt_kun_keys TYPE STANDARD TABLE OF bdikna1key,
        ls_kun_keys TYPE bdikna1key
        .
ii) ZO2C_I_007_CP_UPDT_SEL

SELECTION-SCREEN BEGIN OF BLOCK b1.

  PARAMETERS : P_MSG TYPE EDI_MESTYP OBLIGATORY.

SELECTION-SCREEN : END OF BLOCK b1.
 
The above code is for BD10 and BD12 and if you don’t need any one of them, you can remove the code.

No comments:

Post a Comment