https://blogs.sap.com/2015/06/02/add-line-items-automatically-in-va01-sales-order-create/
Add a line item automatically when a user clicks on save to sales order in VA01
Limitations:
Expectations:
Approach Followed:
Code:
Execution Snapshots:
Advantages:
Use Cases:
Limitations:
There is a long list of user-exits present in SD module that facilitate customization to the standard
process. However, no User exits , Customer Exits or BADI’s have been provided which can add an
item automatically to Sales Order.
Expectations:
To add an item manually in a SO the only input required from the user is the material number and
quantity. Rest all the information is determined automatically by the standard code.The automatic
addition of item should take care of below points.
Approach Followed:
Code:
*–Check if the user has pressed SAVE button
IF sy–ucomm EQ ‘SICH’.
PERFORM vbap_unterlegen(sapfv45p).
PERFORM vbep_unterlegen(sapfv45e).
*–Populate material number and quantity
vbap–matnr = ‘000000000000240000’.
rv45a–kwmeng = 1000.
*–Call standard performs to populate material details.
*–Perform for material validations and details
PERFORM vbap–matnr_pruefen(sapfv45p) USING charx sy–subrc.
*–Perform for item category determination. This will take care of
substitution items if any for this material.
PERFORM vbap–pstyv_pruefen(sapfv45p).
*–Perform for filling VBAP with default values from configuration and
master tables
PERFORM vbap_fuellen(sapfv45p).
PERFORM vbap–matnr_null_pruefen(sapfv45p).
PERFORM vbep–wmeng_setzen(sapfv45e).
*–Perform to check sales unit
PERFORM vbap–vrkme_pruefen(sapfv45p) USING charx
CHANGING sy–subrc sy–msgid sy–msgty sy–msgno
sy–msgv1 sy–msgv2 sy–msgv3 sy–msgv4.
*–Perform to update VBAP values
PERFORM vbap_bearbeiten(sapfv45p).
*–Perform for filling VBEP with default values. This will take care of
schedule lines of the item
PERFORM vbep_fuellen(sapfv45e).
*–Perform to check quantity
PERFORM vbep–wmeng_pruefen(sapfv45e) USING charx
CHANGING sy–subrc sy–msgid sy–msgty sy–msgno
sy–msgv1 sy–msgv2 sy–msgv3 sy–msgv4.
*–Perform to update VBEP values
PERFORM vbep_bearbeiten(sapfv45e).
*–Perform to fill conditions and pricing data
PERFORM vbap_bearbeiten_ende(sapfv45p).
ENDIF.
ENDIF.
Execution Snapshots:
Go to VA01 to create a sales order. Enter sold to party and other details. Add 1 item manually.
Click on save, item 240000 should be item as next item ( POSNR 20 ).
Thus , we see in this way a line item can be added automatically in the sales order.
Advantages:
The item addition can be done on the basis of certain conditions and multiple items can be
added by placing this code in a loop.
Since all standard performs are called to fill the data , even the different user-exits for item
will be triggered if implemented for the item.
By adding a check for call_bapi equal to ‘X’ and TRTYP eq ‘H’ , this code can be executed
from Create Sales Order BAPI also.
Item number is determined automatically.
Item category , item substitution, scheduling , availability check and pricing is done by
standard performs.
If the requirement is to add items just as the user enters the header details, that can be done
with minimum code changes.
Use Cases:
This can be used in certain business scenarios like:
Suppose based on the customer some free items need to be added to sales order. In such
case the item ( material ) and customer combination can be stored in a custom table. It can
be then read from and populated automatically.
It can also be used if some default material has to be added to all sales orders being
created.
EDIT:
Sometime the requirement can be to delete a line item internally using code.Below is the
code for this
ivbap-selkz = ‘X’.
ENDIF.
Note:
~ Tanmay