Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Modify an database table with BAPI_MATERIAL_SAVEDATA

adrian_mejido
Contributor
0 Likes
2,456

Hi,  I am trying to modify a database table with this BAPI, but I don´t know how I could do that.

I need a function for change the value of a field of my database table. 

My database has many fields and I have two fields which are for the material code, one is the "old material code" and another one which is the "new material code". 

I give to the function " the center", "old material code" and "new material code". The function should find in my table the old material code and then update the value of the new material code.

But I don´t know how I could do this with the BAPI because I don´t know how I could give to the function the parameters.

Thanks for your time.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,503

Hi

Please find the below sample code. Below code is updating delivering plant filed. Similarway you can change new material code.   

CLEAR : wa_header, wa_salesdata, wa_salesdatax, wa_return.

  wa_header-material = wa_mvke-matnr.
  wa_header-sales_view = c_x.

  wa_salesdata-sales_org = wa_mvke-vkorg.
  wa_salesdata-distr_chan = wa_mvke-vtweg.
  wa_salesdata-delyg_plnt = wa_plant-werks.


  wa_salesdatax-sales_org = wa_mvke-vkorg.
  wa_salesdatax-distr_chan = wa_mvke-vtweg.
  wa_salesdatax-delyg_plnt = c_x.

  CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
    EXPORTING
      headdata   = wa_header
      salesdata  = wa_salesdata
      salesdatax = wa_salesdatax
    IMPORTING
      return     = wa_return.
  IF wa_return-type = c_s OR wa_return-type = c_w.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = c_x.

    v_success = v_success + 1.
    CLEAR : wa_output.
    wa_output-matnr = wa_mvke-matnr.
    wa_output-vkorg = wa_mvke-vkorg.
    wa_output-vtweg = wa_mvke-vtweg.
    wa_output-dwerk_old = wa_mvke-dwerk.
    wa_output-dwerk_new = wa_plant-werks.
    wa_output-status = text-019.
    wa_output-msg = text-020.
    APPEND wa_output TO i_output.
    CLEAR wa_output.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    v_error = v_error + 1.
    CLEAR : wa_output.
    wa_output-matnr = wa_mvke-matnr.
    wa_output-vkorg = wa_mvke-vkorg.
    wa_output-vtweg = wa_mvke-vtweg.
    wa_output-dwerk_old = wa_mvke-dwerk.
    wa_output-dwerk_new = wa_plant-werks.
    wa_output-status = text-021.
    CALL FUNCTION 'FORMAT_MESSAGE'
      EXPORTING
        id        = wa_return-id
        lang      = c_en
        no        = wa_return-number
        v1        = wa_return-message_v1
        v2        = wa_return-message_v2
        v3        = wa_return-message_v3
        v4        = wa_return-message_v4
      IMPORTING
        msg       = wa_output-msg
      EXCEPTIONS
        not_found = 1
        OTHERS    = 2.
    IF sy-subrc NE 0.
* Do Nothing
    ENDIF.
    APPEND wa_output TO i_output.
    CLEAR wa_output.
  ENDIF.

8 REPLIES 8
Read only

Former Member
0 Likes
1,503

can you pls share the BAPI name you are using for this

Read only

Former Member
0 Likes
1,503

Hi,

what do you mean my material code (old/new). Is it material number you mean?

If so... you want to create new material number with data of exising material?

Satya.

Read only

Former Member
0 Likes
1,504

Hi

Please find the below sample code. Below code is updating delivering plant filed. Similarway you can change new material code.   

CLEAR : wa_header, wa_salesdata, wa_salesdatax, wa_return.

  wa_header-material = wa_mvke-matnr.
  wa_header-sales_view = c_x.

  wa_salesdata-sales_org = wa_mvke-vkorg.
  wa_salesdata-distr_chan = wa_mvke-vtweg.
  wa_salesdata-delyg_plnt = wa_plant-werks.


  wa_salesdatax-sales_org = wa_mvke-vkorg.
  wa_salesdatax-distr_chan = wa_mvke-vtweg.
  wa_salesdatax-delyg_plnt = c_x.

  CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
    EXPORTING
      headdata   = wa_header
      salesdata  = wa_salesdata
      salesdatax = wa_salesdatax
    IMPORTING
      return     = wa_return.
  IF wa_return-type = c_s OR wa_return-type = c_w.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = c_x.

    v_success = v_success + 1.
    CLEAR : wa_output.
    wa_output-matnr = wa_mvke-matnr.
    wa_output-vkorg = wa_mvke-vkorg.
    wa_output-vtweg = wa_mvke-vtweg.
    wa_output-dwerk_old = wa_mvke-dwerk.
    wa_output-dwerk_new = wa_plant-werks.
    wa_output-status = text-019.
    wa_output-msg = text-020.
    APPEND wa_output TO i_output.
    CLEAR wa_output.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    v_error = v_error + 1.
    CLEAR : wa_output.
    wa_output-matnr = wa_mvke-matnr.
    wa_output-vkorg = wa_mvke-vkorg.
    wa_output-vtweg = wa_mvke-vtweg.
    wa_output-dwerk_old = wa_mvke-dwerk.
    wa_output-dwerk_new = wa_plant-werks.
    wa_output-status = text-021.
    CALL FUNCTION 'FORMAT_MESSAGE'
      EXPORTING
        id        = wa_return-id
        lang      = c_en
        no        = wa_return-number
        v1        = wa_return-message_v1
        v2        = wa_return-message_v2
        v3        = wa_return-message_v3
        v4        = wa_return-message_v4
      IMPORTING
        msg       = wa_output-msg
      EXCEPTIONS
        not_found = 1
        OTHERS    = 2.
    IF sy-subrc NE 0.
* Do Nothing
    ENDIF.
    APPEND wa_output TO i_output.
    CLEAR wa_output.
  ENDIF.

Read only

0 Likes
1,503

First thanks for all your replys, but I think that this is the best for my problem. Can you tell me the name of the table where you are changing the field?

Thanks

Read only

Former Member
0 Likes
1,503

Hi Mejido,

BAPI can be used same as you do with FM , You open the BAPI in se37 and check each and every import parameter structures in it . until you find the required field name that you need to update (in your case 'new material code'  ), pass the value to that structure field of the bapi and execute it .the BAPI updates the new value .

For testing perpose please pass your test data into that bapi SE37 and then code accordingly ,it saves your time .

Regards,

Gafoor

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,503

- Could you explain more you requirement, do you want to "copy", "rename" or "update" the material master data ?
(e.g. : what do you call "old material code", is it a material reference (MATNR), or the field old reference (BISMT) or any field of one material master data.)
- Else look for the data element in the MAR* table, and look for the same data element in the BAPI parameter

Regards,
Raymond

Read only

Former Member
0 Likes
1,503

Hi,

enough documentation is available for this BAPI in Tcode SE37->ur bapi name click on documentation there...click on import/export tabs to find the parameters you need to import to or export from bapi and click on green button in front of each parameter to find its documentaiton by SAP..it would be much helpful to you.

Read only

Former Member
0 Likes
1,503

Try to call the Function Module with the following import & export parameters. It is mandatory to pass the header data. If you carefully see the parameter (clientdata: Client-specific material data), you will find it contains the field OLD_MAT_NO (old material no.), the number under which you have managed the material so far or still manage it, Moreover the parameter (clientdatax :information on update for CLIENTDATA)  also contains the field OLD_MAT_NO (Domain: BAPIUPDATE), which is a flag that you need to populate with 'X to indicate that is used to select fields containing modified values in the parameter.

 

CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
        EXPORTING
          headdata   = la_headdata
          clientdata  = la_clientdata
          clientdatax = la_clientdatax
        IMPORTING
          return     = la_return.

Regards.

Tanmoy

Message was edited by: Tanmoy Mondal