Pages

Wednesday, August 13, 2014

Easy way of passing values from custom program to Exits - without IMPORT/EXPORT or memory id

Often its required to transfer values from custom program to EXITS while calling any Object creation BAPi.
Using IMPORT/EXPORT to memory or INDX is not very reliable especially when the custom programs are ran in background mode.

One of the easy way is to :
- use SAVE_TEXT to update the value to the Biz object in the custom program
_ call the creation BAPI for Deivery, order, billing etc.
- during the creation process, find an exit where the VALUEs can be impported using READ_TEXT.
- after creation process , once control comes back to the custom program, delete the value from the object using DELETE_TEXT.

custom program

DATAlt_range TYPE TABLE OF zrange.
FIELD-SYMBOLS TYPE zrange,
                LIKE xvbpa.

DATAlv_name TYPE tdobname,
      lt_lines TYPE STANDARD TABLE OF tline,
      ls_lines LIKE LINE OF lt_lines,
      ls_lips  TYPE lips,
      lv_ebelp TYPE ebelp.

CONSTANTSco_savelang  TYPE thead-tdspras VALUE 'E'.

* check for availability of PO doc
IF ekko-ebeln IS INITIAL.
  RETURN.
ENDIF.

* Read ZRANGE for triggering of below logic
SELECT FROM zrange INTO TABLE lt_range
  WHERE application  'HUBSTOSA' AND
        program_name 'MV50AFZ1' AND
        identifier  ekko-ekorg.

IF sy-subrc EQ 0.
  SORT lt_range BY fieldname.
  "Check doc type
  READ TABLE lt_range ASSIGNING  WITH KEY fieldname 'LFART' BINARY SEARCH.
  IF sy-subrc NE AND  IS NOT ASSIGNED.
    RETURN.
  ELSEIF  IS ASSIGNED.
    IF -low NE likp-lfart.
      RETURN.
    ENDIF.
  ENDIF.
  READ TABLE lt_range ASSIGNING  WITH KEY fieldname 'BSART' BINARY SEARCH.
  IF sy-subrc NE AND  IS NOT ASSIGNED.
    RETURN.
  ELSEIF  IS ASSIGNED.
    IF -low NE ekko-bsart.
      RETURN.
    ENDIF.
  ENDIF.
ELSE.
  RETURN.
ENDIF.


CHECK sy-tcode EQ 'ZSD_SCHEDULE_HUB' OR sy-batch EQ 'X'.

READ TABLE xvbpa[] ASSIGNING  WITH KEY parvw 'ZB'."during change this Partner will be there
IF sy-subrc EQ 0.
  RETURN."it means this code has been called during DN creation time. We need thi code during DN change
ENDIF.

********* Read Text *********

SORT xlips[] BY vgpos."text ID will always be there for the 1st item of the delivery as per Report logic
READ TABLE xlips[] INTO ls_lips INDEX 1.
MOVE ls_lips-vgpos+1(5TO lv_ebelp.

IF sy-subrc EQ 0.

  MOVE ls_lips-vgbel TO lv_name.

  CALL FUNCTION 'READ_TEXT'
    EXPORTING
*     client                  = sy-mandt
      id                      'L04'
      language                co_savelang
      name                    lv_name
      object                  'EKPO'
    TABLES
      lines                   lt_lines
    EXCEPTIONS
      id                      1
      language                2
      name                    3
      not_found               4
      object                  5
      reference_check         6
      wrong_access_to_archive 7
      OTHERS                  8.
  IF sy-subrc <> 0.
    RETURN.
  ENDIF.

  READ TABLE lt_lines INTO ls_lines INDEX 1.
  IF sy-subrc EQ 0.
    "Loop has been used here, so that in case of data issues, when multiple
    "DNs are created, all DN should have the HUB Partner number
    LOOP AT  xvbpa[] ASSIGNING  WHERE parvw 'WE'.
      MOVE ls_lines-tdline TO -kunnr.
      SELECT adrnr UP TO ROWS INTO -adrnr
        FROM kna1 WHERE kunnr -kunnr.
      ENDSELECT.
    ENDLOOP.
  ENDIF.

ENDIF.


------- call BAPI for Outboud delivery creation
Include          MV50AFZ1
FORM USEREXIT_SAVE_DOCUMENT_PREPARE.

SORT xlips[] BY vgpos."text ID will always be there for the 1st item of the delivery as per Report logic
READ TABLE xlips[] INTO ls_lips INDEX 1.
MOVE ls_lips-vgpos+1(5TO lv_ebelp.

IF sy-subrc EQ 0.

  MOVE ls_lips-vgbel TO lv_name.

  CALL FUNCTION 'READ_TEXT'
    EXPORTING
*     client                  = sy-mandt
      id                      'L04'
      language                co_savelang
      name                    lv_name
      object                  'EKPO'
    TABLES
      lines                   lt_lines
    EXCEPTIONS
      id                      1
      language                2
      name                    3
      not_found               4
      object                  5
      reference_check         6
      wrong_access_to_archive 7
      OTHERS                  8.
  IF sy-subrc <> 0.
    RETURN.
  ENDIF.

  READ TABLE lt_lines INTO ls_lines INDEX 1.
  IF sy-subrc EQ 0.
    "Loop has been used here, so that in case of data issues, when multiple
    "DNs are created, all DN should have the HUB Partner number
    LOOP AT  xvbpa[] ASSIGNING  WHERE parvw 'WE'.
      MOVE ls_lines-tdline TO -kunnr.
      SELECT adrnr UP TO ROWS INTO -adrnr
        FROM kna1 WHERE kunnr -kunnr.
      ENDSELECT.
    ENDLOOP.
  ENDIF.


---------------------- return to custom program


    ELSE.
*****
      CALL FUNCTION 'DELETE_TEXT'
        EXPORTING
          client          sy-mandt
          id              ls_thead-tdid
          language        co_savelang
          name            ls_thead-tdname
          object          ls_thead-tdobject
          savemode_direct 'X'
        EXCEPTIONS
          not_found       1
          OTHERS          2.
      IF sy-subrc <> 0.
* Implement suitable error handling here
      ENDIF.

    ENDIF.

No comments:

Post a Comment