Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

  This document explains how to create function module for generating Customer Hierarchy IDOCs used in ALE distribution of changes (creates/changes/deletes) of SD Customer Hierarchy.

  FUNCTION ZMSTRIDOC_CREATE_SMD_ZDEBHI.

  DATA: BEGIN OF F_KNVHKEY,
MANDT   LIKE KNVH-MANDT,
HITYP   LIKE KNVH-HITYP,
KUNNR   LIKE KNVH-KUNNR,
VKORG   LIKE KNVH-VKORG,
VTWEG   LIKE KNVH-VTWEG,
SPART   LIKE KNVH-SPART,
DATAB   LIKE KNVH-DATAB,
END OF F_KNVHKEY.
*
DATA: BEGIN OF F_KNVH.
INCLUDE STRUCTURE ZKNVHKEY.
DATA: END OF F_KNVH.
*
DATA: BEGIN OF T_KNVHKEY OCCURS 10.
INCLUDE STRUCTURE ZKNVHKEY.
DATA: END OF T_KNVHKEY.
*
DATA: BEGIN OF T_CHGPTRS OCCURS 10.
INCLUDE STRUCTURE BDCP.
DATA: END OF T_CHGPTRS.

DATA: BEGIN OF T_CPIDENT OCCURS 10,
CPIDENT LIKE BDCP-CPIDENT,
END OF T_CPIDENT.

DATA: BEGIN OF T_CPIDENT_KNVH OCCURS 10,
MANDT   LIKE KNVH-MANDT,
HITYP   LIKE KNVH-HITYP,
KUNNR   LIKE KNVH-KUNNR,
VKORG   LIKE KNVH-VKORG,
VTWEG   LIKE KNVH-VTWEG,
SPART   LIKE KNVH-SPART,
DATAB   LIKE KNVH-DATAB,
CPIDENT LIKE BDCP-CPIDENT,
END OF T_CPIDENT_KNVH.

* created idocs
* SAP created variables -- does not follow naming stds.
DATA: CREATED_M_IDOCS LIKE SY-TABIX.
DATA: CREATED_COMM_IDOCS LIKE SY-TABIX.
DATA: CREATED_C_IDOCS LIKE SY-TABIX.
DATA: DONE_SINCE_COMMIT LIKE SY-TABIX.

* read all not processed change pointers for the given messagetype,
* object class KUNHIER
* return code is not checked for since no exception is raised by
* the function module.
CALL FUNCTION 'CHANGE_POINTERS_READ'
EXPORTING
CHANGE_DOCUMENT_OBJECT_CLASS = 'KUNHIER'
MESSAGE_TYPE                 = MESSAGE_TYPE
READ_NOT_PROCESSED_POINTERS  = 'X'
TABLES
CHANGE_POINTERS              = T_CHGPTRS.

*
REFRESH T_KNVHKEY.
*
* create all keys from the change pointers
LOOP AT T_CHGPTRS.
*     table KNVH
*     table KNVH of the change document equals the table KNVH
PERFORM MOVE_X_TO_Y USING T_CHGPTRS-TABKEY+0(3)
                  F_KNVHKEY-MANDT.
IF F_KNVHKEY-MANDT = SPACE.
F_KNVHKEY-MANDT = T_CHGPTRS-MANDT.
ENDIF.
*
PERFORM MOVE_X_TO_Y USING T_CHGPTRS-TABKEY+3(1)
                  F_KNVHKEY-HITYP.
*
PERFORM MOVE_X_TO_Y USING T_CHGPTRS-TABKEY+4(10)
                  F_KNVHKEY-KUNNR.
*
PERFORM MOVE_X_TO_Y USING T_CHGPTRS-TABKEY+14(4)
                  F_KNVHKEY-VKORG.
*
PERFORM MOVE_X_TO_Y USING T_CHGPTRS-TABKEY+18(2)
                  F_KNVHKEY-VTWEG.
*
PERFORM MOVE_X_TO_Y USING T_CHGPTRS-TABKEY+20(2)
                  F_KNVHKEY-SPART.
*
PERFORM MOVE_X_TO_Y USING T_CHGPTRS-TABKEY+22(8)
                  F_KNVHKEY-DATAB.
*
READ TABLE T_KNVHKEY WITH KEY F_KNVHKEY.
*
MOVE F_KNVHKEY-MANDT TO T_KNVHKEY-MANDT.
MOVE F_KNVHKEY-HITYP TO T_KNVHKEY-HITYP.
MOVE F_KNVHKEY-KUNNR TO T_KNVHKEY-KUNNR.
MOVE F_KNVHKEY-VKORG TO T_KNVHKEY-VKORG.
MOVE F_KNVHKEY-VTWEG TO T_KNVHKEY-VTWEG.
MOVE F_KNVHKEY-SPART TO T_KNVHKEY-SPART.
MOVE F_KNVHKEY-DATAB TO T_KNVHKEY-DATAB.
*
PERFORM CONVERT_CDCHGID_TO_MSGFN USING T_CHGPTRS-CDCHGID
                               T_KNVHKEY-MSGFN.
IF SY-SUBRC <> 0.
APPEND T_KNVHKEY.
ELSE.
MODIFY T_KNVHKEY INDEX SY-TABIX.
ENDIF.
*
*       add pointer to processed pointers
MOVE-CORRESPONDING F_KNVHKEY TO T_CPIDENT_KNVH.
MOVE T_CHGPTRS-CPIDENT TO T_CPIDENT_KNVH-CPIDENT.
APPEND T_CPIDENT_KNVH.
ENDLOOP.
*
SORT T_KNVHKEY BY MANDT HITYP KUNNR VKORG VTWEG SPART DATAB.
SORT T_CPIDENT_KNVH BY MANDT HITYP KUNNR VKORG VTWEG SPART DATAB.
*
* initialize counter variables for created idocs
CREATED_M_IDOCS = 0.
CREATED_C_IDOCS = 0.
DONE_SINCE_COMMIT = 0.

CLEAR T_CPIDENT. REFRESH T_CPIDENT.

* call of the idoc creator
LOOP AT T_KNVHKEY WHERE MANDT = SY-MANDT.
CLEAR   F_KNVH.
MOVE-CORRESPONDING T_KNVHKEY TO F_KNVH.
* return code is not checked for since no exception is raised by
* the function module.
*
CALL FUNCTION 'ZMASTERIDOC_CREATE_ZDEBHI'
EXPORTING
  KNVHKEY            = F_KNVH
  RCVPFC             = ' '
  RCVPRN             = '         '
  RCVPRT             = ' '
  SNDPFC             = ' '
  SNDPRN             = '         '
  SNDPRT             = ' '
  MESSAGE_TYPE       = MESSAGE_TYPE
IMPORTING
  CREATED_COMM_IDOCS = CREATED_COMM_IDOCS.
*
CREATED_M_IDOCS = CREATED_M_IDOCS + 1.
CREATED_C_IDOCS = CREATED_C_IDOCS + CREATED_COMM_IDOCS.
DONE_SINCE_COMMIT = DONE_SINCE_COMMIT + 1.
*
* append all processed pointer
LOOP AT T_CPIDENT_KNVH WHERE MANDT = F_KNVH-MANDT
                 AND HITYP = F_KNVH-HITYP
                 AND KUNNR = F_KNVH-KUNNR
                 AND VKORG = F_KNVH-VKORG
                 AND VTWEG = F_KNVH-VTWEG
                 AND SPART = F_KNVH-SPART
                 AND DATAB = F_KNVH-DATAB.
*
T_CPIDENT-CPIDENT = T_CPIDENT_KNVH-CPIDENT.
APPEND T_CPIDENT.
*
ENDLOOP.

IF DONE_SINCE_COMMIT >= C_IDOCS_BEFORE_COMMIT.
DONE_SINCE_COMMIT = 0.
*
* write status of all processed pointers
* return code is not checked for since no exception is raised by
* the function module.
CALL FUNCTION 'CHANGE_POINTERS_STATUS_WRITE'
EXPORTING
    MESSAGE_TYPE           = MESSAGE_TYPE
TABLES
    CHANGE_POINTERS_IDENTS = T_CPIDENT.
*
COMMIT WORK.
* return code is not checked for since no exception is raised by
* the function module.
CALL FUNCTION 'DEQUEUE_ALL'.
*
CLEAR T_CPIDENT. REFRESH T_CPIDENT.
*
ENDIF.
*
ENDLOOP.                             "at t_knvhkey
*
* commit if necessary
IF DONE_SINCE_COMMIT > 0.
*
* write status of all processed pointers
* return code is not checked for since no exception is raised by
* the function module.
CALL FUNCTION 'CHANGE_POINTERS_STATUS_WRITE'
EXPORTING
  MESSAGE_TYPE           = MESSAGE_TYPE
TABLES
  CHANGE_POINTERS_IDENTS = T_CPIDENT.
*
COMMIT WORK.
* return code is not checked for since no exception is raised by
* the function module.
CALL FUNCTION 'DEQUEUE_ALL'.
*
ENDIF.
*
MESSAGE ID 'B1' TYPE 'I' NUMBER '038'
WITH CREATED_M_IDOCS MESSAGE_TYPE.
MESSAGE ID 'B1' TYPE 'I' NUMBER '039'
WITH CREATED_C_IDOCS MESSAGE_TYPE.
ENDFUNCTION.

*&---------------------------------------------------------------------*
*&      Form  MOVE_X_TO_Y
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_T_CHGPTRS-TABKEY+20(2)  text                               *
*      -->P_F_KNVHKEY-SPART  text                                      *
*----------------------------------------------------------------------*
FORM MOVE_X_TO_Y USING X Y.
*
Y = X.
*
ENDFORM.                               " MOVE_X_TO_Y
*&---------------------------------------------------------------------*
*&      Form  CONVERT_CDCHGID_TO_MSGFN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_T_CHGPTRS-CDCHGID  text                                    *
*      -->P_T_KNVHKEY-MSGFN  text                                      *
*----------------------------------------------------------------------*
FORM CONVERT_CDCHGID_TO_MSGFN USING CDCHGID LIKE BDCP-CDCHGID
                        MSGFN LIKE Z1KNVHM-MSGFN.
*
MSGFN = SPACE.
*
CASE CDCHGID.
WHEN 'I'.
MSGFN = C_MSGFN_I.
WHEN 'U'.
MSGFN = C_MSGFN_U.
WHEN ' '.
MSGFN = C_MSGFN_R.
WHEN 'D'.
MSGFN = C_MSGFN_D.
WHEN 'E'.
MSGFN = C_MSGFN_D.
WHEN 'S'.
MSGFN = C_MSGFN_S.
ENDCASE.
*
ENDFORM.                               " CONVERT_CDCHGID_TO_MSGFN
*&---------------------------------------------------------------------*
*&      Form  FILL_SEGMENT_Z1KNVHM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_KNVHKEY-MSGFN  text                                        *
*----------------------------------------------------------------------*
FORM FILL_SEGMENT_Z1KNVHM USING MSGFN.
*
CLEAR Z1KNVHM.
*
Z1KNVHM-MSGFN  =  MSGFN.
Z1KNVHM-HITYP  =  KNVH-HITYP.
Z1KNVHM-KUNNR  =  KNVH-KUNNR.
Z1KNVHM-VKORG  =  KNVH-VKORG.
Z1KNVHM-VTWEG  =  KNVH-VTWEG.
Z1KNVHM-SPART  =  KNVH-SPART.
Z1KNVHM-DATAB  =  KNVH-DATAB.
Z1KNVHM-DATBI  =  KNVH-DATBI.
Z1KNVHM-HKUNNR =  KNVH-HKUNNR.
Z1KNVHM-HVKORG =  KNVH-HVKORG.
Z1KNVHM-HVTWEG =  KNVH-HVTWEG.
Z1KNVHM-HSPART =  KNVH-HSPART.
Z1KNVHM-GRPNO  =  KNVH-GRPNO.
Z1KNVHM-BOKRE  =  KNVH-BOKRE.
Z1KNVHM-PRFRE  =  KNVH-PRFRE.
Z1KNVHM-HZUOR  =  KNVH-HZUOR.
*
ENDFORM.                               " FILL_SEGMENT_Z1KNVHM

  • No labels