Difference between revisions of "SAP MM BAPI BOM"

From SapWiki
(Created page with " <nowiki> 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....")
 
 
Line 1: Line 1:
  <nowiki> FORM create_material_bom.
+
  <nowiki>FORM create_material_bom.
+
 
  DATA lt_bomgroup TYPE TABLE OF  bapi1080_bgr_c WITH HEADER LINE.
+
  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_variants TYPE TABLE OF bapi1080_bom_c WITH HEADER LINE.
  DATA lt_materialrelations TYPE TABLE OF  bapi1080_mbm_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_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_itemassignments TYPE TABLE OF  bapi1080_rel_itm_bom_c WITH HEADER LINE.
  DATA lt_return TYPE TABLE OF bapiret2 WITH HEADER LINE.
+
  DATA lt_return TYPE TABLE OF bapiret2 WITH HEADER LINE.
  DATA ls_return LIKE LINE OF lt_return.
+
  DATA ls_return LIKE LINE OF lt_return.
  DATA ld_item_no(4) TYPE n.
+
  DATA ld_item_no(4) TYPE n.
+
 
  CLEAR lt_bomgroup[].
+
  CLEAR lt_bomgroup[].
  CLEAR lt_bomgroup.
+
  CLEAR lt_bomgroup.
  lt_bomgroup-bom_group_identification  = 'MDF'.
+
  lt_bomgroup-bom_group_identification  = 'MDF'.
  lt_bomgroup-object_type = 'BGR'.
+
  lt_bomgroup-object_type = 'BGR'.
  lt_bomgroup-object_id  = '00000001'.
+
  lt_bomgroup-object_id  = '00000001'.
  lt_bomgroup-bom_usage  = '5'." BOM TIPO 5
+
  lt_bomgroup-bom_usage  = '5'." BOM TIPO 5
  lt_bomgroup-bom_group  = '00000001'.
+
  lt_bomgroup-bom_group  = '00000001'.
  lt_bomgroup-created_in_plant  = marc-werks."
+
  lt_bomgroup-created_in_plant  = marc-werks."
  lt_bomgroup-ltxt_lang  = 'S'.
+
  lt_bomgroup-ltxt_lang  = 'S'.
  lt_bomgroup-bom_text  = 'CREADO VIA ZMDF'.
+
  lt_bomgroup-bom_text  = 'CREADO VIA ZMDF'.
  APPEND lt_bomgroup.
+
  APPEND lt_bomgroup.
+
 
  CLEAR lt_variants[].
+
  CLEAR lt_variants[].
  CLEAR lt_variants.
+
  CLEAR lt_variants.
  lt_variants-bom_group_identification  = 'MDF'.
+
  lt_variants-bom_group_identification  = 'MDF'.
  lt_variants-object_type  = 'BOM'.
+
  lt_variants-object_type  = 'BOM'.
  lt_variants-object_id    = '00000001'.
+
  lt_variants-object_id    = '00000001'.
  lt_variants-alternative_bom = '01'.
+
  lt_variants-alternative_bom = '01'.
  lt_variants-bom_status = '01'.
+
  lt_variants-bom_status = '01'.
  lt_variants-base_qty  = '1'.
+
  lt_variants-base_qty  = '1'.
  lt_variants-base_unit  = 'ST' . "'UN'.
+
  lt_variants-base_unit  = 'ST' . "'UN'.
  lt_variants-ltxt_lang  = 'S'.
+
  lt_variants-ltxt_lang  = 'S'.
  lt_variants-alt_text  = 'CREADO VIA ZMDF'.
+
  lt_variants-alt_text  = 'CREADO VIA ZMDF'.
  lt_variants-valid_from_date  = sy-datum - 30.
+
  lt_variants-valid_from_date  = sy-datum - 30.
  lt_variants-function  = 'NEW'.
+
  lt_variants-function  = 'NEW'.
  APPEND lt_variants.
+
  APPEND lt_variants.
+
 
  CLEAR lt_materialrelations[].
+
  CLEAR lt_materialrelations[].
  CLEAR lt_materialrelations.
+
  CLEAR lt_materialrelations.
  lt_materialrelations-bom_group_identification  = 'MDF'.
+
  lt_materialrelations-bom_group_identification  = 'MDF'.
  lt_materialrelations-material = wa_headdata-material.
+
  lt_materialrelations-material = wa_headdata-material.
  lt_materialrelations-plant  = marc-werks.
+
  lt_materialrelations-plant  = marc-werks.
  lt_materialrelations-bom_usage  = '1'.
+
  lt_materialrelations-bom_usage  = '1'.
  lt_materialrelations-alternative_bom  = '01'.
+
  lt_materialrelations-alternative_bom  = '01'.
  APPEND lt_materialrelations.
+
  APPEND lt_materialrelations.
+
 
  CLEAR lt_items[].
+
  CLEAR lt_items[].
  ld_item_no = 0.
+
  ld_item_no = 0.
  LOOP AT gt_comp INTO gs_comp WHERE matnr IS NOT INITIAL.
+
  LOOP AT gt_comp INTO gs_comp WHERE matnr IS NOT INITIAL.
    CLEAR lt_items.
+
    CLEAR lt_items.
+
 
    ld_item_no = ld_item_no + 10.
+
    ld_item_no = ld_item_no + 10.
    lt_items-bom_group_identification = 'MDF'.
+
    lt_items-bom_group_identification = 'MDF'.
    lt_items-object_type = 'ITM'.
+
    lt_items-object_type = 'ITM'.
    lt_items-object_id = '00000001'.
+
    lt_items-object_id = '00000001'.
    lt_items-item_no  = ld_item_no.
+
    lt_items-item_no  = ld_item_no.
*  : Ajuste posiciones de servicio
+
*  : Ajuste posiciones de servicio
    SELECT COUNT(*) FROM mara
+
    SELECT COUNT(*) FROM mara
            WHERE matnr = gs_comp-matnr
+
          WHERE matnr = gs_comp-matnr
            AND mtart = 'DIEN'.
+
          AND mtart = 'DIEN'.
    IF sy-subrc = 0.
+
    IF sy-subrc = 0.
      lt_items-item_cat = 'N'.
+
      lt_items-item_cat = 'N'.
    ELSE.
+
    ELSE.
      lt_items-item_cat = 'L'.
+
      lt_items-item_cat = 'L'.
    ENDIF.
+
    ENDIF.
*  Fin  
+
*  Fin  
    lt_items-component = gs_comp-matnr.
+
    lt_items-component = gs_comp-matnr.
    lt_items-comp_qty  = gs_comp-menge.
+
    lt_items-comp_qty  = gs_comp-menge.
    lt_items-comp_unit  = gs_comp-meins.
+
    lt_items-comp_unit  = gs_comp-meins.
    lt_items-valid_from_date = sy-datum - 30.
+
    lt_items-valid_from_date = sy-datum - 30.
    APPEND lt_items.
+
    APPEND lt_items.
  ENDLOOP.
+
  ENDLOOP.
+
 
  CLEAR lt_itemassignments[].
+
  CLEAR lt_itemassignments[].
  CLEAR lt_itemassignments.
+
  CLEAR lt_itemassignments.
  lt_itemassignments-bom_group_identification  = 'MDF'.
+
  lt_itemassignments-bom_group_identification  = 'MDF'.
  lt_itemassignments-sub_object_type = 'ITM'.
+
  lt_itemassignments-sub_object_type = 'ITM'.
  lt_itemassignments-sub_object_id  = '00000001'.
+
  lt_itemassignments-sub_object_id  = '00000001'.
  lt_itemassignments-super_object_type  = 'BOM'.
+
  lt_itemassignments-super_object_type  = 'BOM'.
  lt_itemassignments-super_object_id  = '00000001'.
+
  lt_itemassignments-super_object_id  = '00000001'.
  lt_itemassignments-valid_from_date  = sy-datum - 30.
+
  lt_itemassignments-valid_from_date  = sy-datum - 30.
  lt_itemassignments-function  = 'NEW'.
+
  lt_itemassignments-function  = 'NEW'.
  APPEND lt_itemassignments.
+
  APPEND lt_itemassignments.
+
 
  CALL FUNCTION 'BAPI_MATERIAL_BOM_GROUP_CREATE'
+
  CALL FUNCTION 'BAPI_MATERIAL_BOM_GROUP_CREATE'
* EXPORTING
+
* EXPORTING
*  TESTRUN                  = ' '
+
*  TESTRUN                  = ' '
*  ALL_ERROR                = ' '
+
*  ALL_ERROR                = ' '
    TABLES
+
    TABLES
      bomgroup                = lt_bomgroup
+
      bomgroup                = lt_bomgroup
      variants                = lt_variants
+
      variants                = lt_variants
      items                    = lt_items
+
      items                    = lt_items
*  SUBITEMS                =
+
*  SUBITEMS                =
      materialrelations        = lt_materialrelations
+
      materialrelations        = lt_materialrelations
      itemassignments          = lt_itemassignments
+
      itemassignments          = lt_itemassignments
*  SUBITEMASSIGNMENTS      =
+
*  SUBITEMASSIGNMENTS      =
*  TEXTS                    =
+
*  TEXTS                    =
        return                  = lt_return
+
      return                  = lt_return
            .
+
            .
+
 
  LOOP AT  lt_return INTO ls_return WHERE type CA 'EA'.
+
  LOOP AT  lt_return INTO ls_return WHERE type CA 'EA'.
+
 
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
+
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
*      IMPORTING
+
*      IMPORTING
*        RETURN        =
+
*        RETURN        =
          .
+
          .
+
 
    MESSAGE ID ls_return-id TYPE 'E' NUMBER ls_return-number
+
    MESSAGE ID ls_return-id TYPE 'E' NUMBER ls_return-number
                    WITH ls_return-message_v1
+
                  WITH ls_return-message_v1
                        ls_return-message_v2
+
                        ls_return-message_v2
                        ls_return-message_v3
+
                        ls_return-message_v3
                        ls_return-message_v4.
+
                        ls_return-message_v4.
  ENDLOOP.
+
  ENDLOOP.
  IF sy-subrc <> 0.
+
  IF sy-subrc <> 0.
    LOOP AT lt_return INTO ls_return WHERE type NA 'EA'.
+
    LOOP AT lt_return INTO ls_return WHERE type NA 'EA'.
    ENDLOOP.
+
    ENDLOOP.
    IF sy-subrc = 0.
+
    IF sy-subrc = 0.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
+
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
+
        EXPORTING
          wait  = co_yes
+
          wait  = co_yes
        IMPORTING
+
        IMPORTING
          return = wa_return_commit.
+
          return = wa_return_commit.
+
 
      MESSAGE ID ls_return-id TYPE 'I' NUMBER ls_return-number
+
      MESSAGE ID ls_return-id TYPE 'I' NUMBER ls_return-number
                  WITH ls_return-message_v1
+
                WITH ls_return-message_v1
                      ls_return-message_v2
+
                      ls_return-message_v2
                      ls_return-message_v3
+
                      ls_return-message_v3
                      ls_return-message_v4.
+
                      ls_return-message_v4.
    ENDIF.
+
    ENDIF.
+
 
+
 
  ENDIF.
+
  ENDIF.
+
 
ENDFORM.                    "create_material_bom</nowiki>
+
ENDFORM.                    "create_material_bom</nowiki>

Latest revision as of 22:09, 4 April 2020

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