on 02-28-2021 4:07 AM
Hi Experts,
Need your help in analyzing the below code.
My requirement is to create a new internal table( et_item_type) from an existing internal table( lt_item_type) by changing a few fields in the structure.
Please find the below code for reference.
The new table et_item_type should be able to switch the item type when it is 'ZABC' to 'ZXYZ' else it should be 2.
Somehow this logic does not seem to work.
Got my reference from
https://answers.sap.com/questions/12331894/for-expressions-with-corresponding-operator-and-ch.html
TYPES: BEGIN OF lty_itemtype,
v_itemtype TYPE /scdl/dl_itemtype,
v_itemcat TYPE /scdl/dl_itemtype,
END OF lty_itemtype.
DATA: lv_zdlv TYPE /scdl/dl_itemtype VALUE 'ZABC'.
DATA: lv_odlv TYPE /scdl/dl_itemtype VALUE 'ZXYZ'.
DATA : it_item_type TYPE STANDARD TABLE OF lty_itemtype,
et_item_type TYPE STANDARD TABLE OF lty_itemtype.
et_item_type = VALUE #( FOR wa IN it_item_type ( CORRESPONDING #(BASE ( VALUE #( v_itemtype = SWITCH /scdl/dl_itemtype( WHEN lv_odlv THEN lv_zdlv ELSE 2 ) ) ) wa ) ) ).
Hi!
Well, there's a missing space before BASE, and you got the SWITCH syntax wrong. It's
SWITCH type( operand WHEN const1 THEN result1 ELSE resultn )
And note that you can not use variables for the const1, so that's the third thing. Fourth would be that you switched the variables in the SWITCH from how you explain it "when it is 'ZABC' to 'ZXYZ'. They should be the opposite order...
Here's the ABAP Documentation for the SWITCH operator
And then, I can't really get my head around your VALUE #( CORRESPONDING #( BASE ( VALUE #( ) ) ) ) thing... Here's how I would do it, using your variable names. Given I understand what you want to accomplish 🙂
et_item_type = VALUE #( FOR wa IN it_item_type
( v_itemtype = SWITCH #( wa-v_itemtype WHEN lv_zdlv THEN lv_odlv ELSE 2 )
v_itemcat = wa-v_itemcat ) ).
With it_item_type:
ZAAA AAA
ZABC BBB
ZABC CCC
ZAAA DDD
ZABC EEE
You will get et_item_type:
2 AAA
ZXYZ BBB
ZXYZ CCC
2 DDD
ZXYZ EEE
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for your comment,
I was able to accomplish the functionality with the tweaks you had mentioned.
The reason I need corresponding is that my table has 20 fields and I cannot fill each field in my code.
This does not work
et_item_type = VALUE #( FOR wa IN it_item_type ( CORRESPONDING #( BASE
( value #( v_itemtype = SWITCH #( wa-v_itemtype WHEN lv_zdlv THEN lv_odlv ELSE 2 ) ) )
wa ) )
).
May be SAP does not work on the above syntax, I am not sure, will try to probe further, else I need to go with the conventional method , of looping the table and use corresponding to achieve the end result
This works flawlessly ( as stated by you ) but I cannot fill all the feilds mentioned in the table.
et_item_type = VALUE #( FOR wa IN it_item_type
( v_itemtype = SWITCH #( wa-v_itemtype WHEN lv_zdlv THEN lv_odlv ELSE 2 )
v_itemcat = wa-v_itemcat ) ).
Adding an alternative solution:
et_item_type = it_item_type.
MODIFY et_item_type FROM VALUE #( v_itemtype = lv_odlv ) TRANSPORTING v_itemtype WHERE v_itemtype = lv_zdlv.
MODIFY et_item_type FROM VALUE #( v_itemtype = 2 ) TRANSPORTING v_itemtype WHERE v_itemtype <> lv_odlv.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
9 | |
4 | |
3 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.