Skip to Content

Here is how to create a material with reference using BAPI_MATERIAL_GET_ALL and BAPI_MATERIAL_SAVEDATA.

Also contains the logic of how to find out what views to update from the reference material by using the Maintenance status field (PSTAT) from MARC

 

report ZSA_TEST002.

selection-screen: begin of block b1 with frame.

PARAMETERS: p_werks0 LIKE marc-werks DEFAULT ‘7530’,

p_vkorg0 LIKE mvke-vkorg DEFAULT ‘7530’,

p_disnso LIKE mvke-vtweg DEFAULT ’01’,

p_mbrsh LIKE mara-mbrsh DEFAULT ‘M’.

selection-screen: end of block b1.

selection-screen: begin of block b2 with frame.

PARAMETERS: P_MATNR LIKE MARA-MATNR.

PARAMETERS: p_matcfg LIKE mara-matnr DEFAULT ‘Z750-CFGPROD-SALES’.

selection-screen: end of block b2.

start-of-selection.

data: gt_marc type table of marc.

data: gt_mara type table of mara.

DATA : str_clientdata LIKE bapi_mara_ga,

str_plantdata LIKE bapi_marc_ga,

str_forecastparameters LIKE bapi_mpop_ga,

str_planningdata LIKE bapi_mpgd_ga,

str_storagelocationdata LIKE bapi_mard_ga,

str_valuationdata LIKE bapi_mbew_ga,

str_salesdata LIKE bapi_mvke_ga,

str_warehousenumberdata LIKE bapi_mlgn_ga,

str_storagetypedata LIKE bapi_mlgt_ga,

str_prtdata LIKE bapi_mfhm_ga,

str_lifovaluationdata LIKE bapi_myms_ga,

tab_get_all_r TYPE TABLE OF bapireturn.

DATA: BEGIN OF tab_head.

INCLUDE STRUCTURE bapimathead.

DATA: END OF tab_head.

DATA: BEGIN OF tab_plantdata.

INCLUDE STRUCTURE bapi_marc.

DATA: END OF tab_plantdata.

DATA: BEGIN OF tab_plantdatax.

INCLUDE STRUCTURE bapi_marcx.

DATA: END OF tab_plantdatax.

DATA: BEGIN OF tab_client.

INCLUDE STRUCTURE bapi_mara.

DATA: END OF tab_client.

DATA: BEGIN OF tab_clientx.

INCLUDE STRUCTURE bapi_marax.

DATA: END OF tab_clientx.

DATA: BEGIN OF tab_salesdata.

INCLUDE STRUCTURE bapi_mvke.

DATA: END OF tab_salesdata.

DATA: BEGIN OF tab_salesdatax.

INCLUDE STRUCTURE bapi_mvkex.

DATA: END OF tab_salesdatax.

DATA : BEGIN OF tab_return.

INCLUDE STRUCTURE bapiret2 .

DATA : END OF tab_return.

DATA : tab_return_msgs type table of BAPI_MATRETURN2 .

data: tab_forecast like BAPI_MPOP,

tab_forecastx like BAPI_MPOPX,

tab_plangdata like BAPI_MPGD,

tab_plangdatax like BAPI_MPGDX,

tab_stlocdata like BAPI_MARD,

tab_stlocdatax like BAPI_MARDX,

tab_valdata like bapi_mbew,

tab_valdatax like bapi_mbewx,

tab_warehouse like BAPI_MLGN,

tab_warehousex like BAPI_MLGNX,

tab_stortyped like BAPI_MLGT,

tab_stortypedx like BAPI_MLGTX.

data gt_makt type table of bapi_makt.

data: gv_len type i,

view,

counter type sy-index,

gv_i type i,

gv_msg(200).

field-symbols:

<fs_marc> type marc,

<f1> type any,

<f2> type any,

<fms> type BAPI_MATRETURN2,

<fstruct> type any,

<fstructx> type any.

CALL FUNCTION ‘BAPI_MATERIAL_GET_ALL’

EXPORTING

material = p_matcfg

COMP_CODE = ‘L153’

VAL_AREA = ‘7530’

 

* VAL_TYPE =

 

PLANT = p_werks0

 

* STGE_LOC =

 

SALESORG = p_vkorg0

DISTR_CHAN = p_disnso

 

* WHSENUMBER =

* STGE_TYPE =

* LIFO_VALUATION_LEVEL =

 

IMPORTING

CLIENTDATA = str_clientdata

PLANTDATA = str_plantdata

FORECASTPARAMETERS = str_forecastparameters

PLANNINGDATA = str_planningdata

STORAGELOCATIONDATA = str_storagelocationdata

VALUATIONDATA = str_valuationdata

WAREHOUSENUMBERDATA = str_warehousenumberdata

SALESDATA = str_salesdata

STORAGETYPEDATA = str_storagetypedata

PRTDATA = str_prtdata

LIFOVALUATIONDATA = str_lifovaluationdata

TABLES

 

* MATERIALDESCRIPTION =

* UNITSOFMEASURE =

* INTERNATIONALARTNOS =

* MATERIALLONGTEXT =

* TAXCLASSIFICATIONS =

* EXTENSIONOUT =

 

RETURN = tab_get_all_r.

 

* Material views update —

 

select * from marc into table gt_marc

where matnr = p_matcfg

and werks = p_werks0.

if sy-subrc = 0.

read table gt_marc assigning <fs_marc> index 1.

if sy-subrc = 0.

counter = 0. clear view.

gv_i = strlen( <fs_marc>-pstat ).

do gv_i times.

view = <fs_marc>-pstat+counter(sy-index).

if view is initial. exit. endif.

counter = counter + 1.

case view.

when ‘K’.

tab_head-basic_view = view.

when ‘V’.

tab_head-sales_view = view.

when ‘E’.

tab_head-purchase_view = view.

when ‘D’ .

tab_head-mrp_view = view.

when ‘P’.

tab_head-forecast_view = view.

when ‘A’.

tab_head-WORK_SCHED_VIEW = view.

when ‘F’.

tab_head-PRT_VIEW = view.

when ‘L’.

tab_head-storage_view = view.

when ‘S’.

tab_head-warehouse_view = view.

when ‘Q’.

tab_head-quality_view = view.

when ‘B’.

tab_head-account_view = view.

when ‘G’.

tab_head-cost_view = view.

when others.

endcase.

enddo.

endif.

endif.

tab_head-material = p_matnr.

tab_head-ind_sector = str_clientdata-ind_sector.

tab_head-matl_type = str_clientdata-matl_type.

move-corresponding str_clientdata to tab_client.

move-corresponding str_plantdata to tab_plantdata.

move-corresponding str_forecastparameters to tab_forecast.

move-corresponding str_planningdata to tab_plangdata.

move-corresponding str_storagelocationdata to tab_stlocdata.

move-corresponding str_valuationdata to tab_valdata.

move-corresponding str_warehousenumberdata to tab_warehouse.

move-corresponding str_salesdata to tab_salesdata.

move-corresponding str_storagetypedata to tab_stortyped.

 

* Fill the update struktur

 

do .

case sy-index.

when 1.

assign tab_client to <f1>.

assign tab_clientx to <f2>.

perform fill_update_struct using <f1> <f2>.

when 2.

assign tab_plantdata to <f1>.

assign tab_plantdatax to <f2>.

perform fill_update_struct using <f1> <f2>.

when 3.

assign tab_forecast to <f1>.

assign tab_forecastx to <f2>.

perform fill_update_struct using <f1> <f2>.

when 4.

assign tab_plangdata to <f1>.

assign tab_plangdatax to <f2>.

perform fill_update_struct using <f1> <f2>.

when 5.

assign tab_stlocdata to <f1>.

assign tab_stlocdatax to <f2>.

perform fill_update_struct using <f1> <f2>.

when 6.

assign tab_valdata to <f1>.

assign tab_valdatax to <f2>.

perform fill_update_struct using <f1> <f2>.

when 7.

assign tab_warehouse to <f1>.

assign tab_warehousex to <f2>.

perform fill_update_struct using <f1> <f2>.

when 8.

assign tab_salesdata to <f1>.

assign tab_salesdatax to <f2>.

perform fill_update_struct using <f1> <f2>.

when 9.

assign tab_stortyped to <f1>.

assign tab_stortypedx to <f2>.

perform fill_update_struct using <f1> <f2>.

when others.

exit.

endcase.

enddo.

tab_salesdata-sales_org = p_vkorg0.

tab_salesdata-distr_chan = p_disnso.

clear: tab_salesdatax-sales_org,

tab_salesdatax-distr_chan.

tab_salesdatax-sales_org = tab_salesdata-sales_org.

tab_salesdatax-distr_chan = tab_salesdata-distr_chan.

clear tab_plantdatax-plant.

tab_plantdatax-plant = tab_plantdata-plant.

CLEAR tab_valdatax-val_area.

tab_valdatax-val_area = tab_valdata-val_area.

CALL FUNCTION ‘BAPI_MATERIAL_SAVEDATA’

EXPORTING

headdata = tab_head

CLIENTDATA = tab_client

CLIENTDATAX = tab_clientx

PLANTDATA = tab_plantdata

PLANTDATAX = tab_plantdatax

FORECASTPARAMETERS = tab_forecast

FORECASTPARAMETERSX = tab_forecastx

PLANNINGDATA = tab_plangdata

PLANNINGDATAX = tab_plangdatax

STORAGELOCATIONDATA = tab_stlocdata

STORAGELOCATIONDATAX = tab_stlocdatax

VALUATIONDATA = tab_valdata

VALUATIONDATAX = tab_valdatax

WAREHOUSENUMBERDATA = tab_warehouse

WAREHOUSENUMBERDATAX = tab_warehousex

SALESDATA = tab_salesdata

SALESDATAX = tab_salesdatax

STORAGETYPEDATA = tab_stortyped

STORAGETYPEDATAX = tab_stortypedx

* FLAG_ONLINE = ‘ ‘

* FLAG_CAD_CALL = ‘ ‘

 

* NO_DEQUEUE = ‘ ‘

 

IMPORTING

RETURN = tab_return

TABLES

MATERIALDESCRIPTION = gt_makt

 

* UNITSOFMEASURE =

* UNITSOFMEASUREX =

* INTERNATIONALARTNOS =

* MATERIALLONGTEXT =

* TAXCLASSIFICATIONS =

 

RETURNMESSAGES = tab_return_msgs.

 

* PRTDATA =

* PRTDATAX =

* EXTENSIONIN =

* EXTENSIONINX =

 

 

if tab_return-type = ‘S’.

CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’

EXPORTING

WAIT = ‘X’.

else.

CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.

endif.

loop at tab_return_msgs assigning <fms>.

gv_msg = <fms>.

write:/ gv_msg.

endloop.

 

*&———————————————————————*

*& Form fill_update_struct

*&———————————————————————*

* text

*———————————————————————-*

* –>P_<F1> text

* –>P_<F2> text

*———————————————————————-*

 

FORM fill_update_struct USING P_F1 type any

P_F2 type any.

do.

assign component sy-index of structure P_F1 to <fstruct>.

if sy-subrc <> 0. exit. endif.

if <fstruct> is not initial.

assign component sy-index of structure P_F2 to <fstructx>.

if sy-subrc = 0.

<fstructx> = ‘X’.

endif.

endif.

enddo.

ENDFORM. ” fill_update_struct

Disclaimer: Please customize according to your specific requirements.

Thanks

Sameer

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Manfred Fettinger

    The form “fill_update_struct” does not take care of the key-fields of the X-Structures. Those have to contain the keys and not “X” if the value-structure-field is filled!

    This coding fills also the keys and if the field-type = BAPIUPDATE then the flag is set

    class-methods FILL_UPDATE_STRUCT
        changing
          !P_F1 type ANY
          !P_F2 type ANY .
    
    METHOD fill_update_struct.
    	DATA: lr_comp_descr_ref   TYPE REF TO cl_abap_datadescr,
    		  lr_structurex_descr TYPE REF TO cl_abap_structdescr,
    		  ls_components       TYPE LINE OF abap_compdescr_tab.
    
    	FIELD-SYMBOLS: <field>  TYPE any,
    				   <fieldx> TYPE any.
    
    	"Hole Komponenten der X-Struktur
    	lr_structurex_descr ?= cl_abap_structdescr=>describe_by_data( p_f2 ).
    
    	"Loop über die Komponenten
    	LOOP AT lr_structurex_descr->components INTO ls_components.
    
    	  "X-Feld zuweisen und Typ holen
    	  ASSIGN COMPONENT ls_components-name OF STRUCTURE p_f2 TO <fieldx>.
    	  IF sy-subrc = 0.
    		CALL METHOD lr_structurex_descr->get_component_type
    		  EXPORTING
    			p_name                 = ls_components-name
    		  RECEIVING
    			p_descr_ref            = lr_comp_descr_ref
    		  EXCEPTIONS
    			component_not_found    = 1
    			unsupported_input_type = 2
    			OTHERS                 = 3.
    
    		"Wenn X-Feld ein BAPIUPDATE ist, dann X setzen, falls in Wertestruktur Feld nicht initial ist
    		"Wenn X-Feld <> BAPIUPDATE dann Wert aus Struktur setzen (Schlüssel)
    		IF sy-subrc = 0.
    		  ASSIGN COMPONENT ls_components-name OF STRUCTURE p_f1 TO <field>.
    		  IF lr_comp_descr_ref->absolute_name = '\TYPE=BAPIUPDATE'.
    			IF <field> IS NOT INITIAL.
    			  <fieldx> = 'X'.
    			ENDIF.
    		  ELSE.
    			<fieldx> = <field>.
    		  ENDIF.
    		ENDIF.
    	  ENDIF.
    	ENDLOOP.
    ENDMETHOD.
    (0) 

Leave a Reply