Application Development 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: 

RE: USE OF BAPI AND FUNCTIONAL MODULE

Former Member
0 Kudos

Hi,

Can anyone help me out with a good example regarding following BAPI and Functional modules.

1.) BAPI_MATERIAL_SAVEDATA -


>BAPI

2.) SO_NEW_DOCUMENT_ATT_SEND_API -


>FUNCTION MODULE

Thanks,

Naveen

2 REPLIES 2

Former Member
0 Kudos

hi Naveen,

please find below, sample code for 'SO_NEW_DOCUMENT_ATT_SEND_API1'


*&---------------------------------------------------------------------*
*&      Form  SEND_EMAIL_MESSAGE
*&---------------------------------------------------------------------*
*       Send email message
*----------------------------------------------------------------------*
*
FORM send_email_message.
* Fill the document data.
  gd_doc_data-doc_size = 1.

* Populate the subject/generic message attributes
  gd_doc_data-obj_langu = sy-langu.
  gd_doc_data-obj_name  = 'SAPRPT'.
  gd_doc_data-obj_descr = psubject.
  gd_doc_data-sensitivty = 'F'.

* Describe the body of the message
  CLEAR it_packing_list.
  REFRESH it_packing_list.
  it_packing_list-transf_bin = space.
  it_packing_list-head_start = 1.
  it_packing_list-head_num = 0.
  it_packing_list-body_start = 1.
  DESCRIBE TABLE it_message LINES it_packing_list-body_num.
  it_packing_list-doc_type = 'RAW'.
  APPEND it_packing_list.

* Add the recipients email address
  CLEAR it_receivers.
  REFRESH it_receivers.

  LOOP AT s_email.
    it_receivers-receiver = s_email-low.
    it_receivers-rec_type = 'U'.
    it_receivers-com_type = 'INT'.
    it_receivers-notif_del = 'X'.
    it_receivers-notif_ndel = 'X'.
    APPEND it_receivers.
  ENDLOOP.

* Call the FM to post the message to SAPMAIL
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = gd_doc_data
      put_in_outbox              = 'X'
    IMPORTING
      sent_to_all                = gd_sent_all
    TABLES
      packing_list               = it_packing_list
      contents_txt               = it_message
      receivers                  = it_receivers
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      document_type_not_exist    = 3
      operation_no_authorization = 4
      parameter_error            = 5
      x_error                    = 6
      enqueue_error              = 7
      OTHERS                     = 8.

* Store function module return code
  gd_error = sy-subrc.



  COMMIT WORK.
ENDFORM.                    " SEND_EMAIL_MESSAGE

Hope this helps,

Sajan Joseph.

Former Member
0 Kudos

hi,

Check the code.

types : begin of ty_main,
         matnr type bapimathead-material,
         mtart type bapimathead-matl_type,
         mbrsh type bapimathead-ind_sector,
         matkl type bapi_mara-matl_group,
         meins type bapi_mara-base_uom,
         spras type bapi_makt-langu,
         maktx type bapi_makt-matl_desc,
         werks type marc-werks,
         herkl type marc-herkl,
         vkorg type mvke-vkorg,
         vtweg type mvke-vtweg,
       end   of ty_main,
       begin of ty_tab,
         matnr type mara-matnr,
         werks type marc-werks,
       end   of ty_tab,
       begin of ty_error,
         matnr type bapimathead-material,
         mesg  type bapiret2-message,
       end   of ty_error.
data : it_main    type standard table of ty_main  with header line,
       it_makt    type standard table of bapi_makt  with header line,
       it_tab     type standard table of ty_tab with header line,
       it_error   type standard table of ty_error with header line,
       it_mathead type bapimathead,
       it_clientdata  type bapi_mara,
       it_clientdatax type bapi_marax,
       it_plantdata type bapi_marc,
       it_plantdatax  type bapi_marcx,
       it_salesdata type bapi_mvke,
       it_salesdatax type bapi_mvkex,
       it_return  type bapiret2,
       v_count type i,
       v_success type i,
       v_error type i.
selection-screen begin of block b1.
  parameter : P_file type rlgrap-filename obligatory.
selection-screen end   of block b1.
at selection-screen on value-request for p_file.
  CALL FUNCTION 'F4_FILENAME'
   EXPORTING
     PROGRAM_NAME        = SYST-CPROG
     DYNPRO_NUMBER       = SYST-DYNNR
     FIELD_NAME          = ' '
   IMPORTING
     FILE_NAME           = p_file.
start-of-selection.
  perform get_data.
end-of-selection.
  if not it_error[] is initial.
    describe table it_error lines v_error.
  endif.
  perform get_summary.
*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form get_data .
  data : lv_infile type string.
         lv_infile = p_file.
         CALL FUNCTION 'GUI_UPLOAD'
           EXPORTING
             filename                      = lv_infile
            FILETYPE                      = 'ASC'
            HAS_FIELD_SEPARATOR           = 'X'
           TABLES
             data_tab                      = it_tab
          EXCEPTIONS
            FILE_OPEN_ERROR               = 1
            FILE_READ_ERROR               = 2
            NO_BATCH                      = 3
            GUI_REFUSE_FILETRANSFER       = 4
            INVALID_TYPE                  = 5
            NO_AUTHORITY                  = 6
            UNKNOWN_ERROR                 = 7
            BAD_DATA_FORMAT               = 8
            HEADER_NOT_ALLOWED            = 9
            SEPARATOR_NOT_ALLOWED         = 10
            HEADER_TOO_LONG               = 11
            UNKNOWN_DP_ERROR              = 12
            ACCESS_DENIED                 = 13
            DP_OUT_OF_MEMORY              = 14
            DISK_FULL                     = 15
            DP_TIMEOUT                    = 16
            OTHERS                        = 17.
         IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
         else.
           select matnr
                  mtart
                  mbrsh
                  matkl
                  meins
                  spras
                  maktx
                  werks
                  herkl
                  vkorg
                  vtweg
                  into table it_main
                  from  ZANAND_BAPI_VIEW
                  for all entries in it_tab
                  where matnr = it_tab-matnr
                  and   werks = it_tab-werks
                  and   spras = sy-langu.
           if sy-subrc = 0.
             describe table it_main lines v_count.
             perform get_bapi.
           endif.
         ENDIF.
endform.                    " get_data
*&---------------------------------------------------------------------*
*&      Form  get_bapi
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_bapi .
    loop at it_main.
      it_mathead-material = it_main-matnr.
      it_mathead-ind_sector = it_main-mbrsh.
      it_mathead-matl_type = it_main-mtart.
      it_clientdata-matl_group  = it_main-matkl.
      it_clientdata-base_uom = it_main-meins.
      it_clientdatax-matl_group = 'X'.
      it_clientdatax-base_uom = 'X'.
      it_plantdata-plant = it_main-werks.
      it_plantdata-countryori = it_main-herkl.
      it_plantdatax-plant = it_main-werks.
      it_plantdatax-countryori = 'X'.
      it_salesdata-sales_org = it_main-vkorg.
      it_salesdata-distr_chan = it_main-vtweg.
      it_salesdatax-sales_org = it_main-vkorg.
      it_salesdatax-distr_chan = it_main-vtweg.
      it_makt-langu = it_main-spras.
      it_makt-matl_desc = it_main-maktx.
      append it_makt.
      clear it_makt.
      CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
        EXPORTING
          headdata                   = it_mathead
         CLIENTDATA                 = it_cliENTDATA
         CLIENTDATAX                = it_CLIENTDATAX
         PLANTDATA                  = it_PLANTDATA
         PLANTDATAX                 = it_PLANTDATAX
       IMPORTING
         RETURN                     = it_RETURN
       TABLES
         MATERIALDESCRIPTION        = it_makt.
      if it_return-type = 'S'.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
* EXPORTING
*   WAIT          = WAIT
* IMPORTING
*   RETURN        = RETURN.

      else.
        it_error-matnr = it_main-matnr.
        it_error-mesg = it_return-message.
        append it_error.
        clear it_error.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
*         IMPORTING
*           RETURN        = RETURN.
      endif.
      clear : it_main,it_mathead,it_clientdata,it_clientdatax,it_plantdata,it_plantdatax,it_salesdata,it_salesdatax,it_return.
      refresh it_makt.
    endloop.
ENDFORM.                    " get_bapi
*&---------------------------------------------------------------------*
*&      Form  get_summary
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form get_summary .
  v_success = v_count - v_error.
  write : /10 'Total Reocrds: ',v_count,
          /10 'Successful Records: ',v_success.
  if not  v_error is initial.
    write :/10 'Error Records : ',v_error.
    skip 3.
    loop at it_error.
      write :/10 it_error-matnr,
             /10 it_error-mesg.
      clear it_error.
    endloop.
  endif.

endform.                    " get_summary

Regards,

Richa