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

using internal table in BAPI

Former Member
0 Likes
1,943

hello friends,

I have a scenario where I have to write a BAPI that accepts an input and exports an internal table with several records.

I am finding it difficult to define an internal table.

When I try to define it in the EXPORT tab, it says that 'Occur n' is missing.

If i try to define it in the SOURCE CODE tab using 'data' command, then i can't export it, since

it says that the internal table definition is already defined.

So I would appreciate if some one can let me know how to doit. If you have a sample BAPI that you can share I would appreciate it..

Thanks

Ram

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,085

Hi Ram

You want an internal table in your export tab. Okay.

To define internal table in export parameters.

You need to go to se11 and select data type radio button and write a z_itab_name and click create

and you would get 3 option and you need to choose table type and while defining you need to write short text and name of the structure with which you want to define your internal table.

And while defining internal table in export tab give z_itab_name in associated type. You shouldn't have any problem.

Besides the given solution you can define your internal table in tables tab and give structure name in associated type or if not available , you can create a z structure depending upon your requirement. but sap doesn't recommend to use tables tab because it adds to confusion, And unless you see the code you can't tell whether it is export or import table.

I hope I'm clear.

With regards

Mandeep

4 REPLIES 4
Read only

Former Member
0 Likes
1,085

check this sample program


  REPORT z_salesorder_create NO STANDARD PAGE HEADING.
* Order Type
  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT 2(20) v_text FOR FIELD p_auart.
  PARAMETERS: p_auart TYPE auart OBLIGATORY  DEFAULT 'ZOR'.
  SELECTION-SCREEN END OF LINE.

* Sales organization
  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT 2(20) v_text1 FOR FIELD p_vkorg.
  PARAMETERS: p_vkorg TYPE vkorg OBLIGATORY  DEFAULT '0081'.
  SELECTION-SCREEN END OF LINE.

* Distribution channel
  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT 2(20) v_text2 FOR FIELD p_vtweg.
  PARAMETERS: p_vtweg TYPE vtweg OBLIGATORY DEFAULT '01'.
  SELECTION-SCREEN END OF LINE.

* Division.
  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT 2(20) v_text3 FOR FIELD p_spart.
  PARAMETERS: p_spart TYPE spart OBLIGATORY default 'RT'.
  SELECTION-SCREEN END OF LINE.


* Sold-to
  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT 2(20) v_text4 FOR FIELD p_sold.
  PARAMETERS: p_sold  TYPE kunnr OBLIGATORY.
  SELECTION-SCREEN END OF LINE.

* Ship-to
  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT 2(20) v_text5 FOR FIELD p_ship.
  PARAMETERS: p_ship  TYPE kunnr OBLIGATORY.
  SELECTION-SCREEN END OF LINE.

  SKIP 1.

* PO Number
  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT 2(20) v_text6 FOR FIELD p_ebeln.
  PARAMETERS: p_ebeln TYPE vbkd-bstkd  OBLIGATORY.
  SELECTION-SCREEN END OF LINE.

* Plant
  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT 2(20) v_text9 FOR FIELD p_plant.
  PARAMETERS: p_plant TYPE werks_d OBLIGATORY DEFAULT '81RT'.
  SELECTION-SCREEN END OF LINE.

*File selection
  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT 2(20) v_text10 FOR FIELD p_infile.
  PARAMETERS: p_infile LIKE rlgrap-filename DEFAULT 'C:\TEMP\SALES.XLS'
                    OBLIGATORY.
  SELECTION-SCREEN END OF LINE.

* Initialization.
INITIALIZATION.
   v_text  = 'Order type'.
  v_text1  = 'Sales Org'.
  v_text2  = 'Distribution channel'.
  v_text3  = 'Division'.
  v_text4  = 'Sold-to'.
  v_text5  = 'Ship-to'.
  v_text6  = 'PO Number'.
  v_text9  = 'Plant'.
  v_text10 = 'Select File'.


  TYPES: BEGIN OF t_record ,
           matnr LIKE  itb-value,  "Material Number
           menge LIKE  itb-value,  "Quantity
           i_text LIKE itb-value,  "Item text
         END OF t_record.

  DATA : line TYPE i.

  DATA: BEGIN OF it_data OCCURS 100,
         matnr TYPE rv45a-mabnr,
         menge TYPE rv45a-kwmeng,
         i_text TYPE char200,
        END OF it_data.


  DATA : BEGIN OF i_vbap OCCURS 0,
           vbeln LIKE  vbap-vbeln,
           posnr LIKE  vbap-posnr,
           text  LIKE  rstxt-txline,
         END OF i_vbap.

* BAPI tables
  DATA: v_vbeln            LIKE vbak-vbeln,
        header             LIKE bapisdhead1,
        headerx            LIKE bapisdhead1x,
        item               LIKE bapisditem  OCCURS 0 WITH HEADER LINE,
        itemx              LIKE bapisditemx OCCURS 0 WITH HEADER LINE,
        partner            LIKE bapipartnr  OCCURS 0 WITH HEADER LINE,
        return             LIKE bapiret2    OCCURS 0 WITH HEADER LINE,
        lt_schedules_inx   TYPE STANDARD TABLE OF bapischdlx
                           WITH HEADER LINE,
        lt_schedules_in    TYPE STANDARD TABLE OF bapischdl
                           WITH HEADER LINE.

START-OF-SELECTION.
  PERFORM read_excel_file.

* Header data
* Sales document type
  header-doc_type = p_auart.
  headerx-doc_type = 'X'.

* Sales organization
  header-sales_org = p_vkorg.
  headerx-sales_org = 'X'.

* Distribution channel
  header-distr_chan  = p_vtweg.
  headerx-distr_chan = 'X'.

* Division
  header-division = p_spart.
  headerx-division = 'X'.
  headerx-updateflag = 'I'.

*po number
  header-purch_no_c = p_ebeln.
  headerx-purch_no_c = 'X'.

* Partner data
* Sold to
  partner-partn_role = 'AG'.
  partner-partn_numb = p_sold.
  APPEND partner.

* Ship to
  partner-partn_role = 'WE'.
  partner-partn_numb = p_ship.
  APPEND partner.
* ITEM DATA
  LOOP AT it_data.
    line = sy-tabix.
    itemx-updateflag = 'I'.
* Material
    item-material = it_data-matnr.
    itemx-material = 'X'.
* Plant
    item-plant    = p_plant.
    itemx-plant   = 'X'.

* Quantity
    item-target_qty = it_data-menge.
    itemx-target_qty = 'X'.
    APPEND item.
    APPEND itemx.

*   Fill schedule lines
    lt_schedules_in-sched_line = line.
    lt_schedules_in-req_qty    = it_data-menge.
    APPEND lt_schedules_in.

*   Fill schedule line flags
    lt_schedules_inx-sched_line  = line.
    lt_schedules_inx-updateflag  = 'X'.
    lt_schedules_inx-req_qty     = 'X'.
    APPEND lt_schedules_inx.
  ENDLOOP.

* Call the BAPI to create the sales order.
  CALL FUNCTION 'BAPI_SALESDOCU_CREATEFROMDATA1'
    EXPORTING
      sales_header_in     = header
      sales_header_inx    = headerx
    IMPORTING
      salesdocument_ex    = v_vbeln
    TABLES
      return              = return
      sales_items_in      = item
      sales_items_inx     = itemx
      sales_schedules_in  = lt_schedules_in
      sales_schedules_inx = lt_schedules_inx
      sales_partners      = partner.

* Check the return table.
  LOOP AT return WHERE type = 'E' OR type = 'A'.
    WRITE :/(72) return-message color 6.
  ENDLOOP.

  IF sy-subrc = 0.
    WRITE: / 'Error in creating document'.
    EXIT.
  ELSE.

* Commit the work.
    COMMIT WORK AND WAIT.
    WRITE: / 'Document ', v_vbeln, ' created'.
  ENDIF.


Read only

Former Member
0 Likes
1,086

Hi Ram

You want an internal table in your export tab. Okay.

To define internal table in export parameters.

You need to go to se11 and select data type radio button and write a z_itab_name and click create

and you would get 3 option and you need to choose table type and while defining you need to write short text and name of the structure with which you want to define your internal table.

And while defining internal table in export tab give z_itab_name in associated type. You shouldn't have any problem.

Besides the given solution you can define your internal table in tables tab and give structure name in associated type or if not available , you can create a z structure depending upon your requirement. but sap doesn't recommend to use tables tab because it adds to confusion, And unless you see the code you can't tell whether it is export or import table.

I hope I'm clear.

With regards

Mandeep

Read only

0 Likes
1,085

Thanks a lot guys for the response. I appreciate it.

Mandeep, I tried creating structure and using that to define in the EXPORT tab as you had suggested.

MY EXPORT TAB LOOKS AS FOLLOWS.

ParameterName Typing AssociatedType Pass Val

ITAB TYPE ZNAMESTRUC (Checked)

But it still gives the same error which says

"ITAB" is not an internal table - the "OCCURS n" specification is missing.

So I cannot use the data statement in the SOURCE tab as follows

data: ITAB like znamestruc occurs 1 with header line.

Because now it says "ITAB" has already been declared.

I am kind of lost here.

Your response will be highly appreciated.

Thanks

Ram

Read only

0 Likes
1,085

did you create table type ZNAMESTRUC.

See if it is a table type, then it shouldn't throw error. but if it is a structure then you need to define you itab at tables paramter .

reply

Mandeep