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: 

BAPI_SALESORDER_CREATEFROMDAT2 Changing Material Number

abdelrahman-emam
Newcomer
0 Kudos
692

Hello everyone,
I have been working on a way to batch entry data for va01, stumbling across the bapi BAPI_SALESORDER_CREATEFROMDAT2, I have created a program for it, however there's a slight issue,
I made loops inside the code so that it could accept multiple lines after reading an excel file, and it reads them properly. However when it comes to the bapi call, weird behaviour happens as the bapi enters records that are different from what it takes in the structure in the matnr field.

Here's a snippet for the subroutine that calls the bapi.

The problem occurs in order_items_in table, you'll also find a snapshot of what the data that is being entered looks like

FORM create_order .
CLEAR : gsdata_itab.
DATA: GTDATA2_ITAB LIKE GTDATA_ITAB.
gtdata2_itab = gtdata_itab.testheader.txttestdata.txt
LOOP AT gtdata_itab INTO gsdata_itab.
READ TABLE gtdata2_itab into gsdata_itab index sy-tabix.
at new vbeln.
clear gshd_itab.
READ TABLE gthd_itab WITH KEY vbeln = gsdata_itab-vbeln INTO gshd_itab BINARY SEARCH.
CLEAR: order_header_in, order_header_inx, order_partners[], order_partners.
order_header_in-doc_type = gshd_itab-order_type.
order_header_in-sales_org = gshd_itab-sorg.
order_header_in-distr_chan = gshd_itab-dch.
order_header_in-division = gshd_itab-div.
order_header_in-purch_no_c = gshd_itab-bstkd.
order_header_inx-updateflag = 'I'.
order_header_inx-doc_type = 'X'.
order_header_inx-sales_org = 'X'.
order_header_inx-distr_chan = 'X'.
order_header_inx-division = 'X'.
order_header_inx-purch_no_c = 'X'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = gshd_itab-kunnr
IMPORTING
OUTPUT = gshd_itab-kunnr.
order_partners-partn_role = 'AG'.
order_partners-partn_numb = gshd_itab-kunnr.
APPEND order_partners.
CLEAR: order_partners.
endat.
gv_item = gv_item + 10.
order_items_in-itm_number = gv_item.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = gsdata_itab-matnr
IMPORTING
OUTPUT = gsdata_itab-matnr.
unpack gsdata_itab-matnr to matnr.
order_items_in-material = matnr.
CONDENSE gsdata_itab-qty.
order_items_in-target_qty = gsdata_itab-qty.
order_items_in-plant = gsdata_itab-plant.
order_items_in-store_loc = gsdata_itab-stloc.
APPEND order_items_in.
CLEAR: order_items_in.
order_items_inx-itm_number = gv_item.
order_items_inx-material = 'X'.
order_items_inx-target_qty = 'X'.
order_items_inx-plant = 'X'.
order_items_inx-store_loc = 'X'.
APPEND order_items_inx.
CLEAR: order_items_inx.
order_schedules_in-itm_number = gv_item.
order_schedules_in-req_qty = gsdata_itab-qty.
APPEND order_schedules_in.
CLEAR order_schedules_in.
order_schedules_inx-itm_number = gv_item.
order_schedules_inx-req_qty = 'X'.
APPEND order_schedules_inx.
CLEAR order_schedules_inx.
order_conditions_in-itm_number = gv_item.
order_conditions_in-cond_type = gsdata_itab-kschl.
CONDENSE gsdata_itab-price.
order_conditions_in-cond_value = ( gsdata_itab-price / 10 ). "dividing by 10 here cause bapi multiplies by 10 for some reason
APPEND order_conditions_in.
clear order_conditions_in.
order_conditions_in-itm_number = gv_item.
order_conditions_inx-cond_type = gsdata_itab-kschl.
order_conditions_inx-cond_value = 'X'.
APPEND order_conditions_inx.
clear order_conditions_inx.
CLEAR gsdata_itab.
at end of vbeln.
CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
EXPORTING
order_header_in = order_header_in
ORDER_HEADER_INX = order_header_inx
IMPORTING
SALESDOCUMENT = SALESDOCUMENT
TABLES
RETURN = RETURN
ORDER_ITEMS_IN = ORDER_ITEMS_IN
ORDER_ITEMS_INX = ORDER_ITEMS_INX
order_partners = order_partners
ORDER_SCHEDULES_IN = ORDER_SCHEDULES_IN
ORDER_SCHEDULES_INX = ORDER_SCHEDULES_INX
ORDER_CONDITIONS_IN = ORDER_CONDITIONS_IN
ORDER_CONDITIONS_INX = ORDER_CONDITIONS_INX
.
if SALESDOCUMENT IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
write: SALESDOCUMENT.
else.
write: / 'Issues in order creation.'.
endif.
CLEAR: ORDER_ITEMS_IN[], order_header_in, order_header_inx, ORDER_ITEMS_INX[], ORDER_SCHEDULES_IN[], ORDER_SCHEDULES_INX[], order_partners[], ORDER_CONDITIONS_IN[],ORDER_CONDITIONS_INX[], RETURN[].
endat.
ENDLOOP.
6 REPLIES 6

Sandra_Rossi
Active Contributor
0 Kudos
608

Please edit your question (Actions>Edit), select your code and press the button [CODE], which makes the code appear colored/indented, it'll be easier for people to look at it. Thanks!

(please also remove the useless commented lines)

NB: you may also provide input data as ABAP code to help people (Eclipse debugger provides the tool to turn internal table data into equivalent ABAP code initialization)

Also please explain the original problem, not your conclusion (XY problem - Wikipedia)

Sandra_Rossi
Active Contributor
0 Kudos
608

Pretty print also helps:

FORM create_order.
  DATA: gtdata2_itab LIKE gtdata_itab.
  CLEAR : gsdata_itab.
  gtdata2_itab = gtdata_itab."testheader.txttestdata.txt
  LOOP AT gtdata_itab INTO gsdata_itab.
    READ TABLE gtdata2_itab INTO gsdata_itab INDEX sy-tabix.
    AT NEW vbeln.
      CLEAR gshd_itab.
      READ TABLE gthd_itab WITH KEY vbeln = gsdata_itab-vbeln INTO gshd_itab BINARY SEARCH.
      CLEAR: order_header_in, order_header_inx, order_partners[], order_partners.
      order_header_in-doc_type   = gshd_itab-order_type.
      order_header_in-sales_org  = gshd_itab-sorg.
      order_header_in-distr_chan = gshd_itab-dch.
      order_header_in-division   = gshd_itab-div.
      order_header_in-purch_no_c = gshd_itab-bstkd.
      order_header_inx-updateflag = 'I'.
      order_header_inx-doc_type   = 'X'.
      order_header_inx-sales_org  = 'X'.
      order_header_inx-distr_chan = 'X'.
      order_header_inx-division   = 'X'.
      order_header_inx-purch_no_c = 'X'.
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING input  = gshd_itab-kunnr
        IMPORTING output = gshd_itab-kunnr.
      order_partners-partn_role = 'AG'.
      order_partners-partn_numb = gshd_itab-kunnr.
      APPEND order_partners.
      CLEAR: order_partners.
    ENDAT.
    gv_item = gv_item + 10.
    order_items_in-itm_number = gv_item.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING input  = gsdata_itab-matnr
      IMPORTING output = gsdata_itab-matnr.
    UNPACK gsdata_itab-matnr TO matnr.
    order_items_in-material = matnr.
    CONDENSE gsdata_itab-qty.
    order_items_in-target_qty = gsdata_itab-qty.
    order_items_in-plant      = gsdata_itab-plant.
    order_items_in-store_loc  = gsdata_itab-stloc.
    APPEND order_items_in.
    CLEAR: order_items_in.
    order_items_inx-itm_number = gv_item.
    order_items_inx-material   = 'X'.
    order_items_inx-target_qty = 'X'.
    order_items_inx-plant      = 'X'.
    order_items_inx-store_loc  = 'X'.
    APPEND order_items_inx.
    CLEAR: order_items_inx.
    order_schedules_in-itm_number = gv_item.
    order_schedules_in-req_qty    = gsdata_itab-qty.
    APPEND order_schedules_in.
    CLEAR order_schedules_in.
    order_schedules_inx-itm_number = gv_item.
    order_schedules_inx-req_qty    = 'X'.
    APPEND order_schedules_inx.
    CLEAR order_schedules_inx.
    order_conditions_in-itm_number = gv_item.
    order_conditions_in-cond_type  = gsdata_itab-kschl.
    CONDENSE gsdata_itab-price.
    order_conditions_in-cond_value = ( gsdata_itab-price / 10 ). "dividing by 10 here cause bapi multiplies by 10 for some reason
    APPEND order_conditions_in.
    CLEAR order_conditions_in.
    order_conditions_in-itm_number = gv_item.
    order_conditions_inx-cond_type  = gsdata_itab-kschl.
    order_conditions_inx-cond_value = 'X'.
    APPEND order_conditions_inx.
    CLEAR order_conditions_inx.
    CLEAR gsdata_itab.
    AT END OF vbeln.
      CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
        EXPORTING order_header_in      = order_header_in
                  order_header_inx     = order_header_inx
        IMPORTING salesdocument        = salesdocument
        TABLES    return               = return
                  order_items_in       = order_items_in
                  order_items_inx      = order_items_inx
                  order_partners       = order_partners
                  order_schedules_in   = order_schedules_in
                  order_schedules_inx  = order_schedules_inx
                  order_conditions_in  = order_conditions_in
                  order_conditions_inx = order_conditions_inx.
      IF salesdocument IS NOT INITIAL.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING wait = 'X'.
        WRITE: salesdocument.
      ELSE.
        WRITE: / 'Issues in order creation.'.
      ENDIF.
      CLEAR: order_items_in[], order_header_in, order_header_inx, order_items_inx[], order_schedules_in[], order_schedules_inx[], order_partners[], order_conditions_in[],order_conditions_inx[], return[].
    ENDAT.
  ENDLOOP.
ENDFORM.

Sandra_Rossi
Active Contributor
0 Kudos
608

testdata.txt

Sales Document  Item  Material  Order Quantity  Plant  Storage Location  condition  Net price
68440           10    5001801   100             1000   DM51              ZPR0       180    
68441           10    5002101   500             1200   JD55              ZPR0       110    
68442           10    5001402   18              1100   RY70              ZPR0       296    
68442           20    5000402   18              1100   RY70              ZPR0       265    
68443           10    5002101   600             1200   JD55              ZPR0       110    
68444           10    5000801   200             1000   DM51              ZPR0       193    
68444           20    5001403   400             1000   DM52              ZPR0       270    

testheader.txt

Sales Document  Created on  Sales Document Type  Sales Organization  Distribution Channel  Division  Sold-to party  Purchase order no.
68440           20240102    ZOR                  1000                10                    01        100000038      1
68441           20240102    ZOR                  1200                20                    01        100001051      PO:112269
68442           20240102    ZOR                  1100                30                    01        100001051      1
68443           20240102    ZOR                  1200                20                    01        100000038      PO:112229
68444           20240102    ZOR                  1000                10                    01        100001051      1

Sandra_Rossi
Active Contributor
0 Kudos
608

Rather than telling "weird behaviour happens", could you tell the exact symptom? (the result you get, not your feeling)

Sandra_Rossi
Active Contributor
0 Kudos
608

NB

This is probably NOT the answer to your question, but:

For MATNR, theoretically, CONVERSION_EXIT_ALPHA_OUTPUT and UNPACK are wrong, you should use either CONVERSION_EXIT_MATNR_OUTPUT and CONVERSION_EXIT_MATNR_INPUT respectively because they may give different results, but of course calling both at the same time is something that is probably not requested, only one should be called.

The BAPI expects the values in internal format, you may use BAPI_CONVERSION_EXT2INT1 if needed, except for currency code, country code, language code, unit of measure, currency amount which must be in external format.

RAF
Active Contributor
0 Kudos
608

Hi,

if you are on S/4 better use

and also check the long fields (MATERIAL_LONG..)

Kind regards

Robert