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: 

My client has a requirement he wants to create a sales order in VA01

ravimohanbhatt
Explorer

My client has a requirement he wants to create a sales order in VA01 and he wants its data to be copied to another sales order in which the sales document number should be incremented by 1 and customer reference number should be saved with an _ in the second order before saving the data. I am stuck at USEREXIT_SAVE_DOCUMENT_PREPARE . please guide

17 REPLIES 17

former_member182466
Contributor

With the way number ranges work, this seems to be an impossible requirement. Or at least really tricky to implement.

Also, it would be better to create the second order in USEREXIT_SAVE_DOCUMENT using BAPI_SALESORDER_CREATEFROMDAT1 or BAPI_SALESORDER_CREATEFROMDAT2. Be careful to be able to distinguish the original and the copy order creation, if you mess up there you'd have an endless loop.

0 Kudos

can you elaborate use of bapi_salesorder_createfromdat1 or bapi_salesorder_createfromdat1. Also whats the diffrence between them

can i provide them with my own vbeln number

what variable to capture data and pass to the bapi

Well, bapi_salesorder_createfromdat1 has been obsolete since 4.6C, for newer releases people use bapi_salesorder_createfromdat2.

There is a nice transaction code - BAPI, which lists details on the parameters, the FM has also some documentation in SE37, there are sap notes dedicated to SD BAPIs - all one needs is search :).

As Gerrit pointed out, fulfilling the requirement is tricky, even with external number ranges. In my opinion, it makes sense to get back to the requester and understand why they need a separate order created in the background with consecutive numbering.

0 Kudos

PLEASE GO THRU CODE BELOW MY BAPI IS RUNNING IN LOOP

Be careful to be able to distinguish the original and the copy order creation, if you mess up there you'd have an endless loop.

To elaborate on that. You need to be able to know when you enter the user exit if it's a call that happened because of the call to the BAPI to create the subsequent order, otherwise you will create a subsequent order for the subsequent order, ad infinitum.

You can do this is a number of ways:

  1. Use EXPORT TO and IMPORT FROM MEMORY;
  2. Set a static attribute on a class;
  3. Set a static attribute in the user exit;
  4. Pass a specific value to the BAPI;
  5. Check the call stack using FM SYSTEM_CALLSTACK;

Note that whatever method you choose you will need test it all works when the user creates two orders in succession without leaving the transaction.

I hope this gives you an idea how to stop the endless loop from happening.

kiran_k8
Active Contributor
0 Kudos

Curious to know what could be the Objective for this sort of set up.

Both these SOs and their corresponding Material,Delivery and Financial Documents will also be accounted to the same GL ?

K.Kiran.

ravimohanbhatt
Explorer
0 Kudos
IF sy-UNAME EQ 'myusernaME' AND T180-TRTYP EQ 'H'.
data: lcount type vbeln,
      header type bapisdhD1,
      headerx type bapisdhD1x,
      return type table of bapiret2,
      it_order type table of bapisditm,
      it_orderx type table of bapisditmx,
      it_partner type table of bapiparnr,
      is_order type bapisditm,
      is_orderx type bapisditmx,
      is_partner type bapiparnr,
      item type matnr.

header-doc_type = vbak-auart.            "Header for Bapi
header-sales_org = vbak-vkorg.           "Header for Bapi
header-distr_chan = vbak-vtweg.          "Header for Bapi
header-sales_off = vbak-vkbur.           "Header for Bapi
header-sales_grp = vbak-vkgrp.          "Header for Bapi
header-division = vbak-spart.

                                 "Header for Bapi

headerx-doc_type = 'X'.            "Header for Bapi
headerx-sales_org = 'X'.           "Header for Bapi
headerx-distr_chan = 'X'.          "Header for Bapi
headerx-sales_off = 'X'.           "Header for Bapi
headerx-sales_grp = 'X'.          "Header for Bapi
headerx-division = 'X'.            "Header for Bapi



*partner data ship-tp-party , sold-to-party
is_partner-partn_role = 'AG'.
is_partner-partn_numb = KUAGV-KUNNR.
Append is_partner to it_partner.
is_partner-partn_role = 'WE'.
is_partner-partn_numb = KUWEV-KUNNR.
append is_partner to it_partner.
* order table to pass to bapi.



IS_ORDER-ITM_NUMBER = VBAP-POSNR.
IS_ORDER-MATERIAL = VBAP-MATNR.
IS_ORDER-TARGET_QTY = VBAP-ZMENG.
IS_ORDER-TARGET_QU = VBAP-ZIEME.
IS_ORDER-PURCH_NO_C = VBKD-BSTKD.
IS_ORDER-PURCH_DATE = VBKD-BSTDK.
APPEND IS_ORDER TO IT_ORDER.

IS_ORDERX-ITM_NUMBER = 'X'.
IS_ORDERX-MATERIAL = 'X'.
IS_ORDERX-TARGET_QTY = 'X'.
IS_ORDERX-TARGET_QU = 'X'.
IS_ORDERX-PURCH_NO_C = 'X'.
IS_ORDERX-PURCH_DATE = 'X'.
APPEND IS_ORDERX TO IT_ORDERX.



CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
  EXPORTing
*   SALESDOCUMENTIN               =
   order_header_in               = HEADER
   ORDER_HEADER_INX              = HEADERX
*   SENDER                        =
*   BINARY_RELATIONSHIPTYPE       =
*   INT_NUMBER_ASSIGNMENT         =
*   BEHAVE_WHEN_ERROR             =
*   LOGIC_SWITCH                  =
*   TESTRUN                       =
*   CONVERT                       = ' '
* IMPORTING
*   SALESDOCUMENT                 = lcount
  tables
   RETURN                        = return
   ORDER_ITEMS_IN                = IT_order
   ORDER_ITEMS_INX               = IT_ORDERX
   order_partners                = IT_partner
*   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              =
*   EXTENSIONEX                   =
*   NFMETALLITMS                  =
.
endif.

0 Kudos

I applied this code in userexit_save_document and its looping at bapi creating multiple sales order in developement system

0 Kudos

This code only creates one item, does not set the order number as required and results in an endless loop.

Jelena
Active Contributor

What business process would this support and what value would this add to the business? What is the significance of this operation? Was this evaluated by a qualified SD consultant?

Sorry but such requirement for custom ABAP development sounds rather capricious. As correctly pointed out, with the internal number range assignment it's impossible to guarantee exactly +1 number anyway. And I don't get why would anyone possibly need a duplicate order (some severely misunderstood intercompany scenario?). If someone can explain the actual business requirement (other than "he wants") then we might be able to propose a more adequate solution.

kiran_k8
Active Contributor
0 Kudos

(other than "he wants") - I liked that 🙂

K.Kiran.

0 Kudos

Its true that its not a regular business process to create salesorder replicating themselves we tried to convince the consultants on client side.but they stick on to what i asked . but anyhow thanks all for providing such valuable inputs.

we did fulfilled the requirement.

by implementing bapi_salesdocument_createfromdat at userexit_save_document with a ( if condition ) check on sy-cprog without the condition check the bapi will enter in an infinite loop creating salesorder.

kiran_k8
Active Contributor
0 Kudos

All the best 😞

Jelena
Active Contributor
0 Kudos

Well, best of luck to whomever end up maintaining this, going forward.

pjcools
Active Contributor

Agree with jelena.perfiljeva2 here. Expecting people to respond to this without a full explanation of the requirements is just plan lazy. Detail the full business process and scenarios, then we can help with more information.

This is my motto at the moment - Just because you can possibly do something with code doesn't mean you should. It is our job as Senior SAP Consultants to guide and educate clients into making right decisions taking into account future flexibility, future support and maintenance. This would be a nightmare to support. Like gert.beukema states getting the next number would be like winning the Lottery - you probably have more chance winning that than getting the next number in that range.

So, if the client still does not get the message (and wants to proceed) the best way to do these sorts of things is through output as it creates a separate process (outside the current Sales order create step you are currently in). This will work. If you need a similar number then I would say you need to create the order with an External number Range however you won't know the number until AFTER you've created the first one!

Jelena
Active Contributor

Yep. Using output would be the way to go here if there was a legitimate business requirement. Which, I guess, we'll never find...

kiran_k8
Active Contributor

"This is my motto at the moment - Just because you can possibly do something with code doesn't mean you should. It is our job as Senior SAP Consultants to guide and educate clients into making right decisions taking into account future flexibility, future support and maintenance. This would be a nightmare to support."

I bow down to Phil for those lines.Fully agree with you.

K.Kiran.