SAP MM BAPI OC

From SapWiki

BAPI_PO_CREATE1

Ejemplo 1

form crear_pedido CHANGING p_oc.
*  Rutina que crea los pedidos de traslado (ME21N)

  DATA: ls_header     TYPE bapimepoheader,
        ls_headerx    TYPE bapimepoheaderx,
        lt_items      TYPE STANDARD TABLE OF bapimepoitem,
        ls_items      TYPE bapimepoitem,
        lt_itemsx     TYPE STANDARD TABLE OF bapimepoitemx,
        ls_itemsx     TYPE bapimepoitemx,
        lt_schedules  TYPE STANDARD TABLE OF bapimeposchedule,
        ls_schedules  TYPE bapimeposchedule,
        lt_schedulesx TYPE STANDARD TABLE OF bapimeposchedulx,
        ls_schedulesx TYPE bapimeposchedulx,
        lt_return     TYPE TABLE OF bapiret2,
        ls_return     TYPE bapiret2,
        l_item_idx    TYPE I,
        l_numped      TYPE bapiekkoc-po_number.

  CLEAR: ls_header,
         ls_headerx.

  ls_header-doc_type   = 'ZXXX'.

  SELECT SINGLE EKORG EKGRP BUKRS lifnr INTO
  (ls_header-purch_org,ls_header-pur_group,ls_header-comp_code,ls_header-VENDOR)
  FROM ekko WHERE ebeln = wa_data-zzcontrato.

  ls_header-suppl_plnt = wa_data-werks_ori.
  ls_header-doc_date   = sy-datum.
  ls_header-REF_1      = wa_data-vbeln.

  ls_headerx-doc_type   = 'X'.
  ls_headerx-purch_org  = 'X'.
  ls_headerx-pur_group  = 'X'.
  ls_headerx-comp_code  = 'X'.
  ls_headerx-suppl_plnt = 'X'.
  ls_headerx-doc_date   = 'X'.
  ls_headerX-REF_1      = 'X'.
  ls_headerX-VENDOR     = 'X'.

*Grabamos las tablas correspondientes a los materiales y repartos
  l_item_idx          = wa_data-posnr.
  ls_items-po_item    = l_item_idx.
  ls_items-material   = wa_data-matnr.
  ls_items-plant      = wa_data-werks_sumi.
  ls_items-stge_loc   = wa_data-lgort_sumi.
*ls_items-item_cat    = '7'.
  ls_items-po_unit    = wa_data-VRKME.
  ls_items-TRACKINGNO = wa_data-posnr.  "wa_data-vbeln.   "N° necesidad
  ls_items-QUANTITY   = wa_data-KWMENG.

  ls_items-agreement  = wa_data-zzcontrato.
  select SINGLE * into @data(ls_ekpo) from ekpo
                               where ebeln = @wa_data-zzcontrato
                                 and LOEKZ <> 'X'.
  ls_items-agmt_item = ls_ekpo-ebelp.

  select SINGLE NETWR INTO ls_items-net_price from vbap
                               where vbeln = wa_data-vbeln and
                                     posnr = wa_data-posnr.
*ls_items-no_rounding = 'X'.
  APPEND ls_items TO lt_items.

  ls_itemsx-po_item     = l_item_idx.
  ls_itemsx-po_itemx    = 'X'.
  ls_itemsx-material    = 'X'.
  ls_itemsx-plant       = 'X'.
  ls_itemsx-stge_loc    = 'X'.
*ls_itemsx-item_cat    = 'X'.
  ls_itemsx-po_unit     = 'X'.
*ls_itemsx-no_rounding = 'X'.
  ls_itemsx-TRACKINGNO  = 'X'.
  ls_itemsX-QUANTITY    = 'X'.
  ls_itemsX-agreement   = 'X'.
  ls_itemsX-agmt_item   = 'X'.
  ls_itemsX-net_price   = 'X'.
  APPEND ls_itemsx TO lt_itemsx.

  ls_schedules-po_item       = l_item_idx.
  ls_schedules-quantity      = wa_data-KWMENG.
  ls_schedules-delivery_date = wa_data-edatu.
  APPEND ls_schedules TO lt_schedules.

  ls_schedulesx-po_item       = l_item_idx.
  ls_schedulesx-po_itemx      = 'X'.
  ls_schedulesx-quantity      = 'X'.
  ls_schedulesx-delivery_date = 'X'.
  APPEND ls_schedulesx TO lt_schedulesx.

* Llamamos a la BAPI para crear el pedido de traslados
  CALL FUNCTION 'BAPI_PO_CREATE1'
  EXPORTING
    poheader               = ls_header
    poheaderx              = ls_headerx
  IMPORTING
    exppurchaseorder       = l_numped
  TABLES
    RETURN                 = lt_return
    poitem                 = lt_items
    poitemx                = lt_itemsx
    poschedule             = lt_schedules
    poschedulex            = lt_schedulesx
    .

  if l_numped is INITIAL.
* error
   LOOP AT lt_return INTO ls_return
    GROUP BY COND string( WHEN ls_return-type = 'E' THEN |EQ 1|
                          WHEN ls_return-type = 'A' THEN |EQ 2|
                          WHEN ls_return-type = 'W' THEN |EQ 3|
                          WHEN ls_return-type = 'I' THEN |EQ 4|
                          )
              ASCENDING
              ASSIGNING FIELD-SYMBOL(<group>).
     LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<ls_return>).
     ENDLOOP.
     EXIT.
   ENDLOOP.
   IF sy-subrc = 0.
      move <ls_return>-message to g_message.
      PERFORM add_news USING 'Crear Pedido:' icon_red_light.
      wa_data-stat =  icon_red_light.
   ENDIF.
  else.
* éxito
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        WAIT = 'X'.

      p_oc = l_numped.

* Pedido grabado correctamente, lo informamos en el log
        MESSAGE i000(0K) WITH 'Pedido grabado correctamente' l_numped INTO g_message.
        PERFORM add_news USING 'Crear Pedido:' icon_green_light.
        wa_data-stat =  icon_green_light.
  endif.

  CLEAR: lt_items, lt_schedules, lt_itemsx, lt_schedulesx.
  REFRESH: lt_items, lt_schedules, lt_itemsx, lt_schedulesx.

ENDFORM.

Ejemplo 2, Crear orden de compra con referencia a una factura(al momento de crear factura)

*----------------------------------------------------------------------*
*      Print of a invoice by SAPscript SMART FORMS               *
*----------------------------------------------------------------------*

REPORT zsd_crear_pcompra.

* declaration of data
INCLUDE rlb_invoice_data_declare.
* definition of forms
INCLUDE rlb_invoice_form01.
INCLUDE rlb_print_forms.

*---------------------------------------------------------------------*
*       FORM ENTRY
*---------------------------------------------------------------------*
FORM entry USING return_code us_screen.

  DATA: lf_retcode TYPE sy-subrc.
  CLEAR retcode.
  xscreen = us_screen.
  PERFORM processing USING us_screen
                     CHANGING lf_retcode.
  IF lf_retcode NE 0.
    return_code = 1.
  ELSE.
    return_code = 0.
  ENDIF.

ENDFORM.                    "ENTRY
*---------------------------------------------------------------------*
*       FORM PROCESSING                                               *
*---------------------------------------------------------------------*
FORM processing USING proc_screen
                CHANGING cf_retcode.

  DATA: ls_print_data_to_read TYPE lbbil_print_data_to_read.
  DATA: ls_bil_invoice TYPE lbbil_invoice.
  DATA: lf_fm_name            TYPE rs38l_fnam.
  DATA: ls_control_param      TYPE ssfctrlop.
  DATA: ls_composer_param     TYPE ssfcompop.
  DATA: ls_recipient          TYPE swotobjid.
  DATA: ls_sender             TYPE swotobjid.
  DATA: lf_formname           TYPE tdsfname.
  DATA: ls_addr_key           LIKE addr_key.
  DATA: ls_dlv-land           LIKE vbrk-land1.
  DATA: ls_job_info           TYPE ssfcrescl.

   DATA ld_vbeln LIKE vbrk-vbeln.

* SmartForm from customizing table TNAPR
  lf_formname = tnapr-sform.

  IF nast-objky+10 NE space.
    nast-objky = nast-objky+16(10).
  ELSE.
    nast-objky = nast-objky.
  ENDIF.

  ld_vbeln = nast-objky.


*  IF cf_retcode = 0.
    CALL FUNCTION 'Z_MM_BAPI_PO_CREATE1' in BACKGROUND TASK
*    AS SEPARATE UNIT DESTINATION 'NONE'
      EXPORTING
        I_VBELN       = ld_vbeln
        i_nast        = nast
*     IMPORTING
*       O_EBELN       =
              .
*  ENDIF.

* get SmartForm protocoll and store it in the NAST protocoll
* PERFORM ADD_SMFRM_PROT.

* get SmartForm protocoll and store it in the NAST protocoll
* PERFORM ADD_SMFRM_PROT.

ENDFORM.                    "PROCESSING

FUNCTION Z_MM_BAPI_PO_CREATE1.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     VALUE(I_VBELN) TYPE  VBRK-VBELN
*"     VALUE(I_NAST) TYPE  NAST OPTIONAL
*"  EXPORTING
*"     VALUE(O_EBELN) TYPE  EKKO-EBELN
*"  EXCEPTIONS
*"      DOC_NO_EXISTE
*"----------------------------------------------------------------------
  data ls_knmt type knmt.
  data ld_AMOUNT_EXTERNAL LIKE  BAPICURR-BAPICURR.

  nast = i_nast.

  select single * into gs_vbrk from vbrk where vbeln = i_vbeln.
  if sy-subrc <> 0.
    if nast is NOT INITIAL.
      CLEAR : syst-msgv1, syst-msgv2, syst-msgv3.
      CALL FUNCTION 'NAST_PROTOCOL_INITIALIZE'.
      sy-msgid = 'VN'.
      sy-msgno = '056'.
      sy-msgty = 'I'.
      sy-msgv1 = 'ZSD_CREAR_PCOMPRA'  .
      sy-msgv2 = 'ENTRY'.
      CALL FUNCTION 'NAST_PROTOCOL_UPDATE'
        EXPORTING
          msg_arbgb = sy-msgid
          msg_nr    = sy-msgno
          msg_ty    = sy-msgty
          msg_v1    = sy-msgv1
          msg_v2    = sy-msgv2
        EXCEPTIONS
          OTHERS    = 0.
      CLEAR : syst-msgv1, syst-msgv2, syst-msgv3.
      syst-msgid = 'VF'.
      syst-msgno = '041'.
      syst-msgty = 'E'.
      syst-msgv1 = i_vbeln.
      CALL FUNCTION 'NAST_PROTOCOL_UPDATE'
        EXPORTING
          msg_arbgb = syst-msgid
          msg_nr    = syst-msgno
          msg_ty    = syst-msgty
          msg_v1    = syst-msgv1
          msg_v2    = syst-msgv2
          msg_v3    = syst-msgv3
        EXCEPTIONS
          OTHERS    = 1.
      NAST-VSTAT = '2'.
      PERFORM protocol_store USING    nast-kappl
                                nast-kschl
                       CHANGING nast-cmfpnr.
      PERFORM nast_update.
      COMMIT WORK AND WAIT.
    endif.
    RAISE doc_no_existe.
  endif.

  if gs_vbrk-FKSTO = 'X'.
    if nast is NOT INITIAL.
      CLEAR : syst-msgv1, syst-msgv2, syst-msgv3.
      CALL FUNCTION 'NAST_PROTOCOL_INITIALIZE'.
      sy-msgid = 'VN'.
      sy-msgno = '056'.
      sy-msgty = 'I'.
      sy-msgv1 = 'ZSD_CREAR_PCOMPRA'  .
      sy-msgv2 = 'ENTRY'.
      CALL FUNCTION 'NAST_PROTOCOL_UPDATE'
        EXPORTING
          msg_arbgb = sy-msgid
          msg_nr    = sy-msgno
          msg_ty    = sy-msgty
          msg_v1    = sy-msgv1
          msg_v2    = sy-msgv2
        EXCEPTIONS
          OTHERS    = 0.
      CLEAR : syst-msgv1, syst-msgv2, syst-msgv3.
      syst-msgid = 'VF'.
      syst-msgno = '041'.
      syst-msgty = 'E'.
      syst-msgv1 = 'Factura está anulada'.
      CALL FUNCTION 'NAST_PROTOCOL_UPDATE'
        EXPORTING
          msg_arbgb = syst-msgid
          msg_nr    = syst-msgno
          msg_ty    = syst-msgty
          msg_v1    = syst-msgv1
          msg_v2    = syst-msgv2
          msg_v3    = syst-msgv3
        EXCEPTIONS
          OTHERS    = 1.
      NAST-VSTAT = '2'.
      PERFORM protocol_store USING    nast-kappl
                                nast-kschl
                       CHANGING nast-cmfpnr.
      PERFORM nast_update.
      COMMIT WORK AND WAIT.
    endif.
    RAISE doc_no_existe.
  endif.


  select * into table gt_vbrp from vbrp where vbeln = i_vbeln.

  read TABLE gt_vbrp into gs_VBRP index 1.
*--------------------------------------------------------------------*
* datos cabecera
*--------------------------------------------------------------------*
  CLEAR: gs_poheader,
         gs_poheaderx.

*---- Clase Documento
  gs_poheader-doc_type  = 'ZCAD'.
  gs_poheaderx-doc_type = 'X'.

*---- Proveedor
  select single EIKTO into gs_poheader-VENDOR from KNVV
                where KUNNR = gs_VBRK-KUNRG
                and VKORG = gs_VBRK-VKORG
                and VTWEG = gs_VBRK-VTWEG
                and SPART = gs_VBRK-SPART.
*  gs_poheader-VENDOR     = gs_vbrk-kunrg.
*  gs_poheader-VENDOR     = 'COMPANY'.
  gs_poheaderx-VENDOR    = 'X'.

****FECHA CREACION
  gs_poheader-CREAT_DATE   = gs_VBRP-PRSDT.
  gs_poheaderx-CREAT_DATE = 'X'.

*---- Org. de Compra
  gs_poheader-purch_org  = 'OCCA'.
  gs_poheaderx-purch_org = 'X'.

*---- Grupo de Compra
  gs_poheader-PUR_GROUP   = 'C15'.
  gs_poheaderx-PUR_GROUP  = 'X'.

*---- Sociedad
  gs_poheader-comp_code  = '8000'.
  gs_poheaderx-comp_code = 'X'.

****tipo de cambio
  gs_poheader-EXCH_RATE   = gs_VBRK-KURRF.
  gs_poheaderx-EXCH_RATE = 'X'.

****tipo de cambio fijado
  gs_poheader-EX_RATE_FX  = 'X'. "gs_VBRK-CPKUR.
  gs_poheaderx-EX_RATE_FX = 'X'.

****factura
  gs_poheader-COLLECT_NO = gs_VBRK-VBELN.
  gs_poheaderx-collect_no = 'X'.

****moneda
  gs_poheader-CURRENCY = gs_VBRK-waerk.
  gs_poheaderx-CURRENCY = 'X'.

****fecha documento
  gs_poheader-DOC_DATE = gs_VBRP-PRSDT.
  gs_poheaderx-doc_date = 'X'.
*--------------------------------------------------------------------*
* datos posición
*--------------------------------------------------------------------*
  CLEAR  : gt_poitem[],
           gt_poitemx[],
           gt_POSCHEDULE[],
           gt_POSCHEDULEx[],
           gt_POACCOUNT[],
           gt_POACCOUNTx[],
           gt_return[].

*---- Actualizar posiciones del documento
  LOOP AT gt_vbrp INTO gs_vbrp.
    clear: gs_poitemx,
           gs_poitem,
           gs_POSCHEDULE,
           gs_POSCHEDULEx,
           gs_POACCOUNT,
           gs_POACCOUNTx,
           ls_knmt.

*---- Posición
    gs_poitem-po_item   = gs_VBRP-POSNR.
    gs_poitemx-po_item  = gs_VBRP-POSNR.
    gs_poitemx-po_itemx = 'X'.

    gs_POSCHEDULE-po_item = gs_VBRP-POSNR.
    gs_POSCHEDULEx-po_item = gs_VBRP-POSNR.
    gs_POSCHEDULEx-po_itemx = 'X'.

    gs_POACCOUNT-po_item =  gs_VBRP-POSNR.
    gs_POACCOUNTx-po_item = gs_VBRP-POSNR.
    gs_POACCOUNTx-po_itemx = 'X'.

*---- Material
    SELECT single * into ls_knmt FROM  knmt
           WHERE  vkorg  = gs_vbrk-vkorg
           AND    vtweg  = gs_vbrk-vtweg
           AND    kunnr  = gs_vbrk-kunrg
           AND    matnr  = gs_vbrp-matnr.


    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        input        = ls_knmt-KDMAT
      IMPORTING
        output       = gs_poitem-material
      EXCEPTIONS
        length_error = 1
        OTHERS       = 2.

** tipo imputacion
    case ls_knmt-VWPOS.
      when 'ZK'."Centro de costo
        gs_poitem-ACCTASSCAT  = 'J'.
        data: v_tabla like ausp occurs 0 with header line, "v_tabla TYPE TABLE OF ausp,
              v_OBJECT like KSSK-OBJEK.
        move gs_poitem-material to v_OBJECT.
        CALL FUNCTION 'CLA2_GET_ALL_ALLOCATIONS'
          EXPORTING
            OBJECT                   = v_OBJECT
            CLASS_TYPE               = '001'
            ALLOC_ID                 = 'O'
            TABLE                    = 'MARA'
           KEY_DATE                 = SY-DATUM
          TABLES
           VALUES                   = v_tabla
*           ALLOCATIONS              =
*         EXCEPTIONS
*           INVALID_CLASS_TYPE       = 1
*           INVALID_OBJECT           = 2
*           OTHERS                   = 3
.
        IF SY-SUBRC = 0.
          read TABLE v_tabla WITH KEY ATINN = 'ZMAT_CECO_CCNI'.
          data V_CECO(10) type N.
          move V_TABLA-ATWRT to V_CECO.
          gs_POACCOUNT-COSTCENTER = V_CECO.
*         move V_TABLA-ATWRT to gs_POACCOUNT-COSTCENTER.
*         gs_POACCOUNT-COSTCENTER = '0080100100'.
          gs_POACCOUNTx-COSTCENTER = 'X'.
        ENDIF.
      when 'ZP'."Elemento PEP
        gs_poitem-ACCTASSCAT  = 'X'.
        data V_IHREZ like vbkd-IHREZ.
        select single IHREZ into V_IHREZ from vbkd
          where VBELN = GS_VBRP-AUBEL
          and POSNR = GS_VBRP-AUPOS.
        if sy-subrc <> 0.
          select single IHREZ into V_IHREZ from vbkd
          where VBELN = GS_VBRP-AUBEL.
        endif.
        data: V_EQUNR(18) type N..
        move V_IHREZ to V_EQUNR.
*        CONDENSE V_EQUNR.
        select single ZZPOSID into gs_POACCOUNT-WBS_ELEMENT from EQUI
          where EQUNR = V_EQUNR.
        gs_POACCOUNTx-WBS_ELEMENT = 'X'.

    endcase.
    gs_poitemx-ACCTASSCAT = 'X'.

*    gs_poitem-material  = ls_knmt-KDMAT.
    gs_poitemx-material = 'X'.

*---- Cantidad
    gs_poitem-quantity  = gs_VBRP-FKIMG.
    gs_poitemx-quantity = 'X'.

***fecha entrega
    gs_POSCHEDULE-DELIVERY_DATE = gs_VBRP-PRSDT.
    gs_POSCHEDULEx-DELIVERY_DATE =  gs_VBRP-PRSDT.

* monto neto
    CALL FUNCTION 'BAPI_CURRENCY_CONV_TO_EXTERNAL'
      EXPORTING
        CURRENCY        = gs_vbrk-waerk
        AMOUNT_INTERNAL = gs_VBRP-NETWR
      IMPORTING
        AMOUNT_EXTERNAL = ld_AMOUNT_EXTERNAL.
    .

    move ld_AMOUNT_EXTERNAL to gs_poitem-NET_PRICE .
*    gs_poitem-NET_PRICE    = gs_VBRP-NETWR.
    gs_poitemx-NET_PRICE   = 'X'.

    gs_poitem-PO_PRICE = '2'.
    gs_poitemx-PO_PRICE = 'X'.

*gs_poaccount-NET_VALUE =
*gs_poaccountx-NET_VALUE =

*---- Centro Destino
    gs_poitem-plant  = 'CC04'.
    gs_poitemx-plant = 'X'.

*---- División
    gs_POACCOUNT-BUS_AREA  = 'CL01'.
    gs_POACCOUNTx-BUS_AREA = 'X'.


***ind impuesto
    case gs_vbrk-FKART.
      when 'ZFEA'.
        gs_poitem-TAX_CODE  = 'A0'.
        gs_poitemx-TAX_CODE = 'X'.
      when 'ZFEN'.
        gs_poitem-TAX_CODE  = 'E0'.
        gs_poitemx-TAX_CODE = 'X'.
    endcase.

* nro necesidad
    gs_poitem-TRACKINGNO = gs_VBRP-vbelv.
    gs_poitemx-TRACKINGNO = 'X'.

* Verif MIGO
    gs_poitem-GR_BASEDIV  = space.
    gs_poitemx-GR_BASEDIV = 'X'.

*---- Grabar Datos
    APPEND gs_poitem TO  gt_poitem.
    APPEND gs_poitemx TO gt_poitemx.

    append gs_POSCHEDULE to gt_POSCHEDULE.
    append gs_POSCHEDULEx to gt_POSCHEDULEx.

    append gs_POACCOUNT to gt_POACCOUNT.
    append gs_POACCOUNTx to gt_POACCOUNTx.

  ENDLOOP.

  CALL FUNCTION 'BAPI_PO_CREATE1'
    EXPORTING
      poheader         = gs_poheader
      poheaderx        = gs_poheaderx
    IMPORTING
      exppurchaseorder = gd_ebeln
    TABLES
      return           = gt_return
      poitem           = gt_poitem
      poitemx          = gt_poitemx
      POSCHEDULE       = gt_POSCHEDULE
      POSCHEDULEx      = gt_POSCHEDULEx
      POACCOUNT        = gt_POACCOUNT
      POACCOUNTx       = gt_POACCOUNTx.

*error
  LOOP AT gt_return  into gs_return WHERE type = 'E'
                OR type = 'A'.
  ENDLOOP.
  IF sy-subrc = 0.
    if nast is NOT INITIAL.
      CLEAR : syst-msgv1, syst-msgv2, syst-msgv3.
      CALL FUNCTION 'NAST_PROTOCOL_INITIALIZE'.
      sy-msgid = 'VN'.
      sy-msgno = '056'.
      sy-msgty = 'I'.
      sy-msgv1 = 'ZSD_CREAR_PCOMPRA'  .
      sy-msgv2 = 'ENTRY'.
      CALL FUNCTION 'NAST_PROTOCOL_UPDATE'
        EXPORTING
          msg_arbgb = sy-msgid
          msg_nr    = sy-msgno
          msg_ty    = sy-msgty
          msg_v1    = sy-msgv1
          msg_v2    = sy-msgv2
        EXCEPTIONS
          OTHERS    = 0.
      CLEAR : syst-msgv1, syst-msgv2, syst-msgv3.
      syst-msgid = gs_return-id.
      syst-msgno = gs_return-number.
      syst-msgty = 'E'.
      syst-msgv1 = gs_return-message_v1.
      syst-msgv2 = gs_return-message_v2.
      syst-msgv3 = gs_return-message_v3.
      syst-msgv4 = gs_return-message_v4.
      CALL FUNCTION 'NAST_PROTOCOL_UPDATE'
        EXPORTING
          msg_arbgb = syst-msgid
          msg_nr    = syst-msgno
          msg_ty    = syst-msgty
          msg_v1    = syst-msgv1
          msg_v2    = syst-msgv2
          msg_v3    = syst-msgv3
        EXCEPTIONS
          OTHERS    = 1.
      NAST-VSTAT = '2'.
      PERFORM protocol_store USING    nast-kappl
                                nast-kschl
                       CHANGING nast-cmfpnr.
      PERFORM nast_update.
      COMMIT WORK AND WAIT.
    endif.
    return.   "<------------------------

*---- Grabar Documento
  elseIF ( NOT gd_ebeln IS INITIAL ).
    if nast is NOT INITIAL.
      CLEAR : syst-msgv1, syst-msgv2, syst-msgv3.
      CALL FUNCTION 'NAST_PROTOCOL_INITIALIZE'.
      sy-msgid = 'VN'.
      sy-msgno = '056'.
      sy-msgty = 'S'.
      sy-msgv1 = 'ZSD_CREAR_PCOMPRA'  .
      sy-msgv2 = 'ENTRY'.
      CALL FUNCTION 'NAST_PROTOCOL_UPDATE'
        EXPORTING
          msg_arbgb = sy-msgid
          msg_nr    = sy-msgno
          msg_ty    = sy-msgty
          msg_v1    = sy-msgv1
          msg_v2    = sy-msgv2
        EXCEPTIONS
          OTHERS    = 0.
      CLEAR : syst-msgv1, syst-msgv2, syst-msgv3,syst-msgv4.
      syst-msgid = '06'.
      syst-msgno = '017'.
      syst-msgty = 'S'.
      syst-msgv1 = 'PED.CCNI'.
      syst-msgv2 = gd_ebeln.
      CALL FUNCTION 'NAST_PROTOCOL_UPDATE'
        EXPORTING
          msg_arbgb = syst-msgid
          msg_nr    = syst-msgno
          msg_ty    = syst-msgty
          msg_v1    = syst-msgv1
          msg_v2    = syst-msgv2
        EXCEPTIONS
          OTHERS    = 1.
      NAST-VSTAT = '1'.
      PERFORM protocol_store USING nast-kappl
                                   nast-kschl
                          CHANGING nast-cmfpnr.
      PERFORM nast_update.
    endif.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        WAIT = 'X'.

    o_ebeln = gd_ebeln.
  Endif.

ENDFUNCTION.

FORM PROTOCOL_STORE USING    VALUE(PI_KAPPL) LIKE NAST-KAPPL
                            VALUE(PI_KSCHL) LIKE NAST-KSCHL
                   CHANGING P_CMFPNR  LIKE NAST-CMFPNR.
 DATA: L_NAST_CMFPNR LIKE NAST-CMFPNR.
* store protocol entries for this message type?
   CALL FUNCTION 'NAST_PROTOCOL_STORE'
        IMPORTING
             MSG_CMFPNR         = L_NAST_CMFPNR
        EXCEPTIONS
             NO_PROTOCOL_UPDATE = 01.
   IF SY-SUBRC EQ 0.
     P_CMFPNR = L_NAST_CMFPNR.
   ENDIF.
ENDFORM.

FORM NAST_UPDATE .
   UPDATE NAST.
   IF SY-SUBRC NE 0.
     INSERT NAST.
     IF SY-SUBRC NE 0.
     ENDIF.
   ENDIF.
ENDFORM.                    " NAST_UPDATE