Skip to Content

Add a line item automatically when a user clicks on save to sales order in VA01

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.

      • The only required input should be material number and quantity.
      • Item number ( VBAP-POSNR ) should be auto determined.
      • Item category and other item details should be read from master data / configuration tables.
      • Schedule line data to be determined as for a normal manually added item.
      • Pricing should be determined automatically.
      • Any code written in user exits should be triggered for this item as well.

Approach Followed:


We will be using  USEREXIT_MOVE_FIELD_TO_VBAK to write our code. This userexit is present in include MV45AFZZ of program SAPMV45A.

This user exit is used to transfer some fields to the sales order header.However , we will write our code in this exit to add an item during save.

Code:

*–Check if the user has pressed SAVE button

IF syucomm EQ ‘SICH’.

 

*–Read table XVBAP to check if the material is not already added

           READ TABLE xvbap INTO ls_xvbap WITH KEY matnr = 000000000000240000

                                                     updkz = ‘I’.

           IF sysubrc NE 0.

             CLEAR: vbap.

*–Initialize workareas for VBAP and VBEP


             PERFORM vbap_unterlegen(sapfv45p).

             PERFORM vbep_unterlegen(sapfv45e).

*–Populate material number and quantity

             vbapmatnr     = ‘000000000000240000’.

             rv45akwmeng   = 1000.

*–Call standard performs to populate material details.

*–Perform for material validations and details

             PERFORM vbapmatnr_pruefen(sapfv45p) USING charx sysubrc.

*–Perform for item category determination. This will take care of substitution items if any for this material.

             PERFORM vbappstyv_pruefen(sapfv45p).

*–Perform for filling VBAP with default values from configuration and master tables

             PERFORM vbap_fuellen(sapfv45p).

             PERFORM vbapmatnr_null_pruefen(sapfv45p).

             PERFORM vbepwmeng_setzen(sapfv45e).

*–Perform to check sales unit

             PERFORM vbapvrkme_pruefen(sapfv45p) USING charx

                   CHANGING sysubrc symsgid symsgty symsgno

                            symsgv1 symsgv2 symsgv3 symsgv4.

*–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 vbepwmeng_pruefen(sapfv45e) USING charx

                   CHANGING sysubrc symsgid symsgty symsgno

                            symsgv1 symsgv2 symsgv3 symsgv4.

*–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.

/wp-content/uploads/2015/06/1_716346.png

Click on save, item 240000 should be item as next item ( POSNR 20 ).


/wp-content/uploads/2015/06/2_716347.png

Check the SO in VA03. Here the item 20 is added successfully.


/wp-content/uploads/2015/06/3_716397.png



Check line item schedule line data:


/wp-content/uploads/2015/06/4_716398.png

Check Pricing data:

/wp-content/uploads/2015/06/5_716399.png

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:


  • The above code can also be written in user exit  USEREXIT_SAVE_DOCUMENT_PREPARE.


  • Sometime the requirement can be to delete a line item internally using code.Below is the code for this

        Suppose we want  to delete the item number 20.


        ivbap-selkz = ‘X’.

        MODIFY ivbap TRANSPORTING selkz WHERE posnr = ‘0020’.

        IF sy-subrc EQ 0.

          PERFORM xvbap_loeschen_loop(sapfv45p).

        ENDIF.



Help Taken from below SCN posts:


Re: New line  in SD order

Re: Adding default line items on VA01/VA21 based on Incoterms




Note:


Sensitive data has been blurred in the snapshots.

Feel free to provide feedback and valuable comments.

  • ~ Tanmay
To report this post you need to login first.

42 Comments

You must be Logged on to comment or reply to a post.

    1. Tanmay Verma Post author

      Hi Sampath,

      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.

      Regards,

      Tanmay.

      (0) 
    1. Tanmay Verma Post author

      Hi Juwin ,

      Thanks for posting the links. I had taken help from SCN posts for achieving my requirement but now was not able to find these posts  again whil creating the document.

      I will add these 2 links in the document as acknowledgement.

      ~Tanmay.

      (0) 
  1. Jyoti Prakash

    Hi,

    If you propose to hard coding the material values in your enhancement program. Then we can achieve the same result with Standard BOM functionality(when different materials for both items are determined) and free goods determination (when same material are determined in the line items). That to be without adding a single code/enhancement.

    What is the gist of this enhancment, when we can achieve the same functionality with enhancement?

    Thanks, JP

    (0) 
    1. Tanmay Verma Post author

      Hi Jyoti,

      The hard coding showed in the document was only for example. In actual scenario we maintain a custom table having material number , customer and quantity of material. The material is then read on the basis of customer and the quantity to be added is calculated based on certain calculations. This item is then finally added using the above approach during SO creation.

      At the same time the custom table is updated with the quantity added so as to keep track of the quantity left.

      Also , the table provides the facility to:

      • switch off the material for a customer and replace it with new material
      • not adding the material if the quantity is exhausted
      • adding multiple materials for same customer. ( more then 1 material can be added to SO)

      we can achieve the same result with Standard BOM functionality(when different materials for both items are determined) and free goods determination (when same material are determined in the line items).

      I am not fully aware of the BOM functionality. Can we make the material and quantity determination dynamic using this ?

      ~Tanmay.

      (0) 
      1. Jyoti Prakash

        Hi Tanmay,

        Before proposing this solution to the business, did you take consent of your functional consultant for logistics?

        material is then read on the basis of customer and the quantity basis of customer and the quantity to be added is calculated based on certain calculations

        – switch off the material for a customer and replace it with new material

        – not adding the material if the quantity is exhausted

        – adding multiple materials for same customer. ( more then 1 material can be added to SO

        Unfortunately, whatever you have mention above, can be achieved through standard configuration mention by me in my previous post. And that to be dynamically.

        Probably, you can talk to a functional consultant(SAP SD) in your team or you can search and read regarding BOM/Free Goods determination functionality.

        Thanks, JP

        (0) 
        1. Tanmay Verma Post author

          Hi Jyoti ,

          Thanks for the insight on the topic. I would certainly explore on this more.

          As per the solution, it was discussed with the SD consultant. The table approach that we used was being used for other functionalities also apart from just adding the line item and since the table was being maintained by users directly who might not have knowledge of SD configurations, it was done in this way.

          Thanks,

          Tanmay.

          (0) 
          1. Jyoti Prakash

            Hi

            It’s straight, enhancement has its own pros & cons. And enhancement is not at all advisable when you have standard functionality available. But, well-being of system Is consultant’s priority and responsibility.

            If users starts dominating the system configuration. Then why would they need Consultant or Consulting Service provider/partner. Moreover, those are basic SAP SD standard functionality.

            who might not have knowledge of SD configurations

            Strange! By the way, which industry your user belong to?

            Thanks, JP

            (0) 
            1. Tanmay Verma Post author

              Hi ,

              The end users belong to the supply chain industry and accessing the system through web interface / portal applications.

              ~Tanmay

              (0) 
              1. Jyoti Prakash

                Hi Tanmay,

                The discussion going between us is not to discourage you or find fault. And I appreciate the way you have assertively handled my queries/concerns.

                Functionally? Content may not be convincing to me. But, I can’t ignore the way content is represented. It is good and systematic documentation. Probably, you can have

                1. numbering to the title

                2. table of content

                3. Code can be represented in different font (say, courier new)

                .

                Whether user uses SAP System in foreground or background. Basic and standard feature/functionality available in the system should not be ignored. Instead of opting for enhancement.

                Best wishes.

                Cheers, JP

                (0) 
                1. Tanmay Verma Post author

                  Hi Jyoti,

                  Thanks a lot for your suggestions and pointing in the right direction. The discussion was definitely a good learning for me. 🙂   Even , i believe in what you said and try to make use of the standard functionality and go for enhancements unless its the last option.

                  I will explore the BOM / free good configs and see how that can be used.

                  Thanks.

                  Tanmay.

                  (0) 
                      1. Juwin Pallipat Thomas

                        Whenever the order value was lower than a minimum value, we had to add a fixed surcharge to the order. So, we added a new line to the order, using a service material, to add the fixed value.

                        Thanks,

                        Juwin

                        (0) 
                        1. Jyoti Prakash

                          Hi Juwin,

                          I assume fixed surcharge is for the whole sales doc that means it’s a header condition. So, why that is taken item level and that to be as service material?

                          Thanks, JP

                          (0) 
                            1. Bruno Esperança

                              It’s funny how sometimes you learn just as much or even more from the discussions than from the original post.

                              Thanks so much Juwin.

                              And thanks a lot to Tanmay as well for this very helpful post.

                              Cheers,

                              Bruno

                              (0) 
  2. Jobi Sebastian

    Hi Tanmay,

    Good document in terms of the structure and presentation. It will be very handy for ABAPErs, if not for SD consultants.

    these kind of requirements can be met through std configurations in SD, even the additional surcharge for low value orders can be configured in std. its available in std pricing procedure.

    Regards

    Jobi

    (0) 
  3. PA TRUILHE

    Hello Tanmay,

    I have exactly the same requirement within my project :

    In my SD offer, I first need to identify item when material S_ANS is present. Then I need to recreate on item per line in my service order components.

    Based on your guidelines, I developed my exit to create sales order items.

    But although I used all the perform you mentioned, some data from vbap are still not populated what cause problem in SD document flow copy.

    Here is my code in the MOVE_FIELD_TO_VBAK :

    IF ( vbak-auart = ‘ZSDH’ ) AND ( sy-tcode = ‘DP80’ ).

        IMPORT w_skip FROM MEMORY ID ‘ZSDH_SKIP’.

        CHECK w_skip = abap_false.

        w_perform = abap_false.

    * Check if S_ANS Material is present and delete item

        LOOP AT xvbap INTO ls_xvbap WHERE matnr =  ‘S_ANS’.

          ls_xvbap-updkz = ‘D’.

          w_pstyv = xvbap-pstyv.

          w_perform = abap_true.

          MODIFY xvbap FROM ls_xvbap TRANSPORTING updkz.

        ENDLOOP.

        PERFORM vbap_bearbeiten_ende(sapfv45p).

        IF w_perform = abap_true.

    * Get components information from service order

          SELECT werks bdmng meins potx1 rspos

            FROM resb

            APPENDING CORRESPONDING FIELDS OF TABLE tb_components

            WHERE aufnr = vbap-aufnr

            AND matnr EQ ”.

          IF sy-subrc = 0.

            w_skip = abap_true.

            EXPORT w_skip TO MEMORY ID ‘ZSDH_SKIP’.

    * Create one ANS item per component (what DIP cannot do)

            LOOP AT tb_components INTO ls_components.

    *     Fetch last existing item number

              DESCRIBE TABLE xvbap LINES w_lines.

              READ TABLE xvbap INDEX w_lines.

              w_posnr = xvbap-posnr.

    *–Initialize workareas for VBAP and VBEP

              PERFORM VBAP_BEARBEITEN_VORBEREITEN(sapfv45p). “666

              PERFORM vbap_unterlegen(sapfv45p).

              PERFORM vbep_unterlegen(sapfv45e).

              xvbap-posnr = w_posnr + 10.

              xvbap-matnr = ‘S_ANS’.

              xvbap-matwa = ‘S_ANS’.

              xvbap-arktx = ls_components-potx1.

    * Field EANNR is used to store recording of RESB (required for PREPARE TKOMP)

              xvbap-eannr = ls_components-rspos.

              xvbap-zmeng = ls_components-bdmng.

              xvbap-umzin = ls_components-bdmng / 1000.

              xvbap-umziz = ls_components-bdmng / 1000.

              xvbap-meins = ls_components-meins.

              xvbap-werks = ls_components-werks.

              xvbap-zieme = ls_components-meins.

              xvbap-updkz = ‘I’.

              xvbap-spart = ’00’.

              xvbap-pstyv = w_pstyv.

              append xvbap.

    *–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 VBAP_FUELLEN(sapfv45p).  “666

               PERFORM VBAP_BEARBEITEN(sapfv45p).  “666

    *–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 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).

            ENDLOOP.

            w_pricing_to_update = abap_true.

            EXPORT w_pricing_to_update TO MEMORY ID ‘ZSDH_PRICE’.

          ENDIF.

        ENDIF.

      ENDIF.

    It is working but I need to fix many value that are automaticaly populated if I create item manualy. (Like VBAP-KONDM; or KZTLF for example).

    So I assume one standard perform is still missing to commit data at then end.

    Do you have recommendation ?

    (0) 
    1. Tanmay Verma Post author

      Hi  PA,

      Sorry for the late reply.

      The code has to be written in USEREXIT_MOVE_FIELD_TO_VBAK. I can see and extra ‘Append XVBAP’ in your code. KIndly check if that is causing the issue. As the standard performs will take care of the XVBAP table and no explicit append is required.

      Using the code i have mentioned in the document i was able to successfully create an line item with all the required data.

      Regards,

      Tanmay.

      (0) 
  4. Yarnagula Sudhir

    Hi Tanmay Verma ,

    I’m very impressed with your solution. Thank you for providing to us.

    I’m having similar scenario in my project and I’ve done it so good and so simple based on your solution.

    (0) 
      1. Yarnagula Sudhir

        Hi Tanmay Verma ,

        I have a scenario.

        1. My requirement is to add same material again, but only once.

        2. But your read statement does not allow for same material. (SY-SUBRC NE 0.)

        3. If I excluded read statement logic, as this exit triggers multiple times, my proposed material adding multiple times.

        4. Even though i’ve gone for count logic, as this exit triggering multiple times, its clearing my count variable on every iteration.

        Kindly let me know the how to resolve my issue. Kindly help me out on priority.

        With Regards,

        Sudhir.

        (0) 
        1. Tanmay Verma Post author

          Hi Sudhir,

          You can declare your count variable as STATIC. This will hold the value during the LUW and will not get cleared out.

          Ex:  Statics: lv_count type i. ( instead of Data: lv_count type i. )

          Regards,

          Tanmay.

          (0) 
          1. Yarnagula Sudhir

            Hi Tanmay Verma ,

            Thanks alot for your reply and solution. Static variable worked.

            But how to make my proposed line item NET and TAX to be ZERO. Functional people maintaining Pricing as Mandatory for proposed Material.

            With Regards,

            Sudhir.

            (0) 
      1. Janagar Sundaramoorthy

        hi tanmay,

        now i am able to see the code, the following are the issues

        a)i am not able to track down the user status of the current line and copy the same into copied line b) i am passing the price, at komv-kbetr just before PERFORM vbap_bearbeiten_ende(sapfv45p).  it is is not getting into the item.

        Please help with some tips.

        even i tried updating xkomv for the netprice, but it is not happening.

        Regards

        S.Janagar

        (0) 
  5. Paul Puvvula

    Hi Tanmay,

     

    Thanks for this blog. Very useful. I am using the same logic to add lines in the same user exit. However it gives me a syntax error ‘The field VBAP-MATNR is unknown’. Did you also get this error ? Many thanks,

    Vijay.

    (0) 

Leave a Reply