SAP MM BAPI BOM

From SapWiki
Revision as of 22:09, 4 April 2020 by WikiSysop (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
FORM create_material_bom.

  DATA lt_bomgroup TYPE TABLE OF  bapi1080_bgr_c WITH HEADER LINE.
  DATA lt_variants TYPE TABLE OF bapi1080_bom_c WITH HEADER LINE.
  DATA lt_materialrelations TYPE TABLE OF  bapi1080_mbm_c WITH HEADER LINE.
  DATA   lt_items TYPE TABLE OF  bapi1080_itm_c WITH HEADER LINE.
  DATA lt_itemassignments TYPE TABLE OF  bapi1080_rel_itm_bom_c WITH HEADER LINE.
  DATA lt_return TYPE TABLE OF bapiret2 WITH HEADER LINE.
  DATA ls_return LIKE LINE OF lt_return.
  DATA ld_item_no(4) TYPE n.

  CLEAR lt_bomgroup[].
  CLEAR lt_bomgroup.
  lt_bomgroup-bom_group_identification  = 'MDF'.
  lt_bomgroup-object_type = 'BGR'.
  lt_bomgroup-object_id  = '00000001'.
  lt_bomgroup-bom_usage  = '5'." BOM TIPO 5
  lt_bomgroup-bom_group  = '00000001'.
  lt_bomgroup-created_in_plant  = marc-werks."
  lt_bomgroup-ltxt_lang   = 'S'.
  lt_bomgroup-bom_text  = 'CREADO VIA ZMDF'.
  APPEND lt_bomgroup.

  CLEAR lt_variants[].
  CLEAR lt_variants.
  lt_variants-bom_group_identification  = 'MDF'.
  lt_variants-object_type  = 'BOM'.
  lt_variants-object_id    = '00000001'.
  lt_variants-alternative_bom = '01'.
  lt_variants-bom_status = '01'.
  lt_variants-base_qty  = '1'.
  lt_variants-base_unit  = 'ST' . "'UN'.
  lt_variants-ltxt_lang  = 'S'.
  lt_variants-alt_text   = 'CREADO VIA ZMDF'.
  lt_variants-valid_from_date  = sy-datum - 30.
  lt_variants-function   = 'NEW'.
  APPEND lt_variants.

  CLEAR lt_materialrelations[].
  CLEAR lt_materialrelations.
  lt_materialrelations-bom_group_identification  = 'MDF'.
  lt_materialrelations-material = wa_headdata-material.
  lt_materialrelations-plant  = marc-werks.
  lt_materialrelations-bom_usage   = '1'.
  lt_materialrelations-alternative_bom  = '01'.
  APPEND lt_materialrelations.

  CLEAR lt_items[].
  ld_item_no = 0.
  LOOP AT gt_comp INTO gs_comp WHERE matnr IS NOT INITIAL.
    CLEAR lt_items.

    ld_item_no = ld_item_no + 10.
    lt_items-bom_group_identification = 'MDF'.
    lt_items-object_type = 'ITM'.
    lt_items-object_id = '00000001'.
    lt_items-item_no  = ld_item_no.
*   : Ajuste posiciones de servicio
    SELECT COUNT(*) FROM mara
           WHERE matnr = gs_comp-matnr
           AND mtart = 'DIEN'.
    IF sy-subrc = 0.
      lt_items-item_cat = 'N'.
    ELSE.
      lt_items-item_cat = 'L'.
    ENDIF.
*   Fin 
    lt_items-component = gs_comp-matnr.
    lt_items-comp_qty  = gs_comp-menge.
    lt_items-comp_unit  = gs_comp-meins.
    lt_items-valid_from_date = sy-datum - 30.
    APPEND lt_items.
  ENDLOOP.

  CLEAR lt_itemassignments[].
  CLEAR lt_itemassignments.
  lt_itemassignments-bom_group_identification  = 'MDF'.
  lt_itemassignments-sub_object_type = 'ITM'.
  lt_itemassignments-sub_object_id   = '00000001'.
  lt_itemassignments-super_object_type  = 'BOM'.
  lt_itemassignments-super_object_id  = '00000001'.
  lt_itemassignments-valid_from_date  = sy-datum - 30.
  lt_itemassignments-function  = 'NEW'.
  APPEND lt_itemassignments.

  CALL FUNCTION 'BAPI_MATERIAL_BOM_GROUP_CREATE'
* EXPORTING
*   TESTRUN                  = ' '
*   ALL_ERROR                = ' '
    TABLES
      bomgroup                 = lt_bomgroup
      variants                 = lt_variants
      items                    = lt_items
*   SUBITEMS                 =
      materialrelations        = lt_materialrelations
      itemassignments          = lt_itemassignments
*   SUBITEMASSIGNMENTS       =
*   TEXTS                    =
       return                  = lt_return
            .

  LOOP AT  lt_return INTO ls_return WHERE type CA 'EA'.

    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
*       IMPORTING
*         RETURN        =
          .

    MESSAGE ID ls_return-id TYPE 'E' NUMBER ls_return-number
                   WITH ls_return-message_v1
                        ls_return-message_v2
                        ls_return-message_v3
                        ls_return-message_v4.
  ENDLOOP.
  IF sy-subrc <> 0.
    LOOP AT lt_return INTO ls_return WHERE type NA 'EA'.
    ENDLOOP.
    IF sy-subrc = 0.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait   = co_yes
        IMPORTING
          return = wa_return_commit.

      MESSAGE ID ls_return-id TYPE 'I' NUMBER ls_return-number
                 WITH ls_return-message_v1
                      ls_return-message_v2
                      ls_return-message_v3
                      ls_return-message_v4.
    ENDIF.


  ENDIF.

ENDFORM.                    "create_material_bom