SAP SD BAPI

From SapWiki

SAP Note 93091 - Information about the BAPIs in Sales and Distribution

Crear Pedido de Venta VA01

Modificar Pedido de Venta VA02

form cierra using i_vbeln
                  i_data type ty_t_data.
 data: vb like BAPIVBELN-VBELN.
 data: cl like BAPISDH1X.
 data: rt type table of BAPIRET2 with header line.
 data: rt2 type BAPIRET2.
 data: pt type table of BAPIPARNRC with header line.
 data ls_order_item_inx type BAPISDITMX.
 data ls_ORDER_ITEM_IN type BAPISDITM .
 data lt_order_item_inx type TABLE OF BAPISDITMX.
 data lt_ORDER_ITEM_IN type TABLE OF BAPISDITM .
 vb = i_vbeln.
 cl-updateflag = 'U'.
 loop at i_data into gs_data.
   ls_order_item_inx-ITM_NUMBER = gs_data-posnr.
   ls_order_item_inx-UPDATEFLAG = 'U'.
   ls_order_item_inx-REASON_REJ = 'X'. "motivo de rechazo
   APPEND ls_order_item_inx to lt_order_item_inx.
   ls_order_item_in-ITM_NUMBER = gs_data-posnr.
   ls_order_item_in-REASON_REJ = 'Z2'. "motivo de rechazo
   APPEND ls_order_item_in to lt_order_item_in.
 endloop.
 CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
   EXPORTING
     SALESDOCUMENT    = vb
     ORDER_HEADER_INX = cl
   TABLES
     ORDER_ITEM_IN    = lt_ORDER_ITEM_IN
     ORDER_ITEM_INX   = lt_ORDER_ITEM_INX
     RETURN           = rt.

 loop at rt where type = 'E'.
   exit.
 endloop.
 if sy-subrc ne 0.
   rt-MESSAGE = 'Procesado con exito'.
 endif.
 CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
   EXPORTING
     WAIT   = 'X'
   IMPORTING
     RETURN = rt2.
 loop at i_data into gs_data.
   write:/ gs_data-vbeln,
           11 gs_data-kunnr,
           22 gs_kna1-name1,
           57 gs_data-posnr,
           64 gs_data-matnr,
           82 gs_data-ernam,
           94 gs_data-audat,
          105 rt-message.
 endloop.
endform.

Crear Factura VF01

Ejemplo 1

FUNCTION z_sd_crea_factura.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     REFERENCE(I_VBELN) TYPE  LIKP-VBELN
*"     REFERENCE(I_FKART) TYPE  RV60A-FKART
*"     REFERENCE(I_REF_DOC_CA) TYPE  VBTYP_V
*"     REFERENCE(I_DOC_TYPE) TYPE  AUART
*"  EXPORTING
*"     REFERENCE(E_VBELN) TYPE  VBRK-VBELN
*"     REFERENCE(E_RETURN) TYPE  BAPIRET2_T
*"----------------------------------------------------------------------
  data lt_return1 type bapiret1_tab.

  DATA ld_mode(1) VALUE 'A'. " 'N'.

  DATA lt_mtxt LIKE TABLE OF bdcmsgcoll WITH HEADER LINE.
  DATA ls_return TYPE bapiret2.
  DATA ls_return1 TYPE bapiret1.

  CLEAR: e_return[],
         bdcdata[],
         lt_mtxt[],
         e_vbeln.

  PERFORM crea_factura USING i_fkart
                             i_vbeln
                             i_ref_doc_ca
                             i_doc_type
                      CHANGING e_vbeln
                               lt_return1[].

loop at lt_return1 into ls_return1.
    MOVE-CORRESPONDING ls_return1 TO ls_return.
    APPEND ls_return TO e_return.
ENDLOOP.

ENDFUNCTION.

FORM crea_factura USING p_fkart
                        p_vbeln
                        p_ref_doc_ca
                        p_doc_type
               CHANGING p_vbeln_factuta
                        p_return TYPE bapiret1_tab.

  DATA lt_datain TYPE STANDARD TABLE OF bapivbrk WITH HEADER LINE.
  DATA lt_return TYPE STANDARD TABLE OF bapiret1 WITH HEADER LINE.
  DATA lt_success TYPE STANDARD TABLE OF bapivbrksuccess WITH HEADER LINE.

  CLEAR: lt_datain, lt_datain[],
         lt_return, lt_return[],
         lt_success, lt_success[].

  lt_datain-ref_doc    = p_vbeln.
  lt_datain-ref_doc_ca = p_ref_doc_ca. " ls_likp-vbtyp.
  lt_datain-doc_type   = p_doc_type.    "ls_likp-lfart.
  lt_datain-ordbilltyp = p_fkart.
  APPEND lt_datain.

  CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'
    TABLES
      billingdatain = lt_datain
      return        = lt_return
      success       = lt_success.
  READ TABLE lt_success INDEX 1.
  IF sy-subrc = 0.
    p_vbeln_factuta = lt_success-bill_doc.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
     EXPORTING
       WAIT          = 'X'
              .
  endif.
    p_return[] = lt_return[].

ENDFORM.

Ejemplo 2

FORM yydlb_proforma_faktura_house USING p_fkart
                                        p_lf_vbeln.
                                        
 SELECT SINGLE * FROM likp WHERE vbeln = p_lf_vbeln.
* SAP Note Number 597217 - Eliminate Double Items
 CLEAR billingdatain.
 REFRESH billingdatain.
 
 DATA: lv_in_update_task TYPE sy-subrc.
 CALL FUNCTION 'TH_IN_UPDATE_TASK'
   IMPORTING
     in_update_task = lv_in_update_task.
     
 billingdatain-ref_doc    = p_lf_vbeln.
 billingdatain-ref_doc_ca = likp-vbtyp.
 billingdatain-doc_type   = likp-lfart.
 billingdatain-ordbilltyp = p_fkart.
 APPEND billingdatain.
 
 CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'
   TABLES
     billingdatain = billingdatain
     return        = return
     success       = success.
 READ TABLE success INDEX 1.
 IF sy-subrc = 0.
   yy_fk_vbeln = success-bill_doc.
 ELSE.
   retcode = 3.
 ENDIF.
ENDFORM.

Contabilizar SM - VL02N->Contabilizar SM

FUNCTION Z_SD_CONT_SAL_MERC.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     REFERENCE(I_VBELN) TYPE  LIKP-VBELN
*"  EXPORTING
*"     REFERENCE(E_VBELN) TYPE  LIKP-VBELN
*"     REFERENCE(E_RETURN) TYPE  BAPIRET2_T
*"----------------------------------------------------------------------
  CLEAR: e_RETURN[],
         bdcdata[],
         lt_mtxt[],
         e_vbeln.

  perFORM cont_sm USING i_vbeln CHANGING e_return.
 
ENDFUNCTION.

FORM cont_sm USING p_vbeln CHANGING p_return type bapiret2_t.
  DATA: ls_likp       TYPE likp,
        ls_vbkok      TYPE vbkok,
        lv_fail,
        lt_prott      TYPE STANDARD TABLE OF prott
        WITH HEADER LINE.

  data ls_return type bapiret2.

  SELECT SINGLE * INTO ls_likp FROM likp
                              WHERE vbeln = p_vbeln.
  
  CHECK NOT ls_likp IS INITIAL.     
  
  ls_vbkok-vbeln_vl = ls_likp-vbeln.
  ls_vbkok-wabuc    = 'X'.
  SET UPDATE TASK LOCAL.
  
  CALL FUNCTION 'WS_DELIVERY_UPDATE'
  EXPORTING
    vbkok_wa                 = ls_vbkok
    synchron                 = 'X'
    no_messages_update       = ' '
    update_picking           = 'X'
    COMMIT                   = ' '
    delivery                 = ls_likp-vbeln
    nicht_sperren            = 'X'
    if_error_messages_send_0 = 'X'
  TABLES
    prot                     = lt_prott
  EXCEPTIONS
    error_message            = 1
    OTHERS                   = 2.

  IF sy-subrc <> 0.
    ROLLBACK WORK.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO ls_return-message.
    append ls_return to p_return.
    EXIT.
  ELSE.
    LOOP AT lt_prott.
      CHECK lt_prott-msgty = 'E' OR
      lt_prott-msgty = 'A'.
      lv_fail = 'X'.
    ENDLOOP.
    IF lv_fail IS INITIAL.
      COMMIT WORK AND WAIT.
    ELSE.
      ROLLBACK WORK.
      MOVE lt_prott-msgid TO ls_return-ID.
      MOVE lt_prott-msgty TO ls_return-TYPE.
      MOVE lt_prott-msgno TO ls_return-NUMBER.
      MOVE lt_prott-msgv1  TO ls_return-MESSAGE_v1.
      MOVE lt_prott-msgv2  TO ls_return-MESSAGE_v2.
      MOVE lt_prott-msgv3  TO ls_return-MESSAGE_v3.
      MOVE lt_prott-msgv4  TO ls_return-MESSAGE_v4.
      
      MESSAGE ID lt_prott-MSGID TYPE lt_prott-MSGTY NUMBER lt_prott-MSGNo
      WITH lt_prott-MSGV1 lt_prott-MSGV2 lt_prott-MSGV3 lt_prott-MSGV4
      INTO ls_return-MESSAGE.
      
      APPEND ls_return TO p_return.
      EXIT.
    ENDIF.
  ENDIF.

ENDFORM.

Cambiar Lote de Entrega

FUNCTION z_sd_cambia_lote.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     REFERENCE(I_VBELN) TYPE  LIKP-VBELN
*"     REFERENCE(I_VBELN_FACTURA) TYPE  VBRK-VBELN
*"  EXPORTING
*"     REFERENCE(E_RETURN) TYPE  BAPIRET2_T
*"----------------------------------------------------------------------

  PERFORM clear_entrega USING  i_vbeln i_vbeln_factura CHANGING e_return[].
  
  LOOP AT e_return ASSIGNING FIELD-SYMBOL(<return>) where type = 'E' OR type = 'A'.
    MESSAGE ID <return>-ID TYPE <return>-TYPE NUMBER <return>-NUMBER
    WITH <return>-message_v1 <return>-message_v2 <return>-message_v3 <return>-message_v4
    INTO <return>-MESSAGE.
  ENDLOOP.
  IF sy-subrc = 0.
    ROLLBACK WORK.
  ELSE.
    IF NOT line_exists( e_return[ type = 'S' id = 'Z' number = '999' ] ).
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'
        .
    ENDIF.
  ENDIF.

ENDFUNCTION.

types: BEGIN OF ty_lote,
  vbeln type vbfa-vbeln,
  posnn type vbfa-posnn,
  charg type lips-charg,
  UECHA type lips-UECHA,
  umvkz TYPE lips-umvkz,
  umvkn TYPE lips-umvkn,
  umrev TYPE lips-umrev,
  lfimg TYPE lips-lfimg,
  lgmng TYPE lips-lgmng,
  END OF ty_lote.
types ty_lote_t type TABLE OF ty_lote.

data gt_lote type ty_lote_t.
  
FORM buscar_lote USING p_vbeln_factura p_lips TYPE lips CHANGING p_lote TYPE ty_lote_t .
* buscar pedido

  CLEAR p_lote[].

  SELECT SINGLE vbelv INTO @DATA(l_vbelv) FROM vbfa WHERE vbeln = @p_vbeln_factura AND
                                                          vbtyp_v = 'C' AND
                                                          vbtyp_n = 'M'.
* buscar entregas
  SELECT * INTO CORRESPONDING FIELDS OF TABLE p_lote FROM vbfa WHERE vbelv = l_vbelv AND vbtyp_n = 'J'.

  LOOP AT p_lote ASSIGNING FIELD-SYMBOL(<ls_lote>).
    SELECT SINGLE charg
                  uecha
                  umvkz
                  umvkn
                  umrev
                  lfimg
                  lgmng
                  INTO CORRESPONDING FIELDS OF <ls_lote> FROM lips WHERE vbeln = <ls_lote>-vbeln
                                              AND posnr = <ls_lote>-posnn.
  ENDLOOP.

ENDFORM.

FORM clear_entrega USING p_delivery p_vbeln_factura CHANGING p_return TYPE bapiret2_t.
  DATA delivery LIKE  bapiobdlvhdrchg-deliv_numb.
  DATA header_data LIKE      bapiobdlvhdrchg.
  DATA header_control LIKE   bapiobdlvhdrctrlchg.
  DATA item_data TYPE bapiobdlvitemchg OCCURS 1 WITH HEADER LINE.
  DATA item_control TYPE bapiobdlvitemctrlchg OCCURS 1 WITH HEADER LINE.
  DATA return TYPE bapiret2 OCCURS 1 WITH HEADER LINE.
  DATA ti_lips LIKE lips OCCURS 1 WITH HEADER LINE.

  delivery = p_delivery.

*header_control
  header_control-deliv_numb  = p_delivery.
  header_data-deliv_numb  = p_delivery.

  REFRESH ti_lips.

  PERFORM buscar_lote USING p_vbeln_factura ti_lips CHANGING gt_lote[].
  LOOP AT gt_lote TRANSPORTING NO FIELDS WHERE charg IS NOT INITIAL.
  ENDLOOP.
  IF sy-subrc <> 0.
* sin lotes, nada que hacer
    return-type = 'S'.
    return-id = 'Z'.
    return-number = '999'.
    APPEND return TO p_return.
    RETURN.
  ENDIF.

  SELECT * FROM lips INTO TABLE ti_lips WHERE vbeln EQ p_delivery.

  CHECK ti_lips[] IS NOT INITIAL.

  DATA f_lote(1).

  CLEAR f_lote.

* verificar si entrega generada debería tener lotes en base a entrega de salida
* original
  LOOP  AT ti_lips WHERE charg IS INITIAL AND posnr < 900000.
* verificar posicion tiene Part.lotes en entrega de salida original
    IF line_exists( gt_lote[ uecha = ti_lips-posnr ] ).
      f_lote = 'X'.
    ELSE.
* verificar posicion tiene lotes en entrega de salida original
      LOOP AT gt_lote TRANSPORTING NO FIELDS WHERE posnn = ti_lips-posnr
                                               AND charg <> ''.
      ENDLOOP.
      IF sy-subrc = 0.
        f_lote = 'X'.
      ENDIF.
    ENDIF.
  ENDLOOP.

  IF f_lote <> 'X'.
* entrega generada ya tiene lotes, nada que hacer
    return-type = 'S'.
    return-id = 'Z'.
    return-number = '999'.
    APPEND return TO p_return.
    RETURN.
  ENDIF.

  LOOP AT ti_lips WHERE charg IS INITIAL AND posnr < 900000.
*item_data

*** buscar si tiene particion de lotes
    LOOP AT gt_lote INTO DATA(ls_lote) WHERE uecha = ti_lips-posnr.
      item_data-deliv_numb      = ti_lips-vbeln.
      item_data-deliv_item      = ls_lote-posnn.
      item_data-fact_unit_nom = ls_lote-umvkz .
      item_data-fact_unit_denom = ls_lote-umvkn.
      item_data-conv_fact = ls_lote-umrev.
      item_data-dlv_qty         = ls_lote-lfimg.
      item_data-dlv_qty_imunit  = ls_lote-lgmng .
      item_data-batch = ls_lote-charg.
      item_data-hieraritem = ti_lips-posnr. "The batch split record below delivery item hierary
      item_data-usehieritm = '1'.

      APPEND item_data.
      CLEAR item_data.

      item_control-deliv_numb  = ti_lips-vbeln.
      item_control-deliv_item  = ls_lote-posnn.
      item_control-chg_delqty   = 'X'.
      APPEND item_control.
      CLEAR item_control.
    ENDLOOP.
    IF sy-subrc <> 0.
      item_data-dlv_qty         = ti_lips-lfimg.
      item_data-dlv_qty_imunit  = ti_lips-lgmng .
    ENDIF.

    item_data-deliv_numb      = ti_lips-vbeln.
    item_data-deliv_item      = ti_lips-posnr.
    item_data-fact_unit_nom = ti_lips-umvkz .
    item_data-fact_unit_denom = ti_lips-umvkn.
    item_data-conv_fact = ti_lips-umrev.

    READ TABLE gt_lote INTO ls_lote WITH KEY posnn = ti_lips-posnr.
    item_data-batch = ls_lote-charg.
*  PERFORM buscar_lote USING p_vbeln_factura ti_lips CHANGING ITEM_DATA-batch.

    APPEND item_data.
    CLEAR item_data.

*item_control
    item_control-deliv_numb  = ti_lips-vbeln.
    item_control-deliv_item  = ti_lips-posnr.
    item_control-chg_delqty   = 'X'.
    APPEND item_control.
    CLEAR item_control.

  ENDLOOP.

  CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE'
    EXPORTING
      header_data    = header_data
      header_control = header_control
      delivery       = delivery
    TABLES
      item_data      = item_data
      item_control   = item_control
      return         = p_return
    EXCEPTIONS
      OTHERS         = 1.

ENDFORM.                    " CLEAR_ENTREGA

Crear pedido con referencia VA01

FUNCTION z_sd_crea_pedido_con_ref_fact.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     REFERENCE(I_VBELN) TYPE  VBRK-VBELN
*"     REFERENCE(I_AUART) TYPE  VBAK-AUART
*"     REFERENCE(I_SAVE) TYPE  XFELD DEFAULT 'X'
*"     REFERENCE(I_AUGRU) TYPE  VBAK-AUGRU
*"  EXPORTING
*"     REFERENCE(E_VBELN) TYPE  VBAK-VBELN
*"     REFERENCE(E_VBELN_ENTREGA) TYPE  LIKP-VBELN
*"     REFERENCE(E_RETURN) TYPE  BAPIRET2_T
*"----------------------------------------------------------------------
  DATA ld_mode(1) VALUE 'N'. " 'N'.

  DATA lt_mtxt LIKE TABLE OF bdcmsgcoll WITH HEADER LINE.
  DATA ls_return TYPE bapiret2.

  CLEAR: e_return[],
         bdcdata[],
         lt_mtxt[],
         e_vbeln.

  IF i_save <> 'X'.
    ld_mode = 'E'.
  ENDIF.

  SELECT SINGLE vkbur INTO @DATA(l_vkbur) FROM vbrp WHERE vbeln EQ @i_vbeln.


  PERFORM bdc_dynpro      USING 'SAPMV45A' '0101'.
  PERFORM bdc_field       USING 'BDC_OKCODE'
                                '=COPY'.
  PERFORM bdc_field       USING 'VBAK-AUART'
                                 i_auart.
  PERFORM bdc_field       USING 'VBAK-VKORG'
                                ''.
  PERFORM bdc_field       USING 'VBAK-VTWEG'
                                ''.
  PERFORM bdc_field       USING 'VBAK-SPART'
                                 ''.
  PERFORM bdc_field       USING 'VBAK-VKBUR'
                                 l_vkbur.

  PERFORM bdc_dynpro      USING 'SAPLV45C' '0100'.
  PERFORM bdc_field       USING 'BDC_OKCODE'
                                '=UEBR'.
  PERFORM bdc_field       USING 'VBRK-VBELN'
                                 i_vbeln.

  IF i_save = 'X'.
*    PERFORM bdc_dynpro      USING 'SAPMV45A' '4001'.
*    PERFORM bdc_field       USING 'BDC_OKCODE'
*                                   '/00'.

    PERFORM bdc_dynpro      USING 'SAPMV45A' '4001'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=HEAD'.
    if i_augru is NOT INITIAL.
    PERFORM bdc_field       USING 'VBAK-AUGRU'
                                  i_augru.
    endif.

    PERFORM bdc_dynpro      USING 'SAPMV45A' '4002'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=T\10'.

    PERFORM bdc_dynpro      USING 'SAPMV45A' '4002'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=SICH'.
    PERFORM bdc_field       USING 'VBKD-BSARK'
                                  '1'.
  ENDIF.

  CALL TRANSACTION 'VA01'
  USING bdcdata
        MODE  ld_mode
        UPDATE 'S'
        MESSAGES INTO lt_mtxt.

  IF i_auart = 'ZD02'.
* &1 &2 se ha grabado (se ha creado entrega &3)
    READ TABLE lt_mtxt WITH KEY msgnr = '260'
                                msgid = 'V1'.
  ELSE.
* &1 &2 se ha grabado
    READ TABLE lt_mtxt WITH KEY msgnr = '311'
                              msgid = 'V1'.
  ENDIF.
  IF sy-subrc EQ 0.
    e_vbeln = lt_mtxt-msgv2.
    e_vbeln_entrega = lt_mtxt-msgv3.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = e_vbeln
      IMPORTING
        output = e_vbeln.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = e_vbeln_entrega
      IMPORTING
        output = e_vbeln_entrega.

    MOVE lt_mtxt-msgid TO ls_return-id.
    MOVE lt_mtxt-msgtyp TO ls_return-type.
    MOVE lt_mtxt-msgnr TO ls_return-number.
    MOVE lt_mtxt-msgv1  TO ls_return-message_v1.
    MOVE lt_mtxt-msgv2  TO ls_return-message_v2.
    MOVE lt_mtxt-msgv3  TO ls_return-message_v3.
    MOVE lt_mtxt-msgv4  TO ls_return-message_v4.

    MESSAGE ID lt_mtxt-msgid TYPE lt_mtxt-msgtyp NUMBER lt_mtxt-msgnr
    WITH lt_mtxt-msgv1 lt_mtxt-msgv2 lt_mtxt-msgv3 lt_mtxt-msgv4
    INTO ls_return-message.

    APPEND ls_return TO e_return.
  ELSE.

    LOOP AT lt_mtxt.
      MOVE lt_mtxt-msgid TO ls_return-id.
      MOVE lt_mtxt-msgtyp TO ls_return-type.
      MOVE lt_mtxt-msgnr TO ls_return-number.
      MOVE lt_mtxt-msgv1  TO ls_return-message_v1.
      MOVE lt_mtxt-msgv2  TO ls_return-message_v2.
      MOVE lt_mtxt-msgv3  TO ls_return-message_v3.
      MOVE lt_mtxt-msgv4  TO ls_return-message_v4.

      MESSAGE ID lt_mtxt-msgid TYPE lt_mtxt-msgtyp NUMBER lt_mtxt-msgnr
      WITH lt_mtxt-msgv1 lt_mtxt-msgv2 lt_mtxt-msgv3 lt_mtxt-msgv4
      INTO ls_return-message.

      APPEND ls_return TO e_return.
    ENDLOOP.
  ENDIF.

ENDFUNCTION.

*       Batchinputdata of single transaction
DATA:   BDCDATA LIKE BDCDATA    OCCURS 0 WITH HEADER LINE.
*       Nodata-Character
DATA:   NODATA_CHARACTER VALUE '/'.

*----------------------------------------------------------------------*
*        Start new screen                                              *
*----------------------------------------------------------------------*
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
  CLEAR BDCDATA.
  BDCDATA-PROGRAM  = PROGRAM.
  BDCDATA-DYNPRO   = DYNPRO.
  BDCDATA-DYNBEGIN = 'X'.
  APPEND BDCDATA.
ENDFORM.

*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.
  IF fval <> NODATA_CHARACTER.
    CLEAR BDCDATA.
    BDCDATA-FNAM = FNAM.
    BDCDATA-FVAL = FVAL.
    APPEND BDCDATA.
  ENDIF.
ENDFORM.

Crear entrega con referencia VL01N

FORM crear_entrega_2 USING i_vbeln.  
 DATA: lt_request like table of bapideliciousrequest with header line,
       lt_items like table of bapideliciouscreateditems with header line,
       lt_return like table of bapiret2 with header line,
       lt_vbap LIKE TABLE OF vbap WITH HEADER LINE,
       ls_vbak LIKE vbak.

 SELECT SINGLE * INTO ls_vbak FROM vbak
                            WHERE vbeln = i_vbeln.
 CHECK sy-subrc EQ 0.

 SELECT * INTO TABLE lt_vbap FROM vbap
                            WHERE vbeln = i_vbeln.

 LOOP AT lt_vbap.
   CLEAR lt_request.
   lt_request-document_numb = lt_vbap-vbeln.
   lt_request-document_item = lt_vbap-posnr.
   lt_request-ship_to = ls_vbak-kunnr.
   lt_request-sold_to = ls_vbak-kunnr.
   lt_request-sales_organisation = ls_vbak-vkorg.
   lt_request-distribution_channel = ls_vbak-vtweg.
   lt_request-division = ls_vbak-spart.
   lt_request-plant = lt_vbap-werks.
   lt_request-quantity_sales_uom = lt_vbap-KWMENG.
   lt_request-quantity_base__uom = lt_vbap-KWMENG.
   lt_request-sales_unit = lt_vbap-vrkme.
   lt_request-base_uom = lt_vbap-meins.
   lt_request-material = lt_vbap-matnr.
   lt_request-delivery_date = ls_vbak-bstdk.
   lt_request-transp_plan_date = ls_vbak-bstdk.
   lt_request-loading_date = ls_vbak-bstdk.
   lt_request-goods_issue_date = ls_vbak-bstdk.
   lt_request-document_type = 'A'. "Delivery
   lt_request-document_type_predecessor = 'A'. "Sales Ord
   lt_request-document_type_delivery = 'LF'.

   APPEND lt_request.
 ENDLOOP.

 CALL FUNCTION 'BAPI_DELIVERYPROCESSING_EXEC'
   TABLES
     request      = lt_request
     createditems = lt_items
     return       = lt_return.

 CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
  EXPORTING
    wait          = 'X'
* IMPORTING
*   RETURN        =
           .
endform.

Crear Entrega Sin Referencia VL01NO

* BAPI for VL01NO
form DELIVERY_CREATENOREF using i_vbeln
                                i_werks
                                i_vstel
                                i_commit
                                i_dlv_items type any TABLE
                       changing o_vbeln
                                o_created_items type ANY TABLE
                                o_return type BAPIRET2_T
                                of_rc type sysubrc.
 data: ls_likp type likp,
       lt_lips type table of lips,
       ls_lips like line of lt_lips,
       ls_t001w like t001w.

 data: lt_dates type table of BAPIDLVDEADLN,
       ls_dates like line of lt_dates,
       lt_DLV_ITEMS type table of BAPIDLVNOREFITEM,
       ls_dlv_items like line of lt_DLV_ITEMS.

 data: ls_return type bapiret2,
       lt_return type table of bapiret2,
       ld_par1 type SY-MSGV1 ,
       ld_par2 type SY-MSGV1 .

 data: lt_CREATED_ITEMS like TABLE OF  BAPIDLVITEMCREATED,
       ls_created_items LIKE LINE OF lt_CREATED_ITEMS.
 data ld_vbtyv like lips-vbtyv.

 data: ld_prctr type prctr,
       ld_kostl like zsdceco_centro-kostl,
       ld_werks like lips-werks,
       ld_werks_o like lips-werks.

 clear of_rc.
 clear o_vbeln.

 select single * into ls_likp from likp
                             where vbeln = i_vbeln.
 if sy-subrc ne 0.
* error entrega no existe
   move i_vbeln to ld_par1.

   CALL FUNCTION 'BALW_BAPIRETURN_GET2'
     EXPORTING
       TYPE   = 'E'
       CL     = 'VL'
       NUMBER = 559
       PAR1   = ld_par1
     IMPORTING
       RETURN = ls_return.

   append ls_return to o_return.
   of_rc = 1.
   return.
 endif.

 select * into table lt_lips from lips
                            where vbeln = i_vbeln.
***
 select single * into ls_t001w from T001W where WERKS = I_WERKS.

***
 ls_dates-timetype = 'WSHDRWADAT'. "WS GOODS ISSUE

 data ls_time(14).
 concatenate sy-datlo sy-timlo into ls_time.

 move ls_time to ls_dates-timestamp_utc.
 ls_dates-timezone = 'CHILE'.

 append ls_dates to lt_dates.

 if i_dlv_items[] is not initial.
* si las posiciones son entregadas=>tomar, si no,
* tomar todas las posiciones de la entrega
   FIELD-SYMBOLS <wa> type any.

   loop at i_dlv_items ASSIGNING <wa>.
     MOVE-CORRESPONDING <wa> to ls_dlv_items.
     append ls_dlv_items to lt_DLV_ITEMS.
   ENDLOOP.
 else.
   loop at lt_lips into ls_lips.
*DATES -TIMESTAMP_UTC = SY-UNAME
     ls_DLV_ITEMS-REF_ITem = ls_LIPS-POSNR.
     ls_DLV_ITEMS-MATERIAL  = ls_LIPS-MATNR.
     ls_DLV_ITEMS-ITEM_categ = 'ZREF'.
     ls_DLV_ITEMS-DLV_QTY = ls_LIPS-LFIMG.
     ls_DLV_ITEMS-sales_unit = ls_lips-meins.
*    ls_DLV_ITEMS-plant = i_werks.

     append ls_DLV_ITEMS to lt_DLV_ITEMS.
   endloop.
 endif.

*  select single * into ls_t001w from T001W where WERKS = i_WERKS.
*  data ld_ship_to type KUNWE.
*  ld_ship_to = i_werks.

 CALL FUNCTION 'BAPI_OUTB_DELIVERY_CREATENOREF'
   EXPORTING
     SHIP_POINT           = i_vstel "ls_LIKP-VSTEL
     DLV_TYPE             = 'ZREF'
     SALESORG             = ls_t001w-vkorg
     DISTR_CHAN           = ls_t001w-vtweg
     DIVISION             = ls_t001w-spart
     SHIP_TO              = ls_t001w-kunnr
    DATE_USAGE           = '0'
*     DEBUG_FLG            =
  IMPORTING
    DELIVERY             = o_vbeln
*     NUM_DELIVERIES       =
   TABLES
     DATES                = lt_dates
     DLV_ITEMS            = lt_dlv_items
*     SERIAL_NUMBERS       =
*     EXTENSION_IN         =
*     DELIVERIES           =
     CREATED_ITEMS        = lt_created_items
*     EXTENSION_OUT        =
     RETURN               = o_return
           .
 o_created_items[] = lt_created_items[].

 if o_vbeln is not initial
   and i_commit is not initial.

   CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
     EXPORTING
       WAIT   = 'X'
     IMPORTING
       RETURN = ls_return.

   if ls_return is INITIAL.
* actualizar entrega creada(LIPS) con documento y pos de referencia y KOSTL
     loop at lt_CREATED_ITEMS into ls_CREATED_ITEMS .
       select single vbtyp into ld_vbtyv from likp
                            where vbeln = i_vbeln. "entrega entrada
*                              and posnr  = ls_created_items-REF_ITem.

* obtiene centro de entrega salida creada
       select single werks into ld_werks from lips
                                  where vbeln = o_vbeln "entrega  salida creada
                                    and posnr = ls_created_items-DELIV_ITEM.

       select single prctr into ld_prctr
                     from marc where matnr = ls_CREATED_ITEMS-material
                                and werks  = ld_werks.
       if sy-subrc = 0.
* obtiene centro de entrega entrada
         select single werks into ld_werks_o from lips
                          where vbeln = i_vbeln "entrega entrada
                            and posnr = ls_created_items-REF_ITEM.

         select single kostl into ld_kostl
                        from zsdceco_centro
                             where werks = ld_werks_o
                               and prctr = ld_prctr.
         if sy-subrc ne 0.
           clear ld_kostl.
         endif.
       endif.

       UPDATE lips
              set kostl   = ld_kostl
                  zzvbeln = i_vbeln
                  zzposnr = ls_created_items-REF_ITem
                  zzvbtyv = ld_vbtyv
        where vbeln = o_vbeln
           and posnr = ls_created_items-deliv_item.

     endloop.
     commit WORK AND WAIT.
   endif.

 endif.
endform.                    "DELIVERY_CREATENOREF

Crear Contacto

FORM crea_contacto USING p_vbeln
                CHANGING p_subrc.

  DATA: vl_vbeln LIKE vbak-vbeln.

  DATA: lt_salesactivityidout LIKE bapi_vbka_boidref OCCURS 0 WITH HEADER LINE,
        lt_return             LIKE bapiret2          OCCURS 0,
        ls_return             LIKE LINE OF lt_return.

* BAPI_BPCONTACT_CREATEFROMDATA Declarations
  DATA: lv_sys                   TYPE bapi_sender,
        lt_generaldata           LIKE bapi_bus1037_vbkakom_cr OCCURS 0,
        ls_generaldata           LIKE LINE OF lt_generaldata,
        lt_businesspartner       LIKE bapi_vbka_vbpa2kom      OCCURS 0,
        ls_businesspartner       LIKE LINE OF lt_businesspartner,
        lt_text                  LIKE bapi_vbka_tlinekom      OCCURS 0,
        ls_text                  LIKE LINE OF lt_text,
        lt_object_reference_list LIKE bapi_vbka_objref_cr OCCURS 0,
        ls_object_reference_list LIKE LINE OF lt_object_reference_list.
  DATA: ls_notes LIKE tline OCCURS 0 WITH HEADER LINE.
  DATA: lt_vbpa TYPE TABLE OF vbpa,
        ls_vbpa TYPE vbpa,
        ls_vbka TYPE vbka.

    DATA: t_vbka LIKE vbka,
          ld_atype(3) TYPE n,
          ld_date LIKE sy-datum,
          w_table(30).

  FIELD-SYMBOLS: <field>.

* Obtiene data necesaria
  vl_vbeln = p_vbeln.
  SELECT SINGLE * INTO ls_vbka FROM vbka WHERE vbeln = vl_vbeln.
  CHECK sy-subrc = 0.

  CLEAR: t_vbka.
  w_table = '(SAPLV43D)XVBKA'.
  ASSIGN (w_table) TO <field>.
  IF sy-subrc EQ 0.
    MOVE <field> TO t_vbka.
    ld_date = t_vbka-ktaen.
  ELSE.
*  ld_date = sy-datum .
    ld_date = ls_vbka-ktaen.
  ENDIF.

  DO 7 TIMES.
    ld_date = ld_date + 1.

    CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE'
      EXPORTING
        correct_option               = '+'
        date                         = ld_date
        factory_calendar_id          = 'CL'
      IMPORTING
        date                         = ld_date
      EXCEPTIONS
        calendar_buffer_not_loadable = 1
        correct_option_invalid       = 2
        date_after_range             = 3
        date_before_range            = 4
        date_invalid                 = 5
        factory_calendar_not_found   = 6
        OTHERS                       = 7.
    IF sy-subrc <> 0.
      CLEAR ld_date.
      EXIT.
    ENDIF.
  ENDDO.

  MOVE ls_vbka-ktaar+1(3) TO ld_atype.

  ADD 1 TO ld_atype.

  CONCATENATE 'Z' ld_atype INTO   ls_generaldata-activity_type .

  ls_generaldata-doc_number       = ls_vbka-vbeln.
  ls_generaldata-salesorg         = ls_vbka-vkorg.
  ls_generaldata-distr_chan       = ls_vbka-vtweg.
  ls_generaldata-sales_off        = ls_vbka-vkbur.
  ls_generaldata-sales_grp        = ls_vbka-vkgrp.
  ls_generaldata-division         = ls_vbka-spart.
  ls_generaldata-from_date        = ld_date.
  ls_generaldata-to_date          = ld_date.
  ls_generaldata-from_time        = sy-uzeit.
  ls_generaldata-to_time          = sy-uzeit.
  ls_generaldata-follow_up_date   = ld_date.
  ls_generaldata-reason           = 'Z01'.
  ls_generaldata-result           = ''.
  ls_generaldata-direction        = '1'.
  ls_generaldata-state            = '0'.
  ls_generaldata-activity_comment = ls_vbka-ktext. "ls_VBKA-VBELN.
  ls_generaldata-partn_role       = 'AG'.
  ls_generaldata-partn_id         = ls_vbka-kunnr.
  ls_generaldata-langu            = 'D'.

  IF ls_vbka-ktast EQ '7' AND ls_vbka-ktaer EQ 'Z02'.
    ls_generaldata-activity_type    = 'Z010'.
    ls_generaldata-reason           = 'Z02'.
  ENDIF.

  SELECT * INTO TABLE lt_vbpa FROM vbpa
                             WHERE vbeln = vl_vbeln.
  CLEAR ls_vbpa.
  READ TABLE lt_vbpa INTO ls_vbpa WITH KEY parvw = 'AP'.
  IF sy-subrc = 0.
** Contact
    ls_generaldata-contact          = ls_vbpa-parnr.
    ls_generaldata-contact_role     = 'AP'.

    ls_businesspartner-doc_number   = vl_vbeln.
    ls_businesspartner-itm_number   = '000000'.
    ls_businesspartner-partn_role   = 'AP'.  "GERMAN = AP ENGLISH = CP
    ls_businesspartner-partn_id     = ls_vbpa-parnr.
    APPEND ls_businesspartner TO lt_businesspartner.
  ENDIF.

  CLEAR ls_vbpa.
  READ TABLE lt_vbpa INTO ls_vbpa  WITH KEY parvw = 'AG'.
* Sold to party
  ls_businesspartner-doc_number   = vl_vbeln.
  ls_businesspartner-itm_number   = '000000'.
  ls_businesspartner-partn_role   = 'AG'. "GERMAN = AG ENGLISH = SP
  ls_businesspartner-partn_id     = ls_vbpa-kunnr.
  APPEND ls_businesspartner TO lt_businesspartner.

  CLEAR ls_vbpa.
  READ TABLE lt_vbpa INTO ls_vbpa  WITH KEY parvw = 'VE'.
* Sold to party
  ls_businesspartner-doc_number   = vl_vbeln.
  ls_businesspartner-itm_number   = '000000'.
  ls_businesspartner-partn_role   = 'VE'. "GERMAN = AG ENGLISH = SP
  ls_businesspartner-partn_id     = ls_vbpa-pernr.
  APPEND ls_businesspartner TO lt_businesspartner.

  CLEAR ls_vbpa.
  READ TABLE lt_vbpa INTO ls_vbpa  WITH KEY parvw = 'AP'.
* Sold to party
  ls_businesspartner-doc_number   = vl_vbeln.
  ls_businesspartner-itm_number   = '000000'.
  ls_businesspartner-partn_role   = 'AP'. "GERMAN = AG ENGLISH = SP
  ls_businesspartner-partn_id     = ls_vbpa-pernr.
  APPEND ls_businesspartner TO lt_businesspartner.

  CLEAR ls_vbpa.
  READ TABLE lt_vbpa INTO ls_vbpa WITH KEY parvw = 'ZM'.
* Employee Responsible
  ls_businesspartner-doc_number   = vl_vbeln.
  ls_businesspartner-itm_number   = '000000'.
  ls_businesspartner-partn_role   = 'ZM'.  "GERMAN = ZM ENGLISH = ER
  ls_businesspartner-partn_id     = ls_vbpa-pernr.
  APPEND ls_businesspartner TO lt_businesspartner.

* Object Reference
  ls_object_reference_list-doc_number = vl_vbeln.
  ls_object_reference_list-roletype_ab = 'A'.
  APPEND ls_object_reference_list TO lt_object_reference_list.

  APPEND ls_generaldata TO lt_generaldata.

  ls_notes-tdformat = '*'.
  ls_notes-tdline   = 'Copy of signed agreement'.

  ls_text-text_id   = '0001'.
  ls_text-function  = '009'.
  ls_text-langu     =  sy-langu.
  ls_text-text_line =  ls_notes.

  APPEND ls_text TO lt_text.

  CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'
    IMPORTING
      own_logical_system             = lv_sys
    EXCEPTIONS
      own_logical_system_not_defined = 1
      OTHERS                         = 2.

*Call BAPI
  CALL FUNCTION 'BAPI_BPCONTACT_CREATEFROMDATA'
    EXPORTING
      sender          = lv_sys
    TABLES
      generaldata     = lt_generaldata
      businesspartner = lt_businesspartner
      text            = lt_text
*     object_reference_list = lt_object_reference_list
      return          = lt_return.

  IF sy-subrc EQ 0.
    REFRESH lt_return.
    CALL FUNCTION 'BAPI_BPCONTACT_SAVEFROMMEMORY'
      TABLES
        salesactivityidout = lt_salesactivityidout
        return             = lt_return.
*error
    LOOP AT lt_return INTO ls_return WHERE type = 'E'
                  OR type = 'A'.
    ENDLOOP.
    IF sy-subrc = 0.
      RETURN.
    ENDIF.

    DATA lt_vbfa TYPE TABLE OF vbfa WITH HEADER LINE.

    SELECT SINGLE vbtyp INTO @DATA(ld_vbtyp)
                  FROM vbak WHERE vbeln = @vl_vbeln.
    LOOP AT lt_salesactivityidout.
      lt_vbfa-vbelv   = vl_vbeln.
      lt_vbfa-vbeln   = lt_salesactivityidout-doc_number.
      lt_vbfa-vbtyp_n = '1'.
      lt_vbfa-vbtyp_v = ld_vbtyp. "'L'.
      lt_vbfa-erdat   = sy-datum.
      lt_vbfa-erzet   = sy-uzeit.
      APPEND lt_vbfa.
    ENDLOOP.
    LOOP AT lt_vbfa.
      MODIFY vbfa FROM lt_vbfa.
    ENDLOOP.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
  ENDIF.

ENDFORM.

Crear/Modificar deudor XD01/XD02

Crear

FORM create.
  DATA: ls_masterdata            TYPE cmds_ei_main,
        ls_customers             TYPE  cmds_ei_extern,
        lt_customers             TYPE cmds_ei_extern_t,
* for Exportparameters of Methode
        ls_master_data_correct   TYPE  cmds_ei_main,
        ls_message_correct       TYPE  cvis_message,
        ls_master_data_defective TYPE  cmds_ei_main,
        ls_message_defective     TYPE  cvis_message,
* for Transfer Communication data
        ls_phone_main            TYPE cvis_ei_cvi_phone,
        ls_phone                 TYPE cvis_ei_phone_str,
        lt_phone                 TYPE cvis_ei_phone_t,
        ls_fax_main              TYPE cvis_ei_cvi_fax,
        ls_fax                   TYPE cvis_ei_fax_str,
        lt_fax                   TYPE cvis_ei_fax_t,
        ls_mail_main             TYPE cvis_ei_cvi_smtp,
        ls_mail                  TYPE cvis_ei_smtp_str,
        lt_mail                  TYPE cvis_ei_smtp_t.

* Customer number (in case of external creating of numbers)
*  ls_customers-header-object_instance-kunnr = '0022281958'.   "!!!!!!!!!!!!!!!!
* Mark CREATE
  ls_customers-header-object_task = 'I'.
* Fill central data like Customer Account Group
  ls_customers-central_data-central-data-ktokd = 'CLNA'.
  ls_customers-central_data-central-data-kukla = '01'.
  ls_customers-central_data-central-data-stcd1 = '22281958-K'.
  ls_customers-central_data-central-data-gform = '02'.

  ls_customers-central_data-central-datax-ktokd = 'X'.
  ls_customers-central_data-central-datax-kukla = 'X'.
  ls_customers-central_data-central-datax-stcd1 = 'X'.
  ls_customers-central_data-central-datax-gform = 'X'.
* now address data
  ls_customers-central_data-address-task = 'I'.
* if the FROM date should be filled here , it has also to be filled in all other used structures
  ls_customers-central_data-address-postal-data-from_date = sy-datum.
* NAME and SORT1 are Mandatory fields
  ls_customers-central_data-address-postal-data-name = 'pablo'.
  ls_customers-central_data-address-postal-data-sort1  = 'pablo'.
  ls_customers-central_data-address-postal-data-name_2 = 'tapia'.
  ls_customers-central_data-address-postal-data-city  = '2'.            "city1
  ls_customers-central_data-address-postal-data-postl_cod1  = '.'.
  ls_customers-central_data-address-postal-data-street = '2'.
  ls_customers-central_data-address-postal-data-country = 'CL'.
  ls_customers-central_data-address-postal-data-langu = 'S'.
  ls_customers-central_data-address-postal-data-title = '0002'.
  ls_customers-central_data-address-postal-data-district = 'santiago'.  "city2
  ls_customers-central_data-address-postal-data-region = '13'.          "regio
  ls_customers-central_data-address-postal-data-transpzone = 'Z1340'.
* and now mark all used fields
  ls_customers-central_data-address-postal-datax-from_date = 'X'.
  ls_customers-central_data-address-postal-datax-to_date = 'X'.
  ls_customers-central_data-address-postal-datax-name = 'X'.
  ls_customers-central_data-address-postal-datax-sort1 = 'X'.
  ls_customers-central_data-address-postal-datax-name_2 = 'X'.
  ls_customers-central_data-address-postal-datax-city = 'X'.
  ls_customers-central_data-address-postal-datax-postl_cod1 = 'X'.
  ls_customers-central_data-address-postal-datax-street = 'X'.
  ls_customers-central_data-address-postal-datax-house_no = 'X'.
  ls_customers-central_data-address-postal-datax-country = 'X'.
  ls_customers-central_data-address-postal-datax-langu = 'X'.
  ls_customers-central_data-address-postal-datax-title = 'X'.
  ls_customers-central_data-address-postal-datax-district = 'X'.
  ls_customers-central_data-address-postal-datax-region = 'X'.
  ls_customers-central_data-address-postal-datax-transpzone = 'X'.

* fill telephon data
  ls_phone-contact-task = 'I'.
  ls_phone-contact-data-telephone = '69792'.
  ls_phone-contact-data-country = 'CL'.
  ls_phone-contact-data-std_no = 'X'.
  ls_phone-contact-data-r_3_user = '1'.
  ls_phone-contact-data-home_flag = 'X'.
  ls_phone-contact-data-caller_no	= '69792'.
  ls_phone-contact-data-consnumber = '001'.

  ls_phone-contact-datax-country = 'X'.
  ls_phone-contact-datax-telephone = 'X'.
  ls_phone-contact-datax-std_no = 'X'.
  ls_phone-contact-datax-r_3_user = 'X'.
  ls_phone-contact-datax-home_flag = 'X'.
  ls_phone-contact-datax-caller_no  = 'X'.
  ls_phone-contact-datax-consnumber = 'X'.
  APPEND ls_phone TO lt_phone.

  ls_phone-contact-task = 'I'.
  ls_phone-contact-data-telephone = '9843'.
  ls_phone-contact-data-country = 'CL'.
  ls_phone-contact-data-std_no = ''.
  ls_phone-contact-data-r_3_user = '3'.
  ls_phone-contact-data-home_flag = ''.
  ls_phone-contact-data-caller_no	= '9843'.
  ls_phone-contact-data-consnumber = '002'.

  ls_phone-contact-datax-country = 'X'.
  ls_phone-contact-datax-telephone = 'X'.
  ls_phone-contact-datax-std_no = 'X'.
  ls_phone-contact-datax-r_3_user = 'X'.
  ls_phone-contact-datax-home_flag = 'X'.
  ls_phone-contact-datax-caller_no  = 'X'.
  ls_phone-contact-datax-consnumber = 'X'.
  APPEND ls_phone TO lt_phone.

  ls_phone_main-phone = lt_phone.

* fill fax data
  ls_fax-contact-task = 'I'.
  ls_fax-contact-data-fax = '84380'.
  ls_fax-contact-data-country = 'CL'.
  ls_fax-contact-data-home_flag = 'X'.
  ls_fax-contact-data-consnumber = '001'.
  ls_fax-contact-data-std_no = 'X'.
  ls_fax-contact-data-sender_no = '84380'.
  ls_fax-contact-data-std_no  = 'X'.


  ls_fax-contact-datax-fax = 'X'.
  ls_fax-contact-datax-country = 'X'.
  ls_fax-contact-datax-home_flag = 'X'.
  ls_fax-contact-datax-consnumber = 'X'.
  ls_fax-contact-datax-std_no = 'X'.
  ls_fax-contact-datax-sender_no = 'X'.
  ls_fax-contact-datax-std_no  = 'X'.
  APPEND ls_fax TO lt_fax.
  ls_fax_main-fax = lt_fax.
* fill mail data
  ls_mail-contact-task = 'I'.
  ls_mail-contact-data-e_mail = 'pepe@gmail.com'.
  ls_mail-contact-datax-e_mail = 'X'.
  APPEND ls_mail TO lt_mail.
  ls_mail_main-smtp = lt_mail.

  ls_customers-central_data-address-communication-smtp = ls_mail_main.
  ls_customers-central_data-address-communication-phone = ls_phone_main .
  ls_customers-central_data-address-communication-fax = ls_fax_main.

  APPEND ls_customers TO lt_customers.
  ls_masterdata-customers = lt_customers.

  BREAK-POINT.
  cmd_ei_api=>maintain_bapi(
  EXPORTING
*iv_test_run              = 'X'
      iv_collect_messages      = 'X'
  is_master_data           = ls_masterdata
  IMPORTING
  es_master_data_correct   = ls_master_data_correct
  es_message_correct       = ls_message_correct
  es_master_data_defective = ls_master_data_defective
  es_message_defective     = ls_message_defective
  ).
* to start booking
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' .
  BREAK-POINT.
ENDFORM.

Modificar

FORM update_customer USING i_kunnr
                           i_city
                           i_street
                           i_tel
                           i_mail
                           i_test.
  DATA: ls_masterdata            TYPE cmds_ei_main,
        ls_customers             TYPE  cmds_ei_extern,
        lt_customers             TYPE cmds_ei_extern_t,
* for Exportparameters of Methode
        ls_master_data_correct   TYPE  cmds_ei_main,
        ls_message_correct       TYPE  cvis_message,
        ls_master_data_defective TYPE  cmds_ei_main,
        ls_message_defective     TYPE  cvis_message,
* for Transfer Communication data
        ls_phone_main            TYPE cvis_ei_cvi_phone,
        ls_phone                 TYPE cvis_ei_phone_str,
        lt_phone                 TYPE cvis_ei_phone_t,
        ls_fax_main              TYPE cvis_ei_cvi_fax,
        ls_fax                   TYPE cvis_ei_fax_str,
        lt_fax                   TYPE cvis_ei_fax_t,
        ls_mail_main             TYPE cvis_ei_cvi_smtp,
        ls_mail                  TYPE cvis_ei_smtp_str,
        lt_mail                  TYPE cvis_ei_smtp_t.

* Customer number (in case of external creating of numbers)
  ls_customers-header-object_instance-kunnr = i_kunnr.
* Mark CREATE
  ls_customers-header-object_task = 'U'.

* now address data
  ls_customers-central_data-address-task = 'U'.

  ls_customers-central_data-address-postal-data-city  = i_city. "city1
  ls_customers-central_data-address-postal-data-street = i_street.

* and now mark all used fields

  ls_customers-central_data-address-postal-datax-city = 'X'.
  ls_customers-central_data-address-postal-datax-street = 'X'.

* fill telephon data
  ls_phone-contact-task = 'U'.
  ls_phone-contact-data-telephone = i_tel.
  ls_phone-contact-data-country = 'CL'.
  ls_phone-contact-data-std_no = 'X'.
  ls_phone-contact-data-r_3_user = '1'.
  ls_phone-contact-data-home_flag = 'X'.
  ls_phone-contact-data-caller_no	= i_tel.
*ls_phone-contact-data-CONSNUMBER = '001'.

  ls_phone-contact-datax-country = 'X'.
  ls_phone-contact-datax-telephone = 'X'.
  ls_phone-contact-datax-std_no = 'X'.
  ls_phone-contact-datax-r_3_user = 'X'.
  ls_phone-contact-datax-home_flag = 'X'.
  ls_phone-contact-datax-caller_no  = 'X'.
*ls_phone-contact-datax-CONSNUMBER = 'X'.

  APPEND ls_phone TO lt_phone.
  ls_phone_main-phone = lt_phone.

* fill mail data
  ls_mail-contact-task = 'U'.
  ls_mail-contact-data-e_mail = i_mail.
  ls_mail-contact-datax-e_mail = 'X'.
  APPEND ls_mail TO lt_mail.
  ls_mail_main-smtp = lt_mail.

  ls_customers-central_data-address-communication-smtp = ls_mail_main.
  ls_customers-central_data-address-communication-phone = ls_phone_main .

  APPEND ls_customers TO lt_customers.
  ls_masterdata-customers = lt_customers.

  cmd_ei_api=>maintain_bapi(
  EXPORTING
     iv_test_run              = i_test
      iv_collect_messages      = 'X'
  is_master_data           = ls_masterdata
  IMPORTING
  es_master_data_correct   = ls_master_data_correct
  es_message_correct       = ls_message_correct
  es_master_data_defective = ls_master_data_defective
  es_message_defective     = ls_message_defective
  ).

  DATA ls_message TYPE bapiret2.
  LOOP AT ls_message_defective-messages INTO ls_message.
  ENDLOOP.
  IF sy-subrc = 0.
    MESSAGE ID ls_message-id TYPE 'E' NUMBER ls_message-number
                  WITH ls_message-message_v1
                       ls_message-message_v2
                       ls_message-message_v3
                       ls_message-message_v4
                       RAISING error_xd02.
  ENDIF.

ENDFORM.                    "update_customer

Visualizar

FORM display USING p_kunnr TYPE kna1-kunnr.
  DATA: ls_customers_in  TYPE cmds_ei_main,
        ls_customers_out TYPE cmds_ei_main,
        ls_customer_in   TYPE cmds_ei_extern,
        ls_header_in     TYPE cmds_ei_header.

  ls_header_in-object_instance-kunnr = p_kunnr.
  ls_header_in-object_task = cvi_ei_api_map_structure=>gc_modify.
  ls_customer_in-header    = ls_header_in.
* Complete interface structure
  APPEND ls_customer_in TO ls_customers_in-customers.

  CALL METHOD cmd_ei_api_extract=>get_data
    EXPORTING
      is_master_data = ls_customers_in
    IMPORTING
      es_master_data = ls_customers_out.
  BREAK-POINT.
ENDFORM.

Obtener datos de pedido de venta

 DATA: ls_bapi_view            TYPE order_view,
        lt_salesdocuments       TYPE TABLE OF sales_key,
        lt_order_headers_out    TYPE TABLE OF bapisdhd,
        lt_order_items_out      TYPE TABLE OF bapisdit,
        lt_order_schedules_out  TYPE TABLE OF bapisdhedu,
        lt_order_conditions_out TYPE TABLE OF bapisdcond,
        lt_order_partners_out   TYPE TABLE OF bapisdpart,
        lt_order_business_out   type table of BAPISDBUSI.

  ls_bapi_view-header       = 'X'.
  ls_bapi_view-item         = 'X'.
  ls_bapi_view-sdschedule   = 'X'.
  ls_bapi_view-sdcond       = 'X'.
  ls_bapi_view-partner      = 'X'.
  ls_bapi_view-BUSINESS     = 'X'.

  clear lt_salesdocuments[].

  select single aubel into ls_salesdocuments
                           from vbrp where vbeln = i_vbeln.

  append ls_salesdocuments to lt_salesdocuments.

  CALL FUNCTION 'BAPISDORDER_GETDETAILEDLIST'
    EXPORTING
      i_bapi_view                   = ls_bapi_view
*   I_MEMORY_READ                 =
    TABLES
      sales_documents               = lt_salesdocuments
      order_headers_out             = lt_order_headers_out
      order_items_out               = lt_order_items_out
      order_schedules_out           = lt_order_schedules_out
      order_partners_out            = lt_order_partners_out
      order_conditions_out          = lt_order_conditions_out
      order_business_out            = lt_order_business_out

BAPI Transporte

Equivalencia entre tablas/campos SAP y campos de estructuras de BAPI en SD