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: 

How to add and delete an item/line in sales order via user exit?

klef
Explorer
0 Kudos

Under certain conditions I want an item to be added to SO, which I've managed. However, I cannot find information anywhere on how to also delete this very same item (if some conditions change).

I tried with XVBAP-UPDKT = 'D' but it does not work.

13 REPLIES 13

Madjid
Participant
0 Kudos

Hello

you can use "BAPI_SALESORDER_CHANGE"

best regards

0 Kudos

Thanks for the suggestion, however I tried using the BAPI in the USEREXIT_SAVE_DOCUMENT and get an error which I cannot resolve "Illegal state transition from "ON_NUMBERS_ADJUSTED" to "CLEANUP".

Any other ideas?

0 Kudos

The BAPI works OK by itself if I use it outside of the user exit (in a separate program). However, if I try to use it in MV45AFZZ I get an "/BOBF/CX_FRW_CONTRCT_VIOLATION" exception error.

klef
Explorer
0 Kudos

Hi, thanks for the reply!

I tried deleting the item line from XVBAP but it doesn't do anything unfortunately.

I just tried running BAPI_SALESORDER_CHANGE with "IN UPDATE TASK" statement, at the end of USEREXIT_SAVE_DOCUMENT, but I still get an error, however it's a different one: "Non-updating function module called for updating" - CALL_FUNCTION_NO_VB

Do you perhaps have any other ideas?

Adding a reason of cancellation would work too, and I managed to do that, however, the price of the sales order does not update (as if the item was not cancelled at all) ...

thkolz
Contributor
0 Kudos

I guess you can't call BAPI_SALESORDER_CHANGE in UPDATE TASK, because 'Update Module' is not flagged.

thkolz
Contributor
0 Kudos

Please also check note 178328

thkolz
Contributor

According to note 178328, you shouldn't delete entries in a user exit called by VA02:

It's better to write a program that updates the relevant sales orders via BAPI_SALESORDER_CHANGE.
This report can be scheduled in a job and update the orders on a regular basis.

0 Kudos

This cannot be a scheduled job because we need the effect immediately after saving the sales order.


The goal is to add "Handling costs" to orders that don't reach a certain net amount. Then after the line item of "Handling costs" is added, we want it removed or at least cancelled if the sales order changes (added quantity, more items added, etc.), and this should take effect immediately.

Can you think of any other solution? Thanks!

0 Kudos

I am able to cancel the item with the "abgru" field, however, the sales order net price is not getting updated.

Should I perhaps use MV45AFZB repricing for this?

0 Kudos

I’d give it a try…

There is the routine USEREXIT_NEW_PRICING_VBAP in MV45AFZB where you can retrigger the pricing:

* New pricing, when field ABGRU is changed
IF vbap-abgru ne *vbap-abgru.
   new_pricing = 'B'.
ENDIF.

0 Kudos

Thorsten, thank you.

I will give it a try.

0 Kudos

Thorsten,

I tried your suggestion, however, the problem is that in MV45AFZB the item (handling costs) does not get called so there is no option for me to call new_pricing for it.

Let me explain. The sales order before change looks like this:
1. item, net price:60€, net worth:60€,quantity:1

2. handling costs, net price:10€, net worth:10€, quantity:1

------------------------------------------------
total sales order price: 70€

But now the customer changed his mind and wants 2 of the item, which totals to 120€. So that is now over 100€ which is the threshold for us charging handling costs. So our sales representative goes ahead and changes the quantity from 1 to 2. My code in MV45AFZZ (USEREXIT_MOVE_FIELD_TO_VBAK) notices that the price is now over 100€ and goes ahead and cancels the "handling costs" item. Now the sales order looks like this.

1. item, net price:60€, net worth:120€,quantity:2

2. handling costs, net price:10€, net worth:10€, quantity:1 - CANCELED

------------------------------------------------
total sales order price: 130€

As you see, the item shows canceled, however, it still adds 10€ to the net total of the sales order.
When the sales representative manually changes the quantity of the item from 1 to 2 and handling costs get canceled, only the item gets called in MV45AFZB where one can do the new_pricing, the handling costs do not get called.

I hope this was comprehensive enough.

Do you perhaps have any other ideas? Thanks a lot!

Ryan-Crosby
Active Contributor
0 Kudos

Would recommend evaluating "handling costs" as a condition in your pricing instead of retaining it as a line item. You have far more control there on whether it is relevant for an order and the approach doesn't require user-exit hacks.