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

BAPI Sales order create with reference

Former Member
0 Likes
6,725

Hi

I need to use BAPI to create sales order with reference to Quantity Contract. The line item material quantity would be less than or equal to quantity contract quantity.

Sales order needs to be created with new quantity.

Quantity contract quantity change needs to be maintained.

Document flow needs to be maintained.

Is any one aware if this is possible using BAPI_SALESORDER_CREATEFROMDAT2. If yes, how?

Any insights would be highly appreciated.

12 REPLIES 12
Read only

Former Member
0 Likes
3,398

Hi Maninder,

I have on occasion had to create a sales order with reference to a quotation or another document

I don't think that BAPI_SALESORDER_CREATEFROMDAT2 will allow you to do that. However, a workaround that I have used before is to simply read the quotation or other document into an internal table, then populate the parameters for the BAPI from the data I've read.

It's a little cumbersome if the documents have lots of line items, but it seems to work until that functionality is available.

Hope this helps.

Regards,

Brent

Read only

0 Likes
3,398

I am aware of that route and have tried it too. The only glitch there is the document flow. The sales order will be created correctly, but the document flow will not be maintained.

If you refer to note 370988, we see that it can be done only for Quantity Contract.

I have been able to create sales order with reference to quantity contract, but am not able to change material quantity.

Any help would be great.

Read only

0 Likes
3,398

Hi All ,Plz help me about this i have got struck here i am unable to find the suitable bapi for KP26 t.code which i find one B.Obj BUS1031 to create but i need add some fields. plz any on help me out for this .

do you have any sample program for kp26 t.code , plz post it .

Regards

Utham.

Read only

0 Likes
3,398

Hi All I need to know the BAPI interface to upload the data into KP26 Co Activity type /Price Planning

I have these fields to create entries in KP26 t.code.

The Fields are as follows:

Layout (could use the standard layout 1-201)

1.Version -- SP1

2. Period from / to -- 1 to 12

3. Fiscal Year - 2006

4. Cost Center - 1614003999

5. Activity type-C10003

6. Fixed price-1.00

7. Variable price----0.98

8. Price unit--- 100

PLease any body help me which BAPI is suitable for KP26 T.Code .. Its Urgent ...

Regards

Purushotham.Ineni.

Read only

former_member221770
Contributor
0 Likes
3,398

Maninder,

I have been able to create a Sales order with reference to a Quantity contract via the BAPI.

try adding this to your code (where you populate the BAPI Item Table):

tbl_bapisditm-ref_doc = g_contract_no.

tbl_bapisditm-doc_it = g_contract_itm.

tbl_bapisditm-doc_ca = 'G'. "G = Contract

and don't forget:

tbl_bapisditmx-ref_doc = 'X'.

tbl_bapisditmx-doc_it = 'X'.

tbl_bapisditmx-doc_ca = 'X'.

Sales Order will be created with Contract in Doc Flow.

Contract Quantity is updated and if there is insufficient qty in Contract, Sales Order will not be created.

The only problem I have now is that the Pricing Conditions don't come across. Still working to find out why.

Hope this helps in the mean time.

Cheers,

Pat.

Read only

0 Likes
3,398

Hey Pat

Thanks for your response. I had already figured that out and was able to create sales order with reference to Qty Contract. In my case it is picking up the Pricing conditions too. Are you sending in the Schedule line information ?

Maninder

Read only

0 Likes
3,398

Hi Maninder,

Yes I am putting in the Schedule lines. The problem I get is that if the contract has some discounts or other additional Condition types which are entered in manually, they are not being copied across into the new Sales Order. But if you create the Sales Order manually they do come across.

Please let me know if you are getting these additional condition types across successfully.

Cheers,

Pat.

Read only

0 Likes
3,398

Hi Pat

I was wondering whether you find a solution to your problem, where the manual pricing conditions were not getting carried over from qty contract to sales order.

I am facing the exact same problem and any inputs would be highly appreciated.

Maninder

Read only

0 Likes
3,398

Hi Maninder,

I could not get the BAPI to create a sales order with reference to a quantity contract and still have the manual pricing conditions carry across. So I have used a little work around, it's a bit clumsy, but it works.

First create the Sales Order via BAPI_SALESORDER_CREATEFROMDAT2 in the normal way. Then, get the relevant Contract Line Items' Condition Records and call BAPI_SALESORDER_CHANGE for the newly created ales Order (do not use BAPI_SALESORDER_GETDETAILEDLIST to get Condition Records as this populates some Function Group Structures and causes BAPI_SALESORDER_CHANGE to fail - rather hit KONV to get the Conditions and then T685A to get the Pricing Condition Master data, then pass this info into MAP_INT_TO_EXT_STRUCTURE which maps the data into the correct structure format for BAPI_SALESORDER_CHANGE).

Hope this helps.

Cheers,

Pat.

Read only

0 Likes
3,398

Hi Pat

that was a very helpful answer. Unfortunately i dont understand whether we are not using the BAPI correctly to populate the correct condition types, or does the BAPI not have the functionality.

Anyways, your work around sounds like a plan to me; will it be possible for you to send me a snippet of your code to see the correct mapping.

I appreciate your help.

Maninder

Read only

0 Likes
3,398

Hi Maninder,

The BAPI does not allow the Pricing Conditions to be sourced from the Contract. I debugged the BAPI and it uses a piece of code (don't recall which subroutine though) from SAPMV45A which is relevant for dialog processing where it reads from the VBAP and *VBAP structures. Well this structure is only populated when you are using the Front End transaction (VA41, VA42, etc). Because this structure is empty, pricing conditions do not flow across.

Here is a little prototype program I whipped up to test my method of creating a Sales order and then changing the Sales Order:


REPORT  ypat_salesorder_create LINE-SIZE 256.

* This program will create a ZOC Sales Order with Reference to Contract
* 20000720 / 10. This will be done via BAPI. There are 2 BAPi calls,
* BAPI_SALESORDER_CREATEFROMDAT2 and BAPI_SALESORDER_CHANGE. We need to
* perform 2 BAPI calls as the BAPI to create the Sales Order does not
* bring across the Pricing Conditions properly. To get around this we
* create the Order with reference to the Contract, then we get the
* related Pricing Conditions from the COntract and then update the newly
* created Order.

*-----------------------------------------------------------------------
* Constants
*-----------------------------------------------------------------------
CONSTANTS: c_contract  LIKE vbak-vbeln VALUE '0020000720',
           c_item      LIKE vbap-posnr VALUE '000010'.

*-----------------------------------------------------------------------
* Structures
*-----------------------------------------------------------------------
* Structure to hold BAPI Header
DATA: st_bapisdhd1   LIKE bapisdhd1.

*-----------------------------------------------------------------------
* Internal Tables
*-----------------------------------------------------------------------
* Sales Order Create BAPI Return Messages
DATA: tbl_return     TYPE STANDARD TABLE OF bapiret2
                     WITH HEADER LINE.

* Sales Order BAPI Line Item
DATA: tbl_bapisditm  TYPE STANDARD TABLE OF bapisditm
                     WITH HEADER LINE.
* Sales Order BAPI Line Item
DATA: tbl_bapisditmx  TYPE STANDARD TABLE OF bapisditmx
                     WITH HEADER LINE.

* Sales Order BAPI Pricing Conditions
DATA: tbl_bapicond   TYPE STANDARD TABLE OF bapicond
                     WITH HEADER LINE.

* Sales Order BAPI Partner Functions
DATA: tbl_bapiparnr  TYPE STANDARD TABLE OF bapiparnr
                     WITH HEADER LINE.

* Sales Order BAPI Schedule Lines
DATA: tbl_bapischdl  TYPE STANDARD TABLE OF bapischdl
                     WITH HEADER LINE.

* Sales Order BAPI Schedule Lines
DATA: tbl_bapischdlx TYPE STANDARD TABLE OF bapischdlx
                     WITH HEADER LINE.

* Sales Order BAPI Pricing Conditions
DATA: tbl_bapicondx    TYPE STANDARD TABLE OF bapicondx
                       WITH HEADER LINE.

* Customer Enhancement for VBAK, VBAP, VBEP
DATA: tbl_bapiparex    TYPE STANDARD TABLE OF bapiparex
                       WITH HEADER LINE.

* Table to hold BAPI Detail Conditions
DATA: tbl_bapisdcond   TYPE STANDARD TABLE OF bapisdcond
                       WITH HEADER LINE.

* Table to hold Return Messages from Sales Order Change BAPI

DATA: tbl_return_chg  TYPE STANDARD TABLE OF bapiret2
                      WITH HEADER LINE.

*-----------------------------------------------------------------------
* Variables
*-----------------------------------------------------------------------
DATA: g_vbeln_created   LIKE vbak-vbeln,
      g_valid_contract  TYPE c,
      g_cont_price_date TYPE d.

*-----------------------------------------------------------------------
* Start of Selection
*-----------------------------------------------------------------------
START-OF-SELECTION.

  PERFORM validate_contract.
  PERFORM create_sales_ord.
  PERFORM update_sales_ord.

*-----------------------------------------------------------------------
* End of Selection
*-----------------------------------------------------------------------
END-OF-SELECTION.

  PERFORM write_output_report.

*-----------------------------------------------------------------------
* Subroutines
*-----------------------------------------------------------------------
*&---------------------------------------------------------------------*
*&      Form  create_sales_ord
*&---------------------------------------------------------------------*
*       Create the Sales Order
*----------------------------------------------------------------------*
FORM create_sales_ord .

  PERFORM populate_bapi_tables.
  PERFORM call_create_sales_ord_bapi.

ENDFORM.                    " create_sales_ord
*&---------------------------------------------------------------------*
*&      Form  populate_bapi_tables
*&---------------------------------------------------------------------*
*       Fill up the BAPI Tables
*----------------------------------------------------------------------*
FORM populate_bapi_tables .

  PERFORM populate_bapi_header.
  PERFORM build_bapi_partners.
  PERFORM build_bapi_items.
  PERFORM build_bapi_sched_lines.

ENDFORM.                    " populate_bapi_tables
*&---------------------------------------------------------------------*
*&      Form  populate_bapi_header
*&---------------------------------------------------------------------*
*       Build BAPI Header Details
*----------------------------------------------------------------------*
FORM populate_bapi_header .

  CLEAR st_bapisdhd1.
  st_bapisdhd1-doc_type     = 'ZOC'.         "Order type
  st_bapisdhd1-sales_org    = '026'.         "Sales Org
  st_bapisdhd1-distr_chan   = '00'.          "Dist Channel
  st_bapisdhd1-division     = '00'.          "Division
  st_bapisdhd1-purch_no_c   = 'Cust Po No'.  "Cust PO No
  st_bapisdhd1-name         = 'Orderer'.     "Name of Orderer
  st_bapisdhd1-ord_reason   = ''.            "Order Reason
  st_bapisdhd1-sales_off    = '3001'.        "Sales Office
  st_bapisdhd1-sales_grp    = '301'.         "Market Area

  IF g_valid_contract = 'X'.
    st_bapisdhd1-price_date = g_cont_price_date.
  ENDIF.

ENDFORM.                    " populate_bapi_header
*&---------------------------------------------------------------------*
*&      Form  build_bapi_partners
*&---------------------------------------------------------------------*
*       Build BAPI Partner Functions
*----------------------------------------------------------------------*
FORM build_bapi_partners .

  CLEAR tbl_bapiparnr.
  tbl_bapiparnr-partn_role   = 'AG'.
  tbl_bapiparnr-partn_numb   = '0000100750'.
  APPEND tbl_bapiparnr.

  CLEAR tbl_bapiparnr.
  tbl_bapiparnr-partn_role   = 'WE'.
  tbl_bapiparnr-partn_numb   = '0000504472'.
  APPEND tbl_bapiparnr.

ENDFORM.                    " build_bapi_partners
*&---------------------------------------------------------------------*
*&      Form  build_bapi_items
*&---------------------------------------------------------------------*
*       Build The BAPI Line Items
*----------------------------------------------------------------------*
FORM build_bapi_items .

  DATA: l_matnr LIKE mara-matnr.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = '10000072'
    IMPORTING
      output = l_matnr.

  CLEAR tbl_bapisditm.
  tbl_bapisditm-itm_number  = c_item.

  IF g_valid_contract = 'X'.
    tbl_bapisditm-ref_doc     = c_contract.
    tbl_bapisditm-ref_doc_it  = c_item.
    tbl_bapisditm-ref_doc_ca  = 'G'.       "Contract
  ENDIF.

  tbl_bapisditm-material    = l_matnr.
  tbl_bapisditm-plant       = '3012'.
  tbl_bapisditm-target_qty  = '5.000'.

  tbl_bapisditm-target_qu   = 'M3'.
  tbl_bapisditm-item_categ  = 'ZZOC'.
  tbl_bapisditm-sales_dist  = '301'.
  tbl_bapisditm-dlv_prio    = '02'.

  tbl_bapisditm-prc_group5  = '080'.

  tbl_bapisditm-cust_mat35  = 'kdmat'.
  tbl_bapisditm-route       = 'TESYS'.
  tbl_bapisditm-usage_ind   = 'CIV'.


  APPEND tbl_bapisditm.

  CLEAR tbl_bapisditmx.
  tbl_bapisditmx-itm_number = c_item.
  tbl_bapisditmx-ref_doc    = 'X'.
  tbl_bapisditmx-ref_doc_it = 'X'.
  tbl_bapisditmx-ref_doc_ca = 'X'.
  tbl_bapisditmx-material   = 'X'.
  tbl_bapisditmx-updateflag = 'I'.
  tbl_bapisditmx-plant      = 'X'.
  tbl_bapisditmx-target_qty = 'X'.

  tbl_bapisditmx-target_qu  = 'X'.
  tbl_bapisditmx-item_categ = 'X'.
  tbl_bapisditmx-sales_dist = 'X'.
  tbl_bapisditmx-dlv_prio   = 'X'.

  tbl_bapisditmx-prc_group5 = 'X'.
  tbl_bapisditmx-cust_mat35 = 'X'.
  tbl_bapisditmx-usage_ind  = 'X'.
  tbl_bapisditmx-route      = 'X'.

  APPEND tbl_bapisditmx.

ENDFORM.                    " build_bapi_items
*&---------------------------------------------------------------------*
*&      Form  build_bapi_sched_lines
*&---------------------------------------------------------------------*
*       Build the BAPI Schedule Lines
*----------------------------------------------------------------------*
FORM build_bapi_sched_lines .

  CLEAR tbl_bapischdl.
  tbl_bapischdl-itm_number  = c_item.
  tbl_bapischdl-req_qty     = '1'.
  tbl_bapischdl-req_date    = sy-datum.
  APPEND tbl_bapischdl.

ENDFORM.                    " build_bapi_sched_lines
*&---------------------------------------------------------------------*
*&      Form  build_bapi_conditions
*&---------------------------------------------------------------------*
*       Pull the BAPI Pricing Conditions from the Contract
*----------------------------------------------------------------------*
FORM build_bapi_conditions .

  LOOP AT tbl_bapisdcond.
    CLEAR tbl_bapicond.
    MOVE-CORRESPONDING tbl_bapisdcond TO tbl_bapicond.
    APPEND tbl_bapicond.

    CLEAR tbl_bapicondx.
    tbl_bapicondx-itm_number = tbl_bapicond-itm_number.
    tbl_bapicondx-cond_st_no = tbl_bapicond-cond_st_no.
    tbl_bapicondx-cond_count = tbl_bapicond-cond_count.
    tbl_bapicondx-cond_type  = tbl_bapicond-cond_type.
    tbl_bapicondx-updateflag = 'I'.
    tbl_bapicondx-cond_value = 'X'.
    tbl_bapicondx-currency   = 'X'.
    tbl_bapicondx-cond_unit  = 'X'.
    tbl_bapicondx-cond_p_unt = 'X'.
    tbl_bapicondx-varcond    = tbl_bapicond-varcond.
    APPEND tbl_bapicondx.

  ENDLOOP.

ENDFORM.                    " build_bapi_conditions
*&---------------------------------------------------------------------*
*&      Form  call_create_sales_ord_bapi
*&---------------------------------------------------------------------*
*       Call the Sales Order Create BAPI
*----------------------------------------------------------------------*
FORM call_create_sales_ord_bapi .

  CLEAR g_vbeln_created.

  CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
    EXPORTING
      order_header_in    = st_bapisdhd1
    IMPORTING
      salesdocument      = g_vbeln_created
    TABLES
      return             = tbl_return
      order_items_in     = tbl_bapisditm
      order_items_inx    = tbl_bapisditmx
      order_partners     = tbl_bapiparnr
      order_schedules_in = tbl_bapischdl.

  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait = 'X'.

ENDFORM.                    " call_create_sales_ord_bapi
*&---------------------------------------------------------------------*
*&      Form  update_sales_ord
*&---------------------------------------------------------------------*
*       Update the newly created Sales Order with the Pricing Conditions
*       from the Contract
*----------------------------------------------------------------------*
FORM update_sales_ord .

  CHECK g_valid_contract EQ 'X'.

  PERFORM get_contract_details.
  PERFORM build_bapi_conditions.
  PERFORM call_change_sales_ord_bapi.

ENDFORM.                    " update_sales_ord
*&---------------------------------------------------------------------*
*&      Form  get_contract_details
*&---------------------------------------------------------------------*
*       Get Contract Details
*----------------------------------------------------------------------*
FORM get_contract_details .

* Need to manually get the relevant Pricing Conditions as the BAPI
* BAPISDORDER_GETDETAILEDLIST causes problems when we call the BAPI
* BAPI_SALESORDER_CREATEFROMDAT2 and BAPI_SALESORDER_CHANGE (I think
* this is due to the fact that these BAPIs belong to the same Function
* Group and there must be some common structures that are not cleared
* causing us all sorts of grief when we try and call the next BAPI)

  DATA: tbl_konv TYPE STANDARD TABLE OF konv WITH HEADER LINE.
  DATA: tbl_komv TYPE STANDARD TABLE OF komv WITH HEADER LINE.
  DATA: tbl_vbak TYPE STANDARD TABLE OF vbak WITH HEADER LINE.

* Pricing Condition Master
  DATA: BEGIN OF tbl_t685a OCCURS 0,
          kschl     LIKE t685a-kschl,
          kaend_wrt LIKE t685a-kaend_wrt,
        END OF tbl_t685a.

  SELECT *
         INTO TABLE tbl_vbak
         FROM vbak
         WHERE vbeln = c_contract.

  READ TABLE tbl_vbak INDEX 1.

  SELECT *
         INTO TABLE tbl_konv
         FROM konv
         WHERE knumv = tbl_vbak-knumv AND
               kposn = c_item.

  CHECK sy-subrc EQ 0.

* We now need to make sure we only bring across the Condition Types that
* are EDITABLE. If we bring across non editable conditions (such as
* 'ZPR1') the Change Sales Order BAPI will fail
  SELECT kschl kaend_wrt
         INTO TABLE tbl_t685a
         FROM t685a
         FOR ALL ENTRIES IN tbl_konv
         WHERE kappl     EQ 'V'             AND  "Sales
               kschl     EQ tbl_konv-kschl  AND
               kaend_wrt EQ 'X'             AND  "Value is Editable
               kmanu     NE 'D'.                 "Process manually

* Prepare for Binary Search
  SORT tbl_t685a BY kschl.

  LOOP AT tbl_konv.
    READ TABLE tbl_t685a WITH KEY kschl = tbl_konv-kschl BINARY SEARCH.
    IF sy-subrc EQ 0.
      MOVE-CORRESPONDING tbl_konv TO tbl_komv.
      APPEND tbl_komv.
    ENDIF.
  ENDLOOP.

  CHECK NOT tbl_komv[] IS INITIAL.

* Map KOMV into the more BAPI friendly BAPISDCOND structure
  CALL FUNCTION 'MAP_INT_TO_EXT_STRUCTURE'
    TABLES
      fxvbak        = tbl_vbak
      fxkomv        = tbl_komv
      fxbapikomv    = tbl_bapisdcond
    EXCEPTIONS
      entry_missing = 1
      OTHERS        = 2.

ENDFORM.                    " get_contract_details
*&---------------------------------------------------------------------*
*&      Form  call_change_sales_ord_bapi
*&---------------------------------------------------------------------*
*       Call the Change Sales Order BAPI
*----------------------------------------------------------------------*
FORM call_change_sales_ord_bapi .

  DATA: st_head_chg      LIKE bapisdh1x,
        st_logic_switch  TYPE bapisdls.


  CHECK NOT g_vbeln_created IS INITIAL.

  CHECK g_valid_contract EQ 'X'.

  st_head_chg-updateflag     = 'U'.
  st_logic_switch-cond_handl = 'X'.

  CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
    EXPORTING
      salesdocument    = g_vbeln_created
      order_header_inx = st_head_chg
      logic_switch     = st_logic_switch
    TABLES
      return           = tbl_return_chg
      conditions_in    = tbl_bapicond
      conditions_inx   = tbl_bapicondx.

  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait = 'X'.

ENDFORM.                    " call_change_sales_ord_bapi
*&---------------------------------------------------------------------*
*&      Form  write_output_report
*&---------------------------------------------------------------------*
*       Produce Output Report
*----------------------------------------------------------------------*
FORM write_output_report .

  IF NOT g_vbeln_created IS INITIAL.
    WRITE:/ 'Success! Sales Order', g_vbeln_created, 'was created!'.
  ELSE.
    WRITE:/ 'Failure! Sales Order was not created!'.
  ENDIF.

  SKIP.
  WRITE:/ 'Sales Order Create Log'.
  LOOP AT tbl_return.
    WRITE:/ tbl_return-type, tbl_return-id, tbl_return-number,
            tbl_return-message.
  ENDLOOP.

  SKIP.
  WRITE:/ 'Sales Order Change Log'.
  LOOP AT tbl_return_chg.
    WRITE:/ tbl_return_chg-type, tbl_return_chg-id,
            tbl_return_chg-number, tbl_return_chg-message.
  ENDLOOP.

ENDFORM.                    " write_output_report
*&---------------------------------------------------------------------*
*&      Form  validate_contract
*&---------------------------------------------------------------------*
*       Make sure that the Contract is Valid
*----------------------------------------------------------------------*
FORM validate_contract .

  DATA: l_gueen  LIKE vbak-gueen,
        l_prsdt  LIKE vbkd-prsdt.

  SELECT SINGLE  vbak~gueen vbkd~prsdt
         INTO (l_gueen, l_prsdt)
         FROM vbak

         INNER JOIN vbkd
         ON vbak~vbeln = vbkd~vbeln

         WHERE vbak~vbeln = c_contract AND
               vbkd~posnr = '000000'.

  IF sy-datum LE l_gueen.
* Contract is valid! Set Order Price Date
    g_valid_contract  = 'X'.
    g_cont_price_date = l_prsdt.
  ENDIF.

ENDFORM.                    " validate_contract

Hope this helps.

Cheers,

Pat.

Read only

0 Likes
3,398

Hi Pat,

Was checking through your piece of code?

I have a query.

Normally when you create a Sales Order with reference to a Contract the line items are copied into the Sales Order automatically. I have a scenario where I need to get this functionality into my BAPI Call, in your piece of code below you are passing the line item data into the BAPI Item table. Is it possible to do it in the way I need it?

Please let me know.

Regards,

Karthik