SAP / ABAP

BAPI BAPI_SALESORDER_CHANGE

· atualizado em · BAPI_SALESORDER_CHANGEBAPI_SALESORDER_GETSTATUSBAPIVA02ABAP
Publicado originalmente em 2010. Este conteúdo pode se referir a versões antigas do SAP. Versão original: ABAP S4.
Revisão de 29 de novembro de 2026: Convertido para Markdown. Texto reescrito e acrescidos o resumo e o aviso sobre a macro m_insere_log e as tabelas Z. Código mantido como no original, sem renomear objetos; corrigido apenas um espaço antes do asterisco do comentário *-BAPI Sales Order change, que impediria a compilação.

Exemplo de como utilizar a BAPI BAPI_SALESORDER_CHANGE para retirar quantidade de um item de uma ordem de venda disponível na VA02. No trecho, a quantidade retirada de um item é somada ao item 10 da mesma ordem (l_posnr = 10). O status dos itens é lido antes com BAPI_SALESORDER_GETSTATUS.

É um trecho de um programa maior, então a cópia traz algumas partes que podem ser descartadas. Duas dependências ficam de fora: a macro m_insere_log, que grava o log de erro (defina-a ou troque as chamadas pela sua rotina de log), e as tabelas Z usadas nas declarações (ZQME_EST_PLACA e ZPPT_PROCESSCOME).

types:
  begin of ty_aufk,
    aufnr type aufk-aufnr,
    kdauf type aufk-kdauf,
  end of ty_aufk,
  begin of ty_aufk_aux,
    aufnr type zqme_est_placa-aufnr,
    charg type zqme_est_placa-charg,
    erfmg type zqme_est_placa-erfmg,
    kdauf type aufk-kdauf,
  end of ty_aufk_aux,

  begin of ty_mska,
    charg type mska-charg ,
    vbeln type mska-vbeln ,
    posnr type mska-posnr ,
    kalab type mska-kalab  ,
  end of ty_mska,

begin of ty_schedule_line,
  vbeln type vbep-vbeln,      "Sales Document
  posnr type vbep-posnr,      "Sales Document Item
  etenr type vbep-etenr,      "Delivery Schedule Line Number
  ettyp type vbep-ettyp,      "Schedule line category
  wmeng  type vbep-wmeng,
  vrkme  type vbep-vrkme,
  edatu  type vbep-edatu,
end of ty_schedule_line.

data: t_aufk         type standard table of ty_aufk,
      t_aufk_aux     type standard table of ty_aufk_aux,
      t_mska         type standard table of ty_mska,
      t_estpl_aux1   type standard table of zqme_est_placa,
      t_processcome  type standard table of zppt_processcome,
      t_schedule_line type standard table of ty_schedule_line,
      wa_processcome like line of t_processcome,
      wa_aufk        like line of t_aufk,
      wa_aufk_aux    like line of t_aufk_aux,
      wa_mska        like line of t_mska,
      wa_estlp_aux1  like line of t_estpl_aux1,
      wa_schedule_line like line of t_schedule_line.

*-BAPI Sales Order change
  data: t_order_item_in_bapi    type standard table of bapisditm,
        t_order_item_inx_bapi   type standard table of bapisditmx,
        t_schedule_lines_bapi   type standard table of bapischdl,
        t_schedule_linesx_bapi  type standard table of bapischdlx,
        t_return_so_change      type standard table of bapiret2,
        t_schedule_line_loc     type standard table of ty_schedule_line,

*-BAPI_SALESORDER_GETSTATUS
        l_return like  bapireturn,
        lt_statusinfo type standard table of bapisdstat,
        lwa_statusinfo like line of lt_statusinfo,

        wa_order_item_in_bapi    like line of t_order_item_in_bapi,
        wa_order_item_inx_bapi   like line of t_order_item_inx_bapi,
        wa_schedule_lines_bapi   like line of t_schedule_lines_bapi,
        wa_schedule_linesx_bapi  like line of t_schedule_linesx_bapi,
        wa_return_so_change      like line of t_return_so_change,
        wa_return_commit    type bapiret2,

        l_salesdocument     type bapivbeln-vbeln,
        wa_order_header_inx type bapisdh1x,
        l_posnr type mska-posnr,
        l_error_bapi        type i,
        l_message(80)    type c,
        l_erfmg          type erfmg.

  l_posnr = 10.

  loop at t_estpl_aux1 into wa_estlp_aux1.
    read table t_aufk into wa_aufk with key aufnr = wa_estlp_aux1-aufnr.
    if sy-subrc = 0.
      l_salesdocument = wa_aufk-kdauf.
      wa_order_header_inx-updateflag = 'U'.
      call function 'BAPI_SALESORDER_GETSTATUS'
        exporting
          salesdocument = l_salesdocument
        importing
          return        = l_return
        tables
          statusinfo    = lt_statusinfo.

      if not lt_statusinfo[] is initial.

        select vbeln posnr etenr ettyp wmeng vrkme edatu        "#CR
        into table t_schedule_line
        from vbep
        for all entries in lt_statusinfo
        where vbeln = l_salesdocument
        and posnr = lt_statusinfo-itm_number.
        if sy-subrc = 0.

          sort t_schedule_line by edatu descending posnr ascending.

          t_schedule_line_loc[] = t_schedule_line[].

          read table t_aufk_aux into wa_aufk_aux with key kdauf = wa_aufk-kdauf
                                                          aufnr  = wa_estlp_aux1-aufnr
                                                          charg  = wa_estlp_aux1-charg.
          if sy-subrc = 0.
            read table t_mska into wa_mska with key charg = wa_aufk_aux-charg
                                                    vbeln = wa_aufk_aux-kdauf.
            if sy-subrc = 0.
              wa_order_item_in_bapi-itm_number  = wa_mska-posnr.
              wa_order_item_inx_bapi-itm_number = wa_mska-posnr.
              wa_order_item_inx_bapi-updateflag      = 'U'.
              read table lt_statusinfo into lwa_statusinfo with key itm_number = wa_order_item_in_bapi-itm_number.
                if sy-subrc = 0.
                wa_order_item_in_bapi-target_qty       = lwa_statusinfo-req_qty - wa_estlp_aux1-erfmg.
                wa_order_item_inx_bapi-target_qty = 'X'.
                append: wa_order_item_in_bapi to t_order_item_in_bapi,
                        wa_order_item_inx_bapi to t_order_item_inx_bapi.
              endif.
              t_schedule_line_loc[] = t_schedule_line[].

              delete t_schedule_line_loc where posnr ne wa_mska-posnr.
              sort t_schedule_line_loc by edatu descending posnr ascending.
              loop at t_schedule_line_loc into wa_schedule_line.
                wa_schedule_lines_bapi-itm_number  = wa_schedule_line-posnr.
                wa_schedule_linesx_bapi-itm_number = wa_schedule_line-posnr.
                wa_schedule_lines_bapi-sched_line  = wa_schedule_line-etenr.
                wa_schedule_linesx_bapi-sched_line = wa_schedule_line-etenr.
                wa_schedule_lines_bapi-sched_type  = wa_schedule_line-ettyp.
                wa_schedule_lines_bapi-req_date    = wa_schedule_line-edatu.
                wa_schedule_linesx_bapi-req_date   = 'X'.
                wa_schedule_linesx_bapi-sched_type = 'X'.
                if sy-tabix = '1'.
                  l_erfmg = wa_estlp_aux1-erfmg.
                endif.
                if wa_schedule_line-wmeng > l_erfmg.
                  wa_schedule_lines_bapi-req_qty  = wa_schedule_line-wmeng - l_erfmg.
                  wa_schedule_linesx_bapi-req_qty = 'X'.
                  wa_schedule_linesx_bapi-updateflag = 'U'.
                  append wa_schedule_lines_bapi  to t_schedule_lines_bapi[].
                  append wa_schedule_linesx_bapi to t_schedule_linesx_bapi[].
                else.
                  l_erfmg = l_erfmg - wa_schedule_line-wmeng.
                  wa_schedule_lines_bapi-req_qty  = 0.
                  wa_schedule_linesx_bapi-req_qty = 'X'.
                  wa_schedule_linesx_bapi-updateflag = 'D'.
                  append wa_schedule_lines_bapi  to t_schedule_lines_bapi[].
                  append wa_schedule_linesx_bapi to t_schedule_linesx_bapi[].
                endif.
              endloop.

              clear: wa_order_item_in_bapi, wa_order_item_inx_bapi.
              read table lt_statusinfo into lwa_statusinfo with key itm_number = l_posnr.
                if sy-subrc = 0.
                wa_order_item_in_bapi-itm_number  = l_posnr.
                wa_order_item_inx_bapi-itm_number = l_posnr.
                wa_order_item_inx_bapi-updateflag      = 'U'.
                wa_order_item_in_bapi-target_qty       = lwa_statusinfo-req_qty + wa_estlp_aux1-erfmg.
                wa_order_item_inx_bapi-target_qty = 'X'.
                append: wa_order_item_in_bapi to t_order_item_in_bapi,
                        wa_order_item_inx_bapi to t_order_item_inx_bapi.
              endif.

              t_schedule_line_loc[] = t_schedule_line[].

              delete t_schedule_line_loc where posnr ne l_posnr.
              sort t_schedule_line_loc by edatu descending posnr ascending.
              read table t_schedule_line_loc into wa_schedule_line index 1.
              if sy-subrc = 0.
                wa_schedule_lines_bapi-itm_number  = wa_schedule_line-posnr.
                wa_schedule_linesx_bapi-itm_number = wa_schedule_line-posnr.
                wa_schedule_lines_bapi-sched_line  = wa_schedule_line-etenr.
                wa_schedule_linesx_bapi-sched_line = wa_schedule_line-etenr.
                wa_schedule_lines_bapi-sched_type  = wa_schedule_line-ettyp.
                wa_schedule_lines_bapi-req_date    = wa_schedule_line-edatu.
                wa_schedule_linesx_bapi-req_date   = 'X'.
                wa_schedule_linesx_bapi-sched_type = 'X'.

                wa_schedule_lines_bapi-req_qty  = wa_schedule_line-wmeng + wa_estlp_aux1-erfmg.
                wa_schedule_linesx_bapi-req_qty = 'X'.
                wa_schedule_linesx_bapi-updateflag = 'U'.
                append wa_schedule_lines_bapi  to t_schedule_lines_bapi[].
                append wa_schedule_linesx_bapi to t_schedule_linesx_bapi[].
              endif.
            endif.
          endif.
          call function 'BAPI_SALESORDER_CHANGE'
            exporting
              salesdocument    = l_salesdocument
              order_header_inx = wa_order_header_inx
            tables
              return           = t_return_so_change
              order_item_in    = t_order_item_in_bapi
              order_item_inx   = t_order_item_inx_bapi
              schedule_lines   = t_schedule_lines_bapi
              schedule_linesx  = t_schedule_linesx_bapi.

          read table t_return_so_change into wa_return_so_change with key type = 'E'.
            if sy-subrc = 0.
            l_message = wa_return_so_change-message.

            m_insere_log: 'Documento não foi modificado '              .
            m_insere_log:  l_salesdocument                              .
            m_insere_log:  l_message.
            m_insere_log:  '------------------------------------------' .

          else.
            call function 'BAPI_TRANSACTION_COMMIT'
              exporting
                wait   = 'X'
              importing
                return = wa_return_commit.
          endif.

        endif.
      endif.
    endif.

  endloop.