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

sales order creation using BAPI

Former Member
0 Likes
1,540

Hi,

look at the following code. when i hardcode and pass values values to the bapi, it is saying that sales doument type OR not defined. but if i test the BAPI in se37 with the same values given below it is creating a sales document,

kindly tell me the mistake if any.

DATA: lw_hd1 LIKE bapisdhd1.

DATA: lw_hd1x LIKE bapisdhd1x.

DATA: lw_vbeln LIKE bapivbeln-vbeln.

DATA: lt_itm LIKE bapisditm OCCURS 0 WITH HEADER LINE.

DATA: lt_itmx LIKE bapisditmx OCCURS 0 WITH HEADER LINE.

DATA: lt_pr LIKE bapiparnr OCCURS 0 WITH HEADER LINE.

DATA: lt_ret LIKE bapiret2 OCCURS 0 WITH HEADER LINE.

lw_hd1-doc_type = 'SP'.

lw_hd1-sales_org = '1000'.

lw_hd1-distr_chan = '10'.

lw_hd1-division = '00'.

lw_hd1-purch_no_c = '1111'.

lw_hd1-incoterms1 = 'FH'.

lw_hd1-incoterms2 = 'NEWYARK'.

lw_hd1-pmnttrms = '0002'.

lw_hd1x-updateflag = 'I'.

lw_hd1x-doc_type = 'X'.

lw_hd1x-sales_org = 'X'.

lw_hd1x-distr_chan = 'X'.

lw_hd1x-division = 'X'.

lw_hd1x-purch_no_c = 'X'.

lw_hd1x-incoterms1 = 'X'.

lw_hd1x-incoterms2 = 'X'.

lw_hd1x-pmnttrms = 'X'.

lt_itm-material = '100-500'.

lt_itm-plant = '1000'.

lt_itm-target_qty = '1.000'.

APPEND lt_itm.

lt_itmx-updateflag = 'I'.

lt_itmx-material = 'X'.

lt_itmx-plant = 'X'.

lt_itmx-target_qty = 'X'.

APPEND lt_itmx.

lt_pr-partn_role = 'AG'.

lt_pr-partn_numb = '2401'.

APPEND lt_pr.

CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'

EXPORTING

salesdocumentin = ' '

order_header_in = lw_hd1

order_header_inx = lw_hd1x

  • SENDER =

  • BINARY_RELATIONSHIPTYPE = ' '

    • INT_NUMBER_ASSIGNMENT =

  • BEHAVE_WHEN_ERROR = ' '

  • LOGIC_SWITCH = ' '

  • TESTRUN = ' '

  • CONVERT = ' '

IMPORTING

salesdocument = lw_vbeln

TABLES

return = lt_ret[]

order_items_in = lt_itm[]

order_items_inx = lt_itmx[]

order_partners = lt_pr[]

  • ORDER_SCHEDULES_IN =

  • ORDER_SCHEDULES_INX =

  • ORDER_CONDITIONS_IN =

  • ORDER_CONDITIONS_INX =

  • ORDER_CFGS_REF =

  • ORDER_CFGS_INST =

  • ORDER_CFGS_PART_OF =

  • ORDER_CFGS_VALUE =

  • ORDER_CFGS_BLOB =

  • ORDER_CFGS_VK =

  • ORDER_CFGS_REFINST =

  • ORDER_CCARD =

  • ORDER_TEXT =

  • ORDER_KEYS =

  • EXTENSIONIN =

  • PARTNERADDRESSES =

.

IF lw_vbeln IS NOT INITIAL.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

wait = 'X'.

ENDIF.

LOOP AT lt_ret.

WRITE:/ lt_ret-message.

ENDLOOP.

3 REPLIES 3
Read only

Former Member
0 Likes
824

Hi Makam,

If I understand correctly, you are trying to create order which is of type 'OR'.

I can see one of the statement in code as:

<b>lw_hd1-doc_type = 'SP'.</b>

Instead , for Standard order i.e. doc type OR , it should be:

<b>lw_hd1-doc_type = 'TA'.</b>

Hope this helps.

Regds,

Akshay Bhagwat

Read only

0 Likes
824

Hi akshay, if u use document type 'TA'. is that BAPI returns the slaes document?

Read only

Former Member
0 Likes
824

hi

check with this code

&----


*& Form sub_create_sales_order

&----


  • This subroutine creates the sales order from the file data using the

  • BAPI function module BAPI_SALESORDER_CREATEFROMDAT2 and sends

  • notification to the user for incomplete and unprocessed sales order.

----


  • Paramters: None

----


FORM sub_create_sales_order .

  • local data declaration.

DATA:

  • Internal table for return messages from BAPI FM.

l_t_return TYPE STANDARD TABLE OF bapiret2 INITIAL SIZE 0,

  • Internal table for Sales and Distribution Document Item.

l_t_sd_item TYPE STANDARD TABLE OF bapisditm INITIAL SIZE 0,

  • Internal table for Sales and Distribution Document Item Data

  • Checkbox.

l_t_sd_item_x TYPE STANDARD TABLE OF bapisditmx INITIAL SIZE 0,

  • Internal table for SD Document Partner.

l_t_sd_partner TYPE STANDARD TABLE OF bapiparnr INITIAL SIZE 0,

  • Internal table for SD Doc. Schedule Lines.

l_t_sd_schedules TYPE STANDARD TABLE OF bapischdl INITIAL SIZE 0,

  • Structure for Sales and Distribution Document Header.

l_x_sd_header TYPE bapisdhd1,

  • Structure for Sales and Distribution Document Header Check List.

l_x_sd_header_x TYPE bapisdhd1x,

  • Work area for internal table l_t_sd_item.

l_w_sd_item TYPE bapisditm,

  • Work area for internal table l_t_sd_item_x.

l_w_sd_item_x TYPE bapisditmx,

  • Work area for internal table l_t_sd_partner.

l_w_sd_partner TYPE bapiparnr,

  • Work area for internal table t_infile.

l_w_infile TYPE ty_infile,

  • Work area for internal table l_t_sd_schedules.

l_w_sd_schedules TYPE bapischdl,

  • Valiable for created sales order number.

l_vbeln TYPE vbeln_va, "Sales order number

l_index TYPE sytabix. "Index of current record

  • Populate the SD header data.

l_x_sd_header-sales_org = p_vkorg. "Sales organization

l_x_sd_header-distr_chan = p_vtweg. "Distribution channel

l_x_sd_header-division = p_spart. "Sales division

l_x_sd_header-created_by = c_author. "'MMLS'

  • Populate the SD header data check list.

l_x_sd_header_x-doc_type = c_checked.

l_x_sd_header_x-sales_org = c_checked.

l_x_sd_header_x-distr_chan = c_checked.

l_x_sd_header_x-division = c_checked.

  • Populate SD item data check list.

l_w_sd_item_x-itm_number = c_checked.

l_w_sd_item_x-material = c_checked.

l_w_sd_item_x-batch = c_checked.

l_w_sd_item_x-store_loc = c_checked.

l_w_sd_item_x-plant = c_checked.

APPEND l_w_sd_item_x TO l_t_sd_item_x.

  • For each record populate the BAPI FM parameters and call the BAPI

  • function module BAPI_SALESORDER_CREATEFROMDAT2 to create the sales

  • order and send notification to user for incomplete and unprocessed

  • sales order.

LOOP AT t_infile INTO l_w_infile.

l_index = sy-tabix.

  • Populate SD header data.

IF l_w_infile-dirid EQ c_r1. "R1

l_x_sd_header-doc_type = c_zkb. "ZKB (Order type)

ELSEIF l_w_infile-dirid EQ c_r2. "R2

l_x_sd_header-doc_type = c_ka. "KA (Order type)

ENDIF.

  • Populate SD partner data.

PERFORM sub_populate_partner USING l_w_infile-dicus

CHANGING l_w_sd_partner-partn_numb.

l_w_sd_partner-partn_role = c_sold_to_party. "AG

APPEND l_w_sd_partner TO l_t_sd_partner.

CLEAR l_w_sd_partner.

PERFORM sub_populate_partner USING l_w_infile-dienp

CHANGING l_w_sd_partner-partn_numb.

l_w_sd_partner-partn_role = c_sp_stock_partner."SB

APPEND l_w_sd_partner TO l_t_sd_partner.

  • Populate SD item data

l_w_sd_item_x-itm_number = 10. "Item number

l_w_sd_item-material = l_w_infile-dimdel. "Material Number

l_w_sd_item-batch = l_w_infile-disal+0(10)."Batch number

l_w_sd_item-store_loc = l_w_infile-dimcu. "Storage location

l_w_sd_item-plant = c_werks. "Plant(MMLS)'1115'

l_w_sd_item-created_by = c_author. "Created by

APPEND l_w_sd_item TO l_t_sd_item.

  • Populating the sales order schedule line item data

l_w_sd_schedules-itm_number = 10 . "Item number

l_w_sd_schedules-req_qty = l_w_infile-diqty. "Quantity

APPEND l_w_sd_schedules TO l_t_sd_schedules.

  • Call the BAPI function module for sales order creation.

CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'

EXPORTING

order_header_in = l_x_sd_header

order_header_inx = l_x_sd_header_x

IMPORTING

salesdocument = l_vbeln

TABLES

return = l_t_return

order_items_in = l_t_sd_item

order_items_inx = l_t_sd_item_x

order_partners = l_t_sd_partner

order_schedules_in = l_t_sd_schedules.

  • Check the status of sales order creation and in case of error built

  • error log.

PERFORM sub_chk_sales_order_status TABLES l_t_return

USING l_index

l_vbeln.

  • If sales order is created successfully then add serial number for

  • the item to the sales order created.

IF l_vbeln IS NOT INITIAL.

PERFORM sub_add_sernr_to_sales_order USING l_vbeln

l_w_sd_item-batch

l_index.

ENDIF.

  • Clear all work areas.

CLEAR: l_w_sd_item,l_w_infile,l_vbeln,l_w_sd_schedules.

REFRESH: l_t_sd_item, l_t_sd_partner, l_t_sd_schedules.

ENDLOOP.

  • If any error occurred during sales order creation then send user a

  • mail with the record information for notification.

CHECK t_error_log[] IS NOT INITIAL.

PERFORM sub_notify_user.

  • Release the memory occupied by global internal tables.

FREE:

t_infile,

t_bdcdata,

t_mail_body.

ENDFORM. " sub_create_sales_order

&----


*& Form sub_populate_partner

&----


  • This subroutine call the conversion exit routine

  • CONVERSION_EXIT_ALPHA_INPUT to convert external customer to internal

  • customer

----


  • Parameters:

  • --> L_EXTN_KUNNR Customer number from file

  • <--> L_PARTN_NUMB Customer number in internal format

----


FORM sub_populate_partner USING l_extn_kunnr TYPE c

CHANGING l_partn_numb TYPE kunnr.

  • Conversion exit ALPHA, external->internal

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = l_extn_kunnr

IMPORTING

output = l_partn_numb.

ENDFORM. "sub_populate_partner

&----


*& Form sub_chk_sales_order_status

&----


  • This subroutine checks for the sales order creation status: if no

  • error found the commit will be executed otherwise record will be

  • marked as record in error.

----


  • Parameters:

  • --> L_T_RETURN[] Return messages from BAPI

  • --> L_INDEX Index of record in dataset

  • --> L_VBELN Sales order number

----


FORM sub_chk_sales_order_status TABLES l_t_return STRUCTURE bapiret2

USING l_index TYPE sytabix

l_vbeln TYPE vbeln_va.

  • Local data declaration.

DATA:

  • Internal table for BAPI return message.

l_t_temp_return TYPE STANDARD TABLE OF bapiret2 INITIAL SIZE 0.

l_t_temp_return[] = l_t_return[].

  • Check for success of sales order creation.

DELETE l_t_temp_return WHERE type EQ c_success OR

type EQ c_information.

  • If no data is left then sales order successfully created.

IF l_t_temp_return[] IS INITIAL.

  • Commit work and exit from the subroutine.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

wait = c_checked.

  • Populate the report log internal table with the record.

PERFORM sub_built_log_report TABLES l_t_return

USING l_index

c_success

l_vbeln.

ELSE.

  • Now check for incomplete sales order.

REFRESH l_t_temp_return.

l_t_temp_return[] = l_t_return[].

  • Remove all success and warning messages.

DELETE l_t_temp_return WHERE type EQ c_success OR

type EQ c_information OR

type EQ c_warning.

  • If no data is left then sales order is created but incomplete.

IF l_t_temp_return[] IS INITIAL.

  • Commit work and exit from the subroutine.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

wait = c_checked.

  • Put the record in error log and send notification to the user.

PERFORM sub_built_log_report TABLES l_t_return

USING l_index

c_warning

l_vbeln.

ELSE.

  • If l_t_temp_return has the data after removing all messages

  • other than of type 'E' and 'A' then creation of sales order has

  • failed.Put the record in error log and send notification to the

  • user and exit from subroutine without committing.

PERFORM sub_built_log_report TABLES l_t_temp_return

USING l_index

c_error

space.

ENDIF.

ENDIF.

ENDFORM. " sub_chk_sales_order_status

&----


*& Form sub_add_sernr_to_sales_order

&----


  • This subroutine calls the VA02 (Change sales order) to add serial

  • number for the item to sales order created.

----


  • Parameters:

  • --> L_VBELN Sales order number

  • --> L_SERNR Serial number to be added

  • --> L_INDEX Index of the record being processed

----


FORM sub_add_sernr_to_sales_order USING l_vbeln TYPE vbeln_va

l_sernr TYPE charg_d

l_index TYPE sytabix.

  • Local data declaration

DATA:

  • Internal table used for getting the bdc messages during the call

  • transaction to VA02.

l_t_bdcmsgcoll TYPE STANDARD TABLE OF bdcmsgcoll INITIAL SIZE 0.

REFRESH t_bdcdata.

  • Populate the BDC table with the sales order and serial number for the

  • transaction VA02.

PERFORM sub_populate_bdc_table USING l_vbeln

l_sernr.

  • Call transaction VA02 (change sales order) for adding serial number to

  • the item

CALL TRANSACTION c_transaction_code

USING t_bdcdata

MODE c_display_mode

UPDATE c_update_mode

MESSAGES INTO l_t_bdcmsgcoll.

  • Append the record to error log internal table t_error_log if it has

  • failed to add the serial number to the sales order.

PERFORM sub_log_error TABLES l_t_bdcmsgcoll

USING l_index

l_vbeln.

ENDFORM. "sub_add_sernr_to_sales_order

&----


*& Form sub_notify_user

&----


  • This subroutine sends the error record information to the user via

  • email.

----


  • Parameters: None

----


FORM sub_notify_user .

  • Local data declaration.

DATA:

l_w_content TYPE solisti1. "Mail body heading.

  • Prepare the heading of mail subject.

WRITE text-012 TO l_w_content+2. "Row ID

WRITE c_vline TO l_w_content+9. "Vertical bar ' | '

WRITE text-013 TO l_w_content+12. "Status

WRITE c_vline TO l_w_content+19. "Vertical bar ' | '

WRITE text-004 TO l_w_content+22. "Sales order

WRITE c_vline TO l_w_content+32. "Vertical bar ' | '

WRITE text-006 TO l_w_content+34. "Message

  • The header of the report to the mail body

INSERT l_w_content INTO t_mail_body INDEX 1.

  • Function module to send email to the user.

CALL FUNCTION 'ZXCAP_SEND_MAIL'

EXPORTING

i_devid = c_devid

i_subject = text-003

TABLES

t_body = t_mail_body

EXCEPTIONS

invalid_id = 1

mail_not_sent = 2

OTHERS = 3.

IF sy-subrc <> 0.

  • Set the error flag for error in sending mails.

g_mail_failed = c_checked.

ENDIF.

ENDFORM. " sub_notify_user