Friday 7 April 2023

Some useful exits

This blog is to help those who are looking for some exits for interfaces. We are trying to collate number of exits which can help in interfaces.

1. MATMAS – Outbound idoc

i. EXIT_SAPLMV01_002 – This can help in Outbound idocs where we have to modify values as per requirement. example code below:

DATA: ls_E1MARAM  TYPE e1maram,
      ls_ZE1AUSP  TYPE ze1ausp,
      ls_ZE1MARAM TYPE ze1maram,
      ls_EDIDD    TYPE edidd,
      ls_ZE1MVKEM TYPE ze1mvkem,
      ls_E1MVKEM  TYPE e1mvkem
      .
CONSTANTS: lc_ZE1AUSP  TYPE char7  VALUE  'ZE1AUSP',
           lc_ZE1MARAM TYPE char8 VALUE 'ZE1MARAM',
           lc_ZMATEXT  TYPE char7  VALUE  'ZMATEXT',
           lc_ZE1MVKEM TYPE char8 VALUE 'ZE1MVKEM'
           .
idoc_cimtype = lc_ZMATEXT.
CASE segment_name.
  WHEN 'E1MARAM'.
    ls_E1MARAM = idoc_data-sdata.
    "Fetch data from AUSP and add to custom fields
    SELECT * INTO TABLE @DATA(lt_ausp) FROM ausp WHERE objek = @f_mara-matnr.
    IF sy-subrc IS INITIAL.
      idoc_cimtype = lc_ZMATEXT.
      LOOP AT lt_ausp ASSIGNING FIELD-SYMBOL(<fs_ausp>).
        ls_zE1AUSP-objek = <fs_ausp>-objek.
        CALL FUNCTION 'CONVERSION_EXIT_ATINN_OUTPUT'
          EXPORTING
            input  = <fs_ausp>-atinn
          IMPORTING
            output = ls_zE1AUSP-atinn.

*        ls_zE1AUSP-atinn = <fs_ausp>-atinn.
        ls_zE1AUSP-atwrt = <fs_ausp>-atwrt.
        ls_edidd-segnam = lc_ZE1AUSP.
        MOVE ls_zE1AUSP TO ls_edidd-sdata.
        "Append to final table
        APPEND ls_edidd TO idoc_data.
        CLEAR:ls_edidd.
      ENDLOOP.
    ENDIF
ENDCASE.
    ENDIF.
 
2) Good movement Interface(WMMBXY) : This  EXIT_SAPLLMDE_002 can help you with Goods movement inbound idoc.

3) Invoice idoc(EXIT_SAPLVEDF_002) : This EXIT_SAPLVEDF_002 can help you with INVOIC02 outbound idoc.

4) BADI ME_BAPI_PO_CUST – For PO inbound idocs. There are different methods for inbound and outbound . This can help you with changing values for PO. Sample code below for inbound idocs:

SAP ABAP, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation

Below code is changing few values and also ensuring if ref. PO field is available then new PO gets created with ref. PO:

    "Initialization
    CONSTANTS: lc_zbp1 TYPE char4 VALUE 'ZBP1'.
    DATA: lt_ret  TYPE STANDARD TABLE OF bapiret2,
          lt_con  TYPE STANDARD TABLE OF bapimepocond,
          ls_con  TYPE bapimepocond,
          ls_conx TYPE bapimepocondx
          .
    "Refersh all variables
    CLEAR: lt_ret, lt_con,
           ls_con, ls_conx
           .
    "If vendor is not initial
    IF ch_poheader-vendor IS NOT INITIAL.
      "Do the conversion
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
        EXPORTING
          input  = ch_poheader-vendor
        IMPORTING
          output = ch_poheader-vendor.

*    "Select from BUT000 based on ext. number and BU Group
      SELECT SINGLE partner FROM but000 INTO @DATA(lv_partn1) WHERE bpext = @ch_poheader-vendor
                                                           AND bu_group = @lc_zbp1
                                                        .
      IF sy-subrc IS INITIAL.
        "Change LIFNR
        ch_poheader-vendor = lv_partn1.
      ENDIF.
    ENDIF.
    "look for PO, if it exists!
    CALL FUNCTION 'BAPI_PO_GETDETAIL'
      EXPORTING
        purchaseorder = ch_poheader-po_number
      TABLES
        return        = lt_ret
*       pocond        = lt_con
      .
    IF lt_ret IS NOT INITIAL.
*      Create new PO case
      LOOP AT ch_itemcond ASSIGNING FIELD-SYMBOL(<fs_cond>).
        IF <fs_cond>-cond_type IS NOT INITIAL.
          IF <fs_cond>-cond_type EQ 'PBXX'.
            <fs_cond>-change_id = 'U'.
          ELSE.
            <fs_cond>-change_id = 'I'.
          ENDIF.
        ENDIF.
      ENDLOOP.
    ELSE.
      "Commenting below FM would take away dump
*      CALL FUNCTION 'BAPI_PO_GETDETAIL1'
*        EXPORTING
*          purchaseorder = ch_poheader-po_number
*        TABLES
*          return        = lt_ret
*          pocond        = lt_con.
      "Fetch conditon records !
      SELECT SINGLE ebeln,
              knumv INTO ( @DATA(lv_ebeln), @DATA(lv_knumv) )
                FROM ekko WHERE ebeln = @ch_poheader-po_number.
      IF sy-subrc IS INITIAL.
        SELECT * FROM prcd_elements INTO TABLE @DATA(lt_prcd)
          WHERE knumv = @lv_knumv
            AND kposn = '1'
          .
      ENDIF.
*      "Sort records
      SORT ch_itemcond BY cond_type.
      SORT lt_prcd BY kschl.
      SORT ch_itemcondx BY cond_type.
      "There are 3 scenerios:
*        i) Update existing entries from idoc
*       ii) Insert new entries from idoc
*      iii) Delete existing entries which does not exist in PO
      "Modification of existing PO case
      LOOP AT ch_itemcond ASSIGNING FIELD-SYMBOL(<fs_cond2>).
        IF <fs_cond2>-cond_type IS NOT INITIAL.
          READ TABLE ch_itemcondx ASSIGNING FIELD-SYMBOL(<fs_condx>)
                                      WITH KEY condition_no = <fs_cond2>-condition_no
                                               itm_number   = <fs_cond2>-itm_number
                                               cond_st_no   = <fs_cond2>-cond_st_no
                                               BINARY SEARCH
                                               .
*          <No need of SY-SUBRC>
          READ TABLE lt_prcd INTO DATA(ls_prcd)
                                    WITH KEY kschl = <fs_cond2>-cond_type
                                    BINARY SEARCH
                                    .
          IF sy-subrc IS INITIAL .
*            Modify scenerio
            <fs_cond2>-condition_no = ls_prcd-knumv. " Condition no.
            <fs_cond2>-itm_number = ls_prcd-kposn. " item number
            <fs_cond2>-cond_st_no = ls_prcd-stunr. " line no.
            <fs_cond2>-change_id  = 'U'.
            "Pass X
            <fs_condx>-condition_no = ls_prcd-knumv.
            <fs_condx>-itm_number = ls_prcd-kposn.
            <fs_condx>-cond_st_no = ls_prcd-stunr.
            "abap_true
            <fs_condx>-condition_nox = abap_true.
            <fs_condx>-itm_numberx  = abap_true.
            <fs_condx>-change_id    = abap_true.
            <fs_condx>-cond_st_nox  = abap_true.
          ELSE.
            READ TABLE lt_prcd INTO DATA(ls_prcd2) INDEX 1.
            "Insert scenerio
            <fs_cond2>-condition_no = ls_prcd2-knumv.
            <fs_cond2>-itm_number = <fs_cond2>-itm_number.
            <fs_cond2>-cond_st_no = <fs_cond2>-cond_st_no. " line no.
            <fs_cond2>-change_id = 'I'.
            "Pass X
            <fs_condx>-condition_no    = ls_prcd2-knumv.
            <fs_condx>-itm_number = <fs_cond2>-itm_number.
            <fs_condx>-cond_st_no = <fs_cond2>-cond_st_no.
            <fs_condx>-condition_nox = abap_true.
            <fs_condx>-itm_numberx  = abap_true.
            <fs_condx>-change_id  = abap_true.
            <fs_condx>-cond_st_nox  = abap_true.
          ENDIF.
        ENDIF.
      ENDLOOP.
      "Delete scenerio
      SORT lt_prcd BY kschl.
      SORT ch_itemcond BY cond_type.

      LOOP AT lt_prcd ASSIGNING FIELD-SYMBOL(<fs_del>).
        IF <fs_del>-kschl IS NOT INITIAL.
          READ TABLE ch_itemcond TRANSPORTING NO FIELDS
                                                    WITH KEY cond_type = <fs_del>-kschl
                                                      BINARY SEARCH.
          IF sy-subrc IS NOT INITIAL.
            "Delete scenerio
            ls_con-condition_no = <fs_del>-knumv.
            ls_con-cond_no    = <fs_del>-knumh.
            ls_con-itm_number = <fs_del>-kposn.
            ls_con-cond_st_no = <fs_del>-stunr.
            ls_con-cond_count = <fs_del>-zaehk."condition count
*            ls_con-cond
            ls_con-cond_type  = <fs_del>-kschl."condition type
*            ls_con-COND_VALUE = <fs_del>-KWERT."condition value
            ls_con-CURRENCY   = <fs_del>-WAERS.
*            ls_con-CURRENCY_ISO = <fs_del>-WAERS.
            ls_con-COND_UNIT  =   <fs_del>-kmein.
*            ls_con-COND_P_UNT = <fs_del>-KPEIN.
            ls_con-APPLICATIO = <fs_del>-kappl.
            ls_con-CONPRICDAT = <fs_del>-kdatu.
            ls_con-CALCTYPCON = <fs_del>-krech.
            ls_con-CONBASEVAL = <fs_del>-kawrt.
            ls_con-CONEXCHRAT = <fs_del>-kkurs.
            ls_con-NUMCONVERT = <fs_del>-KUMZA.
            ls_con-DENOMINATO = <fs_del>-KUMNE.
            ls_con-CONDTYPE   = <fs_del>-kntyp.
            ls_con-CONDORIGIN = <fs_del>-KHERK.
            ls_con-GROUPCOND  = <fs_del>-KGRPE.
*            ls_con-COND_UPDAT
            ls_con-ACCESS_SEQ = <fs_del>-KOLNR.
            ls_con-CONDCOUNT  = <fs_del>-ZAEHK.
            ls_con-CONDCLASS  = <fs_del>-KOAID.
*            ls_con-COND_UPDAT = 'X'.
            ls_con-change_id  = 'D'.
            APPEND ls_con TO ch_itemcond.
            SORT ch_itemcond BY cond_type.
            CLEAR : ls_con.
            "Pass X
            ls_conx-condition_no = <fs_del>-knumv.
            ls_conx-itm_number = <fs_del>-kposn.
            ls_conx-cond_st_no = <fs_del>-stunr.
            ls_conx-condition_nox = abap_true.
            ls_conx-cond_type     = abap_true.
            ls_conx-itm_numberx  = abap_true.
*            ls_conx-cond_value = abap_true.
            ls_conx-change_id  = abap_true.
            ls_conx-cond_st_nox  = abap_true.
            ls_conx-cond_count   = abap_true.
            ls_conx-condcntrl    = abap_true.
            ls_conx-condisacti   = abap_true.
            ls_conx-condclass   = abap_true.
            ls_conx-factbasval  = abap_true.
            ls_conx-scalebasin  = abap_true.
            ls_conx-scalbasval  = abap_true.
            ls_conx-unitmeasur  = abap_true.
            ls_conx-unitmeasur_iso  = abap_true.
            ls_conx-currenckey  = abap_true.
            ls_conx-currenckey_iso  = abap_true.
            ls_conx-condincomp  = abap_true.
            ls_conx-condconfig  = abap_true.
            ls_conx-condchaman  = abap_true.
            ls_conx-cond_no     = abap_true.
            ls_conx-change_id   = abap_true.
            ls_conx-vendor_no   = abap_true.
            APPEND ls_conx TO ch_itemcondx.
            CLEAR: ls_conx.
          ENDIF.
        ENDIF.
      ENDLOOP.
    ENDIF.

No comments:

Post a Comment