03-02-2015 4:32 PM
I have a requirement.
I need to check sold to, ship to, material and total quantity values if it was previously entered in sales order.If its same while creating or changing while saving, it will show an error with a message 'xyz" and will not allow the sales order to save.
I also need to check sold to, ship to and total order value values if it was previously entered in sales order.If its same while creating or changing while saving, it will show an warning with a message 'xyz" and will allow the sales order to save. Both these conditions will be checked on sales order created in last 2 days in the sap system.
Here is my code:
FORM USEREXIT_SAVE_DOCUMENT_PREPARE tables xvbap STRUCTURE vbapvb xvbpaSTRUCTURE vbpavb
using vbak type vbak vbap type vbap vbpa type vbpa.
DATA: V_DATUM TYPE SY-DATUM,
LV_FLAG TYPE FLAG,
LV_SUBRC1 TYPE SY-SUBRC,
LV_SUBRC2 TYPE SY-SUBRC,
LV_SUBRC3 TYPE SY-SUBRC,
LV_SUBRC4 TYPE SY-SUBRC,
LV_SUBRC5 TYPE SY-SUBRC,
LV_SUBRC6 TYPE SY-SUBRC.
DATA: IT_VBAK TYPE TABLE OF VBAK,
IT_VBAP TYPE TABLE OF VBAP,
IT_VBPA TYPE TABLE OF VBPA.
DATA: WA_VBAK TYPE VBAK,
WA_VBAP TYPE VBAP,
WA_VBPA TYPE VBPA.
CONSTANTS: c_va01 TYPE string value 'VA01',
c_va02 TYPE string value 'VA02',
c_vkorg TYPE string value '0020',
c_spart TYPE string value '30',
c_days_count TYPE char2 value '2',
c_sh TYPE string VALUE 'WE'.
DATA: lv_error type i.
IF SY-TCODE = c_va01 OR SY-TCODE = c_va02.
IF VBAK-VKORG = c_vkorg AND VBAK-SPART = c_spart.
* Get all the sales orders created in last two days
V_DATUM = SY-DATUM - c_days_count.
REFRESH IT_VBAK.
SELECT *
FROM VBAK
INTO TABLE IT_VBAK
WHERE ERDAT GE V_DATUM.
* Get all sales orders' items created in last 2 days
IF SY-SUBRC EQ 0.
SORT IT_VBAK BY VBELN KUNNR.
REFRESH IT_VBAP.
SELECT * FROM VBAP
INTO TABLE IT_VBAP
FOR ALL ENTRIES IN IT_VBAK
WHERE VBELN = IT_VBAK-VBELN.
* Get ship to party customers for all orders created in last 2 days
IF SY-SUBRC EQ 0.
REFRESH IT_VBPA.
SELECT * FROM VBPA
INTO TABLE IT_VBPA
FOR ALL ENTRIES IN IT_VBAK
WHERE VBELN = IT_VBAK-VBELN
AND PARVW = c_sh. "Ship to party
IF SY-SUBRC EQ 0.
SORT IT_VBPA BY VBELN POSNR KUNNR.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
LOOP AT xvbap.
READ TABLE it_vbap with key matnr = xvbap-matnr TRANSPORTING NO FIELDS.
IF LV_SUBRC1 = 0.
ENDIF.
READ TABLE it_vbak with key kunnr = xvbak-kunnr TRANSPORTING NO FIELDS.
IF LV_SUBRC2 = 0.
ENDIF.
READ TABLE it_vbpa with key kunnr = xvbak-kunnr
parvw = c_sh TRANSPORTING NO FIELDS.
IF LV_SUBRC3 = 0.
ENDIF.
READ TABLE it_vbap with key netwr = xvbap-netwr TRANSPORTING NO FIELDS.
IF LV_SUBRC4 = 0.
ENDIF.
READ TABLE it_vbap with key kwmeng = xvbap-kwmeng TRANSPORTING NO FIELDS.
IF LV_SUBRC5 = 0.
ENDIF.
IF LV_SUBRC1 = 0 AND LV_SUBRC2 = 0 AND LV_SUBRC3 = 0 AND LV_SUBRC5 = 0. " CASE 1
MESSAGE 'SOLD TO, SHIP TO, MATERIALS AND QUANTITIES ARE SAME' TYPE 'E' DISPLAYLIKE 'I'.
EXIT.
ELSEIF
LV_SUBRC2 = 0 AND LV_SUBRC3 = 0 AND LV_SUBRC4 = 0 AND LV_SUBRC1 ne 0 ANDLV_SUBRC5 ne 0. " CASE 2
MESSAGE 'SOLD TO, SHIP TO AND ORDER VALUE ARE SAME' TYPE 'W' DISPLAY LIKE 'I'.
CONTINUE.
ENDIF.
ENDLOOP.
Can anyone please help me with a better code.
03-02-2015 4:36 PM
Rather than asking for better code, please explain what is wrong with the code you have.
Rob
03-02-2015 5:50 PM
Hi Rob,
The code right now gives error when sold to, ship to, materials and total quantity are equal.
It is not working for the warning message where i need to check sold to, ship to and net order value entered are same of previous sales orders. After warning message i should be able to save the sales order.
If I can get the complete code for both the requirements it will be great. For both these conditions will be checked on sales order created in last 2 days in the sap system.