生产订单组件创建、修改、删除BAPI

1.创建 可批量

如果CO_XT_COMPONENT_ADD缺少需要的字段
可以拷贝出来,
在ZCO_XT_COMPONENT_ADD
添加字段例如:I_TEST 和 I_TESTX
或者memory id传值
在220行左右添加代码后调用BAPI即可
if i_testx is not initial.
ls_resbd_new-TEST = I_TEST.
endif.

也可以调用bapi后直接修改
(SAPLCOBC)RESB_BT[]
内的对应字段


data:is_order_key         type coxt_ord_key,
       i_material           type coxt_material,
       is_storage_location  type coxt_s_storage_location,
       is_storage_locationx type coxt_s_storage_locationx,
       is_requ_quan         type coxt_s_quantity,
       i_operation          type afvc-aplzl,
       i_posno              type cif_r3res-positionno,
       ls_return            type coxt_bapireturn,
       lv_error             type c.
  data:
    lv_matnr  type mara-matnr,
    lv_aufnr  type afpo-aufnr,
    lv_sortf  type resb-sortf,
    lv_sortfx type c,
    lv_posnr  type char4.

  types: begin of ty_resb_bt.
           include type resbb.
  types: indold type syst_tabix.
  types: no_req_upd type syst_datar.
  types: end of ty_resb_bt.

  types tt_resb_bt type table of ty_resb_bt.

  field-symbols: <ft_resb_bt> type tt_resb_bt,
                 <fs_resb_bt> type ty_resb_bt.

  data:begin of lt_aufnr occurs 0,
         aufnr type afpo-aufnr,
       end of lt_aufnr.

  refresh lt_aufnr.

  loop at gt_data1 assigning <fs_data1> where check eq 'X'
  and cellcolor is initial
  and icon ne icon_led_green.
    lt_aufnr-aufnr = <fs_data1>-aufnr.
    append lt_aufnr.
    clear  lt_aufnr.
  endloop.

  sort lt_aufnr.
  delete adjacent duplicates from lt_aufnr.

  loop at lt_aufnr.

    loop at gt_data1 assigning <fs_data1> where aufnr eq lt_aufnr-aufnr.

      clear:lv_matnr,lv_aufnr,lv_posnr,is_requ_quan,is_storage_location,is_storage_locationx,
      ls_return,lv_error,lv_sortf,lv_sortfx.

      unassign:<ft_resb_bt>,<fs_resb_bt>.

      lv_aufnr = <fs_data1>-aufnr.
      lv_matnr = <fs_data1>-matnr.

      call function 'CONVERSION_EXIT_ALPHA_INPUT'
        exporting
          input  = lv_aufnr
        importing
          output = lv_aufnr.

      call function 'CONVERSION_EXIT_ALPHA_INPUT'
        exporting
          input  = lv_matnr
        importing
          output = lv_matnr.

      is_order_key = lv_aufnr.
      i_material = lv_matnr.

      is_requ_quan-quantity = <fs_data1>-menge.

      is_storage_location-werks = <fs_data1>-werks.
      is_storage_locationx-werks =  'X'.

      if <fs_data1>-lgort is not initial.
        is_storage_location-lgort = <fs_data1>-lgort.
        is_storage_locationx-lgort =  'X'.
      endif.

      call function 'CO_XT_COMPONENT_ADD'
        exporting
          is_order_key         = is_order_key
          i_material           = i_material
          is_requ_quan         = is_requ_quan
          i_operation          = i_operation
          is_storage_location  = is_storage_location
          is_storage_locationx = is_storage_locationx
          i_postp              = 'L'
          i_posno              = i_posno
        importing
          es_bapireturn        = ls_return
          e_error_occurred     = lv_error.

      if lv_error is initial.

        assign ('(SAPLCOBC)RESB_BT[]') to <ft_resb_bt>.

        data(line) = lines( <ft_resb_bt> ).

        loop at <ft_resb_bt> assigning <fs_resb_bt>.

          "新增行数据处理
          if sy-tabix = line.

            if <fs_data1>-posnr is not initial.
              <fs_resb_bt>-posnr = <fs_data1>-posnr.
            else.

              select single max( posnr )
              into <fs_resb_bt>-posnr
              from resb
              where aufnr eq lv_aufnr.

              <fs_resb_bt>-posnr = <fs_resb_bt>-posnr + 1.
              lv_posnr = <fs_resb_bt>-posnr.
            endif.

            call function 'CONVERSION_EXIT_ALPHA_INPUT'
              exporting
                input  = <fs_resb_bt>-posnr
              importing
                output = <fs_resb_bt>-posnr.

            if <fs_data1>-sortf is not initial.
              <fs_resb_bt>-sortf = <fs_data1>-sortf.
            endif.

            if <fs_resb_bt>-xwaok is initial.
              <fs_resb_bt>-xwaok = 'X'.
            endif.

          endif.
        endloop.

        call function 'CO_XT_ORDER_PREPARE_COMMIT'
          importing
            es_bapireturn    = ls_return
            e_error_occurred = lv_error.

        if ( ls_return-type = 'S' or ls_return-type = 'W' or ls_return-type = 'I' ) or
        ls_return is initial.

          call function 'BAPI_TRANSACTION_COMMIT'
            exporting
              wait = 'X'.

          call function 'CO_XT_ORDER_INITIALIZE'.

          <fs_data1>-mseg = '新增成功'.
*          <fs_data1>-color = 'C500'.
          <fs_data1>-icon = icon_led_green.

          perform save_history_add using <fs_data1> lv_posnr.

        else.

          call function 'BAPI_TRANSACTION_ROLLBACK'.

          <fs_data1>-mseg = ls_return-message.
          <fs_data1>-icon = icon_led_red.

        endif.

      else.

        call function 'BAPI_TRANSACTION_ROLLBACK'.

        <fs_data1>-mseg = ls_return-message.
        <fs_data1>-icon = icon_led_red.
      endif.
      wait up to 1 seconds.
    endloop.
  endloop.

2.修改 单条

 data:lt_resb like table of resb with header line.
  data:lv_message type string,
       lv_matny   type mara-matnr.
  data:ft_return type table of bapiret2 with header line.
  data:lt_return type table of bapiret2 with header line.

  data:ls_return  type coxt_bapireturn,
       ls_return2 type bapiret2,
       error      type c.

  data:order   type coxt_ord_key,
       compkey type coxt_s_ord_comp_key,
       ret     type coxt_bapireturn,
       bapiret type coxt_t_bapireturn.

  data : is_order_key               type  coxt_ord_key,
         is_order_component_key     type  coxt_s_ord_comp_key,
         is_requirement_quantity    type  coxt_s_quantity,
         is_requirement_quantityx   type  coxt_s_quantityx,
         is_confirmed_quantity      type  coxt_s_quantity,
         is_confirmed_quantityx     type  coxt_s_quantityx,
         i_material                 type  coxt_material,
         i_materialx                type  coxt_materialx,
         is_order_operation_key     type  coxt_s_ord_opr_key,
         is_order_operation_keyx    type  coxt_s_ord_opr_keyx,
         is_requirement_date        type  coxt_s_date,
         is_requirement_datex       type  coxt_s_datex,
         is_storage_location        type  coxt_s_storage_location,
         is_storage_locationx       type  coxt_s_storage_locationx,
         i_batch                    type  coxt_batch,
         i_batchx                   type  coxt_batchx,
         i_mi_configuration_object  type  coxt_config_object,
         i_mi_configuration_objectx type  coxt_config_objectx.

  data :wa_resb          type resb,
        es_bapireturn    type  coxt_bapireturn,
        e_error_occurred type  c.

  data:begin of lt_aufnr occurs 0,
         aufnr type afpo-aufnr,
       end of lt_aufnr.

  refresh:lt_aufnr,lt_return.

  loop at gt_data2 assigning <fs_data2> where check eq 'X'
  and cellcolor is initial
  and icon ne icon_led_green.
    lt_aufnr-aufnr = <fs_data2>-aufnr.
    append lt_aufnr.
    clear  lt_aufnr.
  endloop.

  sort lt_aufnr.
  delete adjacent duplicates from lt_aufnr.

  loop at lt_aufnr.

    clear is_order_key.

    is_order_key = lt_aufnr-aufnr.

    call function 'CONVERSION_EXIT_ALPHA_INPUT'
      exporting
        input  = is_order_key
      importing
        output = is_order_key.

    loop at gt_data2 assigning <fs_data2> where aufnr eq lt_aufnr-aufnr.

      clear:lv_matny,ft_return[],ft_return,lv_message.

      lv_matny = <fs_data2>-matny.

      call function 'CONVERSION_EXIT_ALPHA_INPUT'
        exporting
          input  = lv_matny
        importing
          output = lv_matny.

      if <fs_data2>-potx1 is initial and p_bf ne 'X'.
        select *
        into corresponding fields of table lt_resb
        from resb
        where aufnr eq is_order_key
        and   matnr eq lv_matny
        and   xloek ne 'X'.
      else.
        select *
        into corresponding fields of table lt_resb
        from resb
        where aufnr eq is_order_key
        and   matnr eq lv_matny
        and   xloek ne 'X'
        and   potx1 eq <fs_data2>-potx1.
      endif.


      if lt_resb[] is initial.
        <fs_data2>-mseg = '无该原物料可替换'.
        <fs_data2>-icon = icon_led_red.
        continue.
      endif.

      loop at lt_resb.

        clear:ls_return,ls_return2,is_order_component_key,
        is_requirement_quantity,is_requirement_quantityx,
        is_storage_location,is_storage_locationx,i_material,i_materialx,
        is_storage_location,is_storage_locationx,lv_message,error.

        move-corresponding lt_resb to is_order_component_key.

        if <fs_data2>-matnr is initial.
          i_material = <fs_data2>-matny.
        else.
          i_material = <fs_data2>-matnr.
        endif.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  = i_material
          importing
            output = i_material.
        i_materialx = 'X'.

        is_storage_location-werks = lt_resb-werks.
        is_storage_locationx-werks = 'X'.

        if <fs_data2>-menge is not initial.
          is_requirement_quantity-quantity = <fs_data2>-menge.
          is_requirement_quantityx-quantity = 'X'.
        endif.
        if <fs_data2>-lgort is not initial.
          is_storage_location-lgort  = <fs_data2>-lgort.
          is_storage_locationx-lgort = 'X'.
        endif.

        clear lv_zcomp.


        if <fs_data2>-matnr is initial."新物料号为空 组件为原物料号
          concatenate lt_resb-posnr lt_resb-matnr into lv_zcomp separated by '/'.
        else."新物料号不为空 修改的是新物料号的 数量和库区
          concatenate lt_resb-posnr <fs_data2>-matnr into lv_zcomp separated by '/'.
        endif.

        call function 'CO_XT_COMPONENT_CHANGE'
          exporting
            is_order_key               = is_order_key
            is_order_component_key     = is_order_component_key
            is_requirement_quantity    = is_requirement_quantity
            is_requirement_quantityx   = is_requirement_quantityx
            is_confirmed_quantity      = is_confirmed_quantity
            is_confirmed_quantityx     = is_confirmed_quantityx
            i_material                 = i_material
            i_materialx                = i_materialx
            is_storage_location        = is_storage_location
            is_storage_locationx       = is_storage_locationx
            i_batch                    = i_batch
            i_batchx                   = i_batchx
            i_mi_configuration_object  = i_mi_configuration_object
            i_mi_configuration_objectx = i_mi_configuration_objectx
          importing
            es_bapireturn              = ls_return
            e_error_occurred           = error.

        if error is initial.

          call function 'CO_XT_ORDER_PREPARE_COMMIT'
            importing
              e_order_key      = is_order_key
              es_bapireturn    = ret
              e_error_occurred = error.
          if ( ret-type = 'S' or ret-type = 'W' or ret-type = 'I' ) or ret is initial.

            data :lt_resb_get type table of resbdget,
                  lw_resb_get like line of lt_resb_get.
            clear:lt_resb_get[],lw_resb_get.
            call function 'CO_BC_RESBD_OF_ORDER_GET'
              exporting
                aufnr_act = is_order_key
*               GET_OLD   = ' '
*               NO_MAKTX  = 'X'
*               NO_STATX  = 'X'
*               CHECK_DELETED        = ' '
*               NO_READ   = ' '
*               CHECK_VBKZ_DEL       = 'X'
*               I_ARCHIVE = ' '
              tables
*               ARESB_EXP =
                resbd_get = lt_resb_get[].
            loop at lt_resb_get into lw_resb_get where rsnum = lt_resb-rsnum
            and rspos = lt_resb-rspos
            and rsart = lt_resb-rsart..
              if <fs_data2>-sortf is not initial.
                lw_resb_get-sortf = <fs_data2>-sortf.
              endif.
              modify lt_resb_get from lw_resb_get index sy-tabix.
            endloop.

            call function 'CO_BC_UPDATE_CMP_OF_ORDERS'
*   EXPORTING
*     FLG_BANF_UPD       = YX
*     FLG_CALC_CHK       = YX
*     FLG_LVS_CHK        = YX
*   IMPORTING
*     FLG_LVS            =
              tables
                resbd_get = lt_resb_get[].

          
            call function 'BAPI_TRANSACTION_COMMIT'
              exporting
                wait = 'X'.

            call function 'CO_XT_ORDER_INITIALIZE'.

          endif.

        else.

          call function 'BAPI_TRANSACTION_ROLLBACK'.

    
        endif.

        wait up to 1 seconds.

      endloop.

   
    endloop.
  endloop.

3.删除 可批量

  data:lt_resb type table of resb with header line.
  data:lt_return type coxt_t_bapireturn with header line.
  data:ls_return type bapiret2.
  data it_resb type coxt_t_resbdel with header line .
  data:lv_matnr   type mara-matnr,
       lv_aufnr   type afko-aufnr,
       lv_message type string,
       lv_error   type c.


  data:ft_return type table of bapiret2 with header line.

  data:begin of lt_aufnr occurs 0,
         aufnr type afpo-aufnr,
       end of lt_aufnr.

  refresh lt_aufnr.

  loop at gt_data3 assigning <fs_data3> where check eq 'X'
  and cellcolor is initial
  and icon ne icon_led_green.
    lt_aufnr-aufnr = <fs_data3>-aufnr.
    append lt_aufnr.
    clear  lt_aufnr.
  endloop.

  sort lt_aufnr.
  delete adjacent duplicates from lt_aufnr.

  loop at lt_aufnr.

    clear:lt_resb[],lt_resb,lv_message,ft_return[],ft_return,ls_return,lv_error,
    lt_return[],lt_return,it_resb[],it_resb.

    loop at gt_data3 assigning <fs_data3> where aufnr eq lt_aufnr-aufnr.

      clear:lv_matnr,lv_aufnr.

      lv_matnr = <fs_data3>-matnr.
      lv_aufnr = <fs_data3>-aufnr.

      call function 'CONVERSION_EXIT_ALPHA_INPUT'
        exporting
          input  = lv_matnr
        importing
          output = lv_matnr.
      call function 'CONVERSION_EXIT_ALPHA_INPUT'
        exporting
          input  = lv_aufnr
        importing
          output = lv_aufnr.

      if <fs_data3>-potx1 is initial and p_bf ne 'X'.
        select rsnum rspos matnr posnr sortf aufnr
        into corresponding fields of table lt_resb
        from resb
        where aufnr eq lv_aufnr
        and   matnr eq lv_matnr
        and   xloek ne 'X'.
      else.
        select rsnum rspos matnr posnr sortf aufnr
        into corresponding fields of table lt_resb
        from resb
        where aufnr eq lv_aufnr
        and   matnr eq lv_matnr
        and   xloek ne 'X'
        and   potx1 eq <fs_data3>-potx1.
      endif.

      if lt_resb[] is initial.
        <fs_data3>-mseg = '无该物料可删除'.
        <fs_data3>-icon = icon_led_red.
        continue.
      endif.

    endloop.

    call function 'CO_XT_COMPONENTS_DELETE'
      exporting
        it_resbkeys_to_delete = it_resb[]
      tables
        ct_bapireturn         = lt_return
      exceptions
        delete_failed         = 1
        others                = 2.

    if lt_return[] is initial.
*刷新变量
      perform change_flag_set(saplcozv).
*数据更新到DB
      call function 'CO_XT_ORDER_PREPARE_COMMIT'
        importing
          es_bapireturn    = ls_return
          e_error_occurred = lv_error.

      if ( ls_return-type = 'S' or ls_return-type = 'W' or ls_return-type = 'I' )
      or ls_return is initial.
*DB COMMMIT
        call function 'BAPI_TRANSACTION_COMMIT'
          exporting
            wait = 'X'.
*刷新变量
        call function 'CO_XT_ORDER_INITIALIZE'.

        loop at gt_data3 assigning <fs_data3> where aufnr eq lt_aufnr-aufnr.
          <fs_data3>-mseg = '删除成功'.
          <fs_data3>-icon = icon_led_green.
        endloop.


      else.
        clear: lv_error,ls_return.
        call function 'BAPI_TRANSACTION_ROLLBACK'.
      endif.

    else.

      call function 'BAPI_TRANSACTION_ROLLBACK'.

   
      loop at gt_data3 assigning <fs_data3> where aufnr eq lt_aufnr-aufnr.
        <fs_data3>-mseg = lv_message.
        <fs_data3>-icon = icon_led_red.
      endloop.

      clear lv_message.

    endif.

  endloop.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343

推荐阅读更多精彩内容