Difference between revisions of "SAP MM BAPI MATERIAL"

From SapWiki
(Created page with " *----------------------------------------------------------------------* ***INCLUDE *----------------------------------------------------------------------* *&-----------...")
 
 
Line 1: Line 1:
  *----------------------------------------------------------------------*
+
  <nowiki>*----------------------------------------------------------------------*
***INCLUDE  
+
***INCLUDE  
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*&      Form  SEARCH
+
*&      Form  SEARCH
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*      text
+
*      text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
*  -->  p1        text
+
*  -->  p1        text
*  <--  p2        text
+
*  <--  p2        text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
FORM SEARCH .
+
FORM SEARCH .
  data lt_stpo type TABLE OF stpo.
+
data lt_stpo type TABLE OF stpo.
  data ls_stpo like LINE OF lt_stpo.
+
data ls_stpo like LINE OF lt_stpo.
  data ld_f.
+
data ld_f.
  data ld_l type i.
+
data ld_l type i.
  
  clear: mast,
+
clear: mast,
        knmt,
+
        knmt,
        kna1-name1,
+
        kna1-name1,
        makt,
+
        makt,
        gd_maktx,
+
        gd_maktx,
        t001w.
+
        t001w.
  
  select single * from kna1 where KUNNR = kna1-kunnr.
+
select single * from kna1 where KUNNR = kna1-kunnr.
  
  select SINGLE * from t001w where werks = marc-werks.
+
select SINGLE * from t001w where werks = marc-werks.
  
  loop at gt_comp into gs_comp where matnr is NOT INITIAL.
+
loop at gt_comp into gs_comp where matnr is NOT INITIAL.
    SELECT    * APPENDING TABLE lt_stpo FROM  STPO
+
  SELECT    * APPENDING TABLE lt_stpo FROM  STPO
          WHERE  IDNRK  = gs_comp-MATNR
+
          WHERE  IDNRK  = gs_comp-MATNR
          AND    MENGE  = gs_comp-menge.
+
          AND    MENGE  = gs_comp-menge.
    if sy-subrc <> 0.
+
  if sy-subrc <> 0.
* debe encontrarse para todos los comp
+
* debe encontrarse para todos los comp
      ld_f = co_yes.
+
    ld_f = co_yes.
      exit.
+
    exit.
    endif.
+
  endif.
  
  ENDLOOP.
+
ENDLOOP.
  
  case ld_f.
+
case ld_f.
    when co_yes.
+
  when co_yes.
  
    when co_no.
+
  when co_no.
      sort lt_stpo by STLNR.
+
    sort lt_stpo by STLNR.
      delete ADJACENT DUPLICATES FROM lt_stpo COMPARING STLNR.
+
    delete ADJACENT DUPLICATES FROM lt_stpo COMPARING STLNR.
  
      ld_l = lines( lt_stpo ).
+
    ld_l = lines( lt_stpo ).
      if ld_l = 1.
+
    if ld_l = 1.
        read TABLE lt_stpo into ls_stpo index 1.
+
      read TABLE lt_stpo into ls_stpo index 1.
        select single * from mast where stlnr = ls_stpo-stlnr
+
      select single * from mast where stlnr = ls_stpo-stlnr
                            and werks  = marc-werks.
+
                          and werks  = marc-werks.
        if sy-subrc = 0.
+
      if sy-subrc = 0.
          select SINGLE * from makt where matnr = mast-matnr
+
        select SINGLE * from makt where matnr = mast-matnr
                                      and spras = sy-langu.
+
                                    and spras = sy-langu.
  
          select single * from KNMT where matnr = mast-matnr
+
        select single * from KNMT where matnr = mast-matnr
                                        and kunnr = kna1-kunnr.
+
                                      and kunnr = kna1-kunnr.
          if sy-subrc = 0.
+
        if sy-subrc = 0.
            select SINGLE maktx into gd_maktx from makt where matnr = knmt-matnr
+
          select SINGLE maktx into gd_maktx from makt where matnr = knmt-matnr
                          and spras = sy-langu.
+
                        and spras = sy-langu.
          endif.
+
        endif.
        endif.
+
      endif.
  
      endif.
+
    endif.
  
  ENDCASE.
+
ENDCASE.
  
ENDFORM.                    " SEARCH
+
ENDFORM.                    " SEARCH
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*&      Form  UPDATE
+
*&      Form  UPDATE
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*      text
+
*      text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
*  -->  p1        text
+
*  -->  p1        text
*  <--  p2        text
+
*  <--  p2        text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
FORM UPDATE .
+
FORM UPDATE .
  
  select single * from kna1 where KUNNR = kna1-kunnr.
+
select single * from kna1 where KUNNR = kna1-kunnr.
  select SINGLE * from t001w where werks = marc-werks.
+
select SINGLE * from t001w where werks = marc-werks.
  
  loop at gt_comp into gs_comp where matnr is NOT INITIAL.
+
loop at gt_comp into gs_comp where matnr is NOT INITIAL.
    gd_tabix = sy-tabix.
+
  gd_tabix = sy-tabix.
    select SINGLE maktx into gs_comp-maktx from makt
+
  select SINGLE maktx into gs_comp-maktx from makt
                        where matnr = gs_comp-matnr
+
                      where matnr = gs_comp-matnr
                          and spras = sy-langu.
+
                          and spras = sy-langu.
    if sy-subrc = 0.
+
  if sy-subrc = 0.
      modify gt_comp from gs_comp INDEX gd_tabix.
+
    modify gt_comp from gs_comp INDEX gd_tabix.
    endif.
+
  endif.
  
  ENDLOOP.
+
ENDLOOP.
  
ENDFORM.                    " UPDATE
+
ENDFORM.                    " UPDATE
  
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*&      Form  f_crear_material
+
*&      Form  f_crear_material
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*      text
+
*      text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
FORM f_crear_material .
+
FORM f_crear_material .
  
  PERFORM fill_data.
+
PERFORM fill_data.
  
  CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
+
CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
  EXPORTING
+
EXPORTING
    headdata            = wa_headdata
+
  headdata            = wa_headdata
    clientdata            = wa_clientdata_new
+
  clientdata            = wa_clientdata_new
    clientdatax          = wa_clientdatax
+
  clientdatax          = wa_clientdatax
    SALESDATA            = wa_salesdata_new
+
  SALESDATA            = wa_salesdata_new
    SALESDATAx          = wa_salesdatax
+
  SALESDATAx          = wa_salesdatax
  IMPORTING
+
IMPORTING
    RETURN              = wa_return
+
  RETURN              = wa_return
  TABLES
+
TABLES
    materialdescription  = gt_bapi_makt
+
  materialdescription  = gt_bapi_makt
    taxclassifications  = gt_mlan          "Tax Data
+
  taxclassifications  = gt_mlan          "Tax Data
    unitsofmeasure      = gt_bapi_marm    "Units of measure
+
  unitsofmeasure      = gt_bapi_marm    "Units of measure
    unitsofmeasurex      = gt_bapi_marmx    "Checkbox for Units of measure
+
  unitsofmeasurex      = gt_bapi_marmx    "Checkbox for Units of measure
    returnmessages      = gt_returnmessages
+
  returnmessages      = gt_returnmessages
*      materiallongtext    = it_bapi_mltx
+
*      materiallongtext    = it_bapi_mltx
    .
+
  .
  
*  if p_test <> 'X'.
+
*  if p_test <> 'X'.
  IF wa_return-type <> 'E'.
+
IF wa_return-type <> 'E'.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
+
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*      EXPORTING
+
*      EXPORTING
*        wait  = abap_on
+
*        wait  = abap_on
      IMPORTING
+
    IMPORTING
        return = wa_return_commit.
+
      return = wa_return_commit.
  ENDIF.
+
ENDIF.
* endif.
+
* endif.
  
ENDFORM.                    " crear_material
+
ENDFORM.                    " crear_material
  
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*&      Form  get_matnr
+
*&      Form  get_matnr
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*      text
+
*      text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
*      -->O_MATNR    text
+
*      -->O_MATNR    text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
form get_matnr CHANGING o_matnr.
+
form get_matnr CHANGING o_matnr.
  data: gv_bapireturn1 type bapireturn1,
+
data: gv_bapireturn1 type bapireturn1,
        gt_bapimatnr like table of BAPIMATINR,
+
      gt_bapimatnr like table of BAPIMATINR,
        gs_bapimatnr LIKE LINE OF  gt_bapimatnr.
+
      gs_bapimatnr LIKE LINE OF  gt_bapimatnr.
  
  CALL FUNCTION 'BAPI_MATERIAL_GETINTNUMBER'
+
CALL FUNCTION 'BAPI_MATERIAL_GETINTNUMBER'
    EXPORTING
+
  EXPORTING
      material_type    = '1'
+
    material_type    = '1'
      industry_sector  = 'HAWA'
+
    industry_sector  = 'HAWA'
      required_numbers = 1
+
    required_numbers = 1
    IMPORTING
+
  IMPORTING
      return          = gv_bapireturn1
+
    return          = gv_bapireturn1
    TABLES
+
  TABLES
      material_number  = gt_bapimatnr.
+
    material_number  = gt_bapimatnr.
  IF gv_bapireturn1-type EQ 'S'.
+
IF gv_bapireturn1-type EQ 'S'.
    READ TABLE gt_bapimatnr INTO gs_bapimatnr INDEX 1.
+
  READ TABLE gt_bapimatnr INTO gs_bapimatnr INDEX 1.
    IF sy-subrc EQ 0.
+
  IF sy-subrc EQ 0.
      o_matnr = gs_bapimatnr-material.
+
    o_matnr = gs_bapimatnr-material.
    ENDIF.
+
  ENDIF.
  ENDIF.
+
ENDIF.
ENDFORM.                    "get_matnr
+
ENDFORM.                    "get_matnr
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*&      Form  fill_data
+
*&      Form  fill_data
*&---------------------------------------------------------------------*
+
*&---------------------------------------------------------------------*
*      text
+
*      text
*----------------------------------------------------------------------*
+
*----------------------------------------------------------------------*
form fill_data.
+
form fill_data.
  data gs_bapi_makt like LINE OF gt_bapi_makt.
+
data gs_bapi_makt like LINE OF gt_bapi_makt.
  data gs_bapi_marm like LINE OF gt_bapi_marm.
+
data gs_bapi_marm like LINE OF gt_bapi_marm.
  data gs_bapi_marmx like LINE OF gt_bapi_marmx.
+
data gs_bapi_marmx like LINE OF gt_bapi_marmx.
  data gs_mlan like LINE OF gt_mlan.
+
data gs_mlan like LINE OF gt_mlan.
  
  clear: wa_headdata,
+
clear: wa_headdata,
        wa_clientdata_new,
+
        wa_clientdata_new,
        wa_clientdatax,
+
        wa_clientdatax,
        wa_salesdata_new,
+
        wa_salesdata_new,
        wa_salesdatax,
+
        wa_salesdatax,
        gt_bapi_makt[],
+
        gt_bapi_makt[],
        gt_bapi_marm[],
+
        gt_bapi_marm[],
        gt_bapi_marmx[],
+
        gt_bapi_marmx[],
        gt_mlan[].
+
        gt_mlan[].
  
  perform get_matnr CHANGING  wa_headdata-material .
+
perform get_matnr CHANGING  wa_headdata-material .
  wa_headdata-ind_sector        =  '1'.
+
wa_headdata-ind_sector        =  '1'.
  wa_headdata-matl_type          =  'HAWA'.
+
wa_headdata-matl_type          =  'HAWA'.
  
  wa_headdata-basic_view        =  co_yes.
+
wa_headdata-basic_view        =  co_yes.
  wa_headdata-sales_view        =  co_yes.
+
wa_headdata-sales_view        =  co_yes.
  wa_headdata-purchase_view      =  co_yes.
+
wa_headdata-purchase_view      =  co_yes.
  wa_headdata-mrp_view            =  co_yes.
+
wa_headdata-mrp_view            =  co_yes.
  wa_headdata-forecast_view      =  co_yes.
+
wa_headdata-forecast_view      =  co_yes.
*  wa_headdata-work_sched_view    =  abap_on.
+
*  wa_headdata-work_sched_view    =  abap_on.
*  wa_headdata-prt_view          =  abap_on.
+
*  wa_headdata-prt_view          =  abap_on.
  wa_headdata-storage_view      =  co_yes.    "!
+
wa_headdata-storage_view      =  co_yes.    "!
**  wa_headdata-warehouse_view    =  abap_on.
+
**  wa_headdata-warehouse_view    =  abap_on.
*  wa_headdata-quality_view      =  abap_on.
+
*  wa_headdata-quality_view      =  abap_on.
  wa_headdata-account_view      =  co_yes.
+
wa_headdata-account_view      =  co_yes.
*  wa_headdata-cost_view          =  abap_on.
+
*  wa_headdata-cost_view          =  abap_on.
**  wa_headdata-material_external  =  wa_clientdata-material_external.
+
**  wa_headdata-material_external  =  wa_clientdata-material_external.
**  wa_headdata-material_guid      =  wa_clientdata-material_guid.
+
**  wa_headdata-material_guid      =  wa_clientdata-material_guid.
**  wa_headdata-material_version  =  wa_clientdata-material_version.
+
**  wa_headdata-material_version  =  wa_clientdata-material_version.
  
  wa_clientdata_new-MATL_GROUP  = '001'.
+
wa_clientdata_new-MATL_GROUP  = '001'.
  wa_clientdata_new-OLD_MAT_NO  = 'MDF'.
+
wa_clientdata_new-OLD_MAT_NO  = 'MDF'.
  wa_clientdata_new-BASE_UOM    = 'UN'.
+
wa_clientdata_new-BASE_UOM    = 'UN'.
  wa_clientdata_new-NET_WEIGHT  = '1,000'.
+
wa_clientdata_new-NET_WEIGHT  = '1,000'.
  wa_clientdata_new-UNIT_OF_WT  = 'KG'.
+
wa_clientdata_new-UNIT_OF_WT  = 'KG'.
  wa_clientdata_new-DIVISION    = '01'.
+
wa_clientdata_new-DIVISION    = '01'.
  wa_clientdata_new-PROD_HIER  = '0001300003'.
+
wa_clientdata_new-PROD_HIER  = '0001300003'.
  wa_clientdata_new-BATCH_MGMT  = 'X'.
+
wa_clientdata_new-BATCH_MGMT  = 'X'.
  wa_clientdata_new-PVALIDFROM  = SY-DATUM.
+
wa_clientdata_new-PVALIDFROM  = SY-DATUM.
  wa_clientdata_new-ITEM_CAT    = 'NORM'.
+
wa_clientdata_new-ITEM_CAT    = 'NORM'.
  
  wa_clientdatax-MATL_GROUP  = co_yes.
+
wa_clientdatax-MATL_GROUP  = co_yes.
  wa_clientdatax-OLD_MAT_NO  = co_yes.
+
wa_clientdatax-OLD_MAT_NO  = co_yes.
  wa_clientdatax-BASE_UOM    = co_yes.
+
wa_clientdatax-BASE_UOM    = co_yes.
  wa_clientdatax-NET_WEIGHT  = co_yes.
+
wa_clientdatax-NET_WEIGHT  = co_yes.
  wa_clientdatax-UNIT_OF_WT  = co_yes.
+
wa_clientdatax-UNIT_OF_WT  = co_yes.
  wa_clientdatax-DIVISION    = co_yes.
+
wa_clientdatax-DIVISION    = co_yes.
  wa_clientdatax-PROD_HIER  = co_yes .
+
wa_clientdatax-PROD_HIER  = co_yes .
  wa_clientdatax-BATCH_MGMT  = co_yes .
+
wa_clientdatax-BATCH_MGMT  = co_yes .
  wa_clientdatax-PVALIDFROM  = co_yes.
+
wa_clientdatax-PVALIDFROM  = co_yes.
  wa_clientdatax-ITEM_CAT    = co_yes.
+
wa_clientdatax-ITEM_CAT    = co_yes.
  
  wa_salesdata_new-SALES_ORG  = '01'.
+
wa_salesdata_new-SALES_ORG  = '01'.
  wa_salesdata_new-DISTR_CHAN  = 'Z2'.
+
wa_salesdata_new-DISTR_CHAN  = 'Z2'.
  wa_salesdata_new-MATL_STATS  = '1'.
+
wa_salesdata_new-MATL_STATS  = '1'.
  wa_salesdata_new-ITEM_CAT    = 'ZMDF'  .
+
wa_salesdata_new-ITEM_CAT    = 'ZMDF'  .
  wa_salesdata_new-ACCT_ASSGT  = '01'.
+
wa_salesdata_new-ACCT_ASSGT  = '01'.
  
  wa_salesdatax-SALES_ORG  = '01'.
+
wa_salesdatax-SALES_ORG  = '01'.
  wa_salesdatax-DISTR_CHAN  = 'Z2'.
+
wa_salesdatax-DISTR_CHAN  = 'Z2'.
  wa_salesdatax-MATL_STATS  = co_yes.
+
wa_salesdatax-MATL_STATS  = co_yes.
  wa_salesdatax-ITEM_CAT    = co_yes  .
+
wa_salesdatax-ITEM_CAT    = co_yes  .
  wa_salesdatax-ACCT_ASSGT  = co_yes.
+
wa_salesdatax-ACCT_ASSGT  = co_yes.
  
  
  gs_bapi_makt-LANGU  = 'ES'.
+
gs_bapi_makt-LANGU  = 'ES'.
  gs_bapi_makt-MATL_DESC  = 'Campo9'.
+
gs_bapi_makt-MATL_DESC  = 'Campo9'.
  append gs_bapi_makt to gt_bapi_makt.
+
append gs_bapi_makt to gt_bapi_makt.
  
  gs_bapi_marm-ALT_UNIT  = 'UN'.
+
gs_bapi_marm-ALT_UNIT  = 'UN'.
  gs_bapi_marm-GROSS_WT  = '1,000'.
+
gs_bapi_marm-GROSS_WT  = '1,000'.
  append gs_bapi_marm to gt_bapi_marm.
+
append gs_bapi_marm to gt_bapi_marm.
  
  gs_bapi_marmx-ALT_UNIT  = 'UN'.
+
gs_bapi_marmx-ALT_UNIT  = 'UN'.
  gs_bapi_marmx-GROSS_WT  = co_yes.
+
gs_bapi_marmx-GROSS_WT  = co_yes.
  append gs_bapi_marmx to gt_bapi_marmx.
+
append gs_bapi_marmx to gt_bapi_marmx.
  
  
  gs_mlan-DEPCOUNTRY  = 'CL'.
+
gs_mlan-DEPCOUNTRY  = 'CL'.
  gs_mlan-TAX_TYPE_1  = 'ZIVA'.
+
gs_mlan-TAX_TYPE_1  = 'ZIVA'.
  gs_mlan-TAXCLASS_1  = '1'.
+
gs_mlan-TAXCLASS_1  = '1'.
  append gs_mlan to gt_mlan.
+
append gs_mlan to gt_mlan.
  
ENDFORM.                    "fill_data
+
ENDFORM.                    "fill_data</nowiki>

Latest revision as of 22:08, 4 April 2020

*----------------------------------------------------------------------*
***INCLUDE 
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  SEARCH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SEARCH .
 data lt_stpo type TABLE OF stpo.
 data ls_stpo like LINE OF lt_stpo.
 data ld_f.
 data ld_l type i.

 clear: mast,
        knmt,
        kna1-name1,
        makt,
        gd_maktx,
        t001w.

 select single * from kna1 where KUNNR = kna1-kunnr.

 select SINGLE * from t001w where werks = marc-werks.

 loop at gt_comp into gs_comp where matnr is NOT INITIAL.
   SELECT    * APPENDING TABLE lt_stpo FROM  STPO
          WHERE  IDNRK  = gs_comp-MATNR
          AND    MENGE  = gs_comp-menge.
   if sy-subrc <> 0.
* debe encontrarse para todos los comp
     ld_f = co_yes.
     exit.
   endif.

 ENDLOOP.

 case ld_f.
   when co_yes.

   when co_no.
     sort lt_stpo by STLNR.
     delete ADJACENT DUPLICATES FROM lt_stpo COMPARING STLNR.

     ld_l = lines( lt_stpo ).
     if ld_l = 1.
       read TABLE lt_stpo into ls_stpo index 1.
       select single * from mast where stlnr = ls_stpo-stlnr
                           and werks  = marc-werks.
       if sy-subrc = 0.
         select SINGLE * from makt where matnr = mast-matnr
                                     and spras = sy-langu.

         select single * from KNMT where matnr = mast-matnr
                                       and kunnr = kna1-kunnr.
         if sy-subrc = 0.
           select SINGLE maktx into gd_maktx from makt where matnr = knmt-matnr
                         and spras = sy-langu.
         endif.
       endif.

     endif.

 ENDCASE.

ENDFORM.                    " SEARCH
*&---------------------------------------------------------------------*
*&      Form  UPDATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM UPDATE .

 select single * from kna1 where KUNNR = kna1-kunnr.
 select SINGLE * from t001w where werks = marc-werks.

 loop at gt_comp into gs_comp where matnr is NOT INITIAL.
   gd_tabix = sy-tabix.
   select SINGLE maktx into gs_comp-maktx from makt
                       where matnr = gs_comp-matnr
                          and spras = sy-langu.
   if sy-subrc = 0.
     modify gt_comp from gs_comp INDEX gd_tabix.
   endif.

 ENDLOOP.

ENDFORM.                    " UPDATE

*&---------------------------------------------------------------------*
*&      Form  f_crear_material
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM f_crear_material .

 PERFORM fill_data.

 CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
 EXPORTING
   headdata             = wa_headdata
   clientdata             = wa_clientdata_new
   clientdatax          = wa_clientdatax
   SALESDATA            = wa_salesdata_new
   SALESDATAx           = wa_salesdatax
 IMPORTING
   RETURN               = wa_return
 TABLES
   materialdescription  = gt_bapi_makt
   taxclassifications   = gt_mlan          "Tax Data
   unitsofmeasure       = gt_bapi_marm     "Units of measure
   unitsofmeasurex      = gt_bapi_marmx    "Checkbox for Units of measure
   returnmessages       = gt_returnmessages
*      materiallongtext     = it_bapi_mltx
   .

*  if p_test <> 'X'.
 IF wa_return-type <> 'E'.
   CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*      EXPORTING
*        wait   = abap_on
     IMPORTING
       return = wa_return_commit.
 ENDIF.
* endif.

ENDFORM.                    " crear_material

*&---------------------------------------------------------------------*
*&      Form  get_matnr
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->O_MATNR    text
*----------------------------------------------------------------------*
form get_matnr CHANGING o_matnr.
 data: gv_bapireturn1 type bapireturn1,
       gt_bapimatnr like table of BAPIMATINR,
       gs_bapimatnr LIKE LINE OF  gt_bapimatnr.

 CALL FUNCTION 'BAPI_MATERIAL_GETINTNUMBER'
   EXPORTING
     material_type    = '1'
     industry_sector  = 'HAWA'
     required_numbers = 1
   IMPORTING
     return           = gv_bapireturn1
   TABLES
     material_number  = gt_bapimatnr.
 IF gv_bapireturn1-type EQ 'S'.
   READ TABLE gt_bapimatnr INTO gs_bapimatnr INDEX 1.
   IF sy-subrc EQ 0.
     o_matnr = gs_bapimatnr-material.
   ENDIF.
 ENDIF.
ENDFORM.                    "get_matnr
*&---------------------------------------------------------------------*
*&      Form  fill_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form fill_data.
 data gs_bapi_makt like LINE OF gt_bapi_makt.
 data gs_bapi_marm like LINE OF gt_bapi_marm.
 data gs_bapi_marmx like LINE OF gt_bapi_marmx.
 data gs_mlan like LINE OF gt_mlan.

 clear: wa_headdata,
        wa_clientdata_new,
        wa_clientdatax,
        wa_salesdata_new,
        wa_salesdatax,
        gt_bapi_makt[],
        gt_bapi_marm[],
        gt_bapi_marmx[],
        gt_mlan[].

 perform get_matnr CHANGING  wa_headdata-material .
 wa_headdata-ind_sector         =  '1'.
 wa_headdata-matl_type          =  'HAWA'.

 wa_headdata-basic_view         =  co_yes.
 wa_headdata-sales_view         =  co_yes.
 wa_headdata-purchase_view      =  co_yes.
 wa_headdata-mrp_view            =  co_yes.
 wa_headdata-forecast_view      =  co_yes.
*  wa_headdata-work_sched_view    =  abap_on.
*  wa_headdata-prt_view           =  abap_on.
 wa_headdata-storage_view       =  co_yes.    "!
**  wa_headdata-warehouse_view     =  abap_on.
*   wa_headdata-quality_view       =  abap_on.
 wa_headdata-account_view       =  co_yes.
*  wa_headdata-cost_view          =  abap_on.
**  wa_headdata-material_external  =  wa_clientdata-material_external.
**  wa_headdata-material_guid      =  wa_clientdata-material_guid.
**  wa_headdata-material_version   =  wa_clientdata-material_version.

 wa_clientdata_new-MATL_GROUP  = '001'.
 wa_clientdata_new-OLD_MAT_NO  = 'MDF'.
 wa_clientdata_new-BASE_UOM    = 'UN'.
 wa_clientdata_new-NET_WEIGHT  = '1,000'.
 wa_clientdata_new-UNIT_OF_WT  = 'KG'.
 wa_clientdata_new-DIVISION    = '01'.
 wa_clientdata_new-PROD_HIER   = '0001300003'.
 wa_clientdata_new-BATCH_MGMT  = 'X'.
 wa_clientdata_new-PVALIDFROM   = SY-DATUM.
 wa_clientdata_new-ITEM_CAT     = 'NORM'.

 wa_clientdatax-MATL_GROUP  = co_yes.
 wa_clientdatax-OLD_MAT_NO  = co_yes.
 wa_clientdatax-BASE_UOM    = co_yes.
 wa_clientdatax-NET_WEIGHT  = co_yes.
 wa_clientdatax-UNIT_OF_WT  = co_yes.
 wa_clientdatax-DIVISION    = co_yes.
 wa_clientdatax-PROD_HIER   = co_yes .
 wa_clientdatax-BATCH_MGMT  = co_yes .
 wa_clientdatax-PVALIDFROM   = co_yes.
 wa_clientdatax-ITEM_CAT     = co_yes.

 wa_salesdata_new-SALES_ORG   = '01'.
 wa_salesdata_new-DISTR_CHAN  = 'Z2'.
 wa_salesdata_new-MATL_STATS  = '1'.
 wa_salesdata_new-ITEM_CAT    = 'ZMDF'  .
 wa_salesdata_new-ACCT_ASSGT  = '01'.

 wa_salesdatax-SALES_ORG   = '01'.
 wa_salesdatax-DISTR_CHAN  = 'Z2'.
 wa_salesdatax-MATL_STATS  = co_yes.
 wa_salesdatax-ITEM_CAT    = co_yes  .
 wa_salesdatax-ACCT_ASSGT  = co_yes.


 gs_bapi_makt-LANGU   = 'ES'.
 gs_bapi_makt-MATL_DESC  = 'Campo9'.
 append gs_bapi_makt to gt_bapi_makt.

 gs_bapi_marm-ALT_UNIT  = 'UN'.
 gs_bapi_marm-GROSS_WT  = '1,000'.
 append gs_bapi_marm to gt_bapi_marm.

 gs_bapi_marmx-ALT_UNIT  = 'UN'.
 gs_bapi_marmx-GROSS_WT  = co_yes.
 append gs_bapi_marmx to gt_bapi_marmx.


 gs_mlan-DEPCOUNTRY  = 'CL'.
 gs_mlan-TAX_TYPE_1  = 'ZIVA'.
 gs_mlan-TAXCLASS_1  = '1'.
 append gs_mlan to gt_mlan.

ENDFORM.                    "fill_data