on 10-15-2015 2:51 AM
Hello, i have a question about a routine. i was able to create an internal table and was able to populate it with the data i want but now the problem arises because i have multiple scenarios and according to those scenarios i have to populate my field. this is what my internal table looks like:
material | material type | Changed by | Return code | Division | ProfitCenter
100 | OR | 1324 | 2143 | USET | 85678
101 | NE | 3567 | 9076 | UKEN | 87562
Now I want to do a read statement in my field routine.
1st scenario: if material type is '1324' then assign value to result which is very simple
2nd scenario has 2 parts to it: if material type is '3567' AND if Division is 'USET', then assign a specific value to result
If material type is '3567' AND DIVISION is not equal to BLANK then assign a specific value to result.
Now, I can do multiple read statements like below. but i want to know if there is a way for me to use IF and ELSE statement to read statements?
READ TABLE imat WITH KEY
material = SOURCE_FIELDS-material
mattype = '1324'
INTO wmat. (work area)
IF SY-SUBRC = 0.
RESULT = wmat-zstype.
ENDIF.
READ TABLE imat WITH KEY
material = SOURCE_FIELDS-material
mattype = '3567'
division = 'USET'
INTO wmat.
IF SY-SUBRC = 0.
RESULT = wmat-zmtype.
ENDIF.
and so on.
Please let me know if there is a way for me to do IF ELSE loop and with in the loop i can do read statements?
Thanks.
Hello Sunil,
pls try to use below code, may be little correction required.
Selec field1 field2 into table imat from <db_table> where <conditions>. <---slect query which u are using
SORT imat BY material mattype division.
imat1[] = imat[]. " created imat1 for scenario 2(B) mentioned below
DELETE imat1 WHERE division = ''.
LOOP AT result_package ASSIGNING <result_fields>.
IF <result_fields>-mattype = '1234'."scenario 1
READ TABLE imat WITH KEY
material = <result_fields>-material
mattype = '1324'
INTO wmat BINARY SEARCH. (work area)
IF SY-SUBRC = 0.
<result_fields>-RESULT = wmat-zstype.
ENDIF.
ELSEIF <result_fields>-mattype = '3567' AND <result_fields>-division = 'USET'."scenario 2 (A)
READ TABLE imat WITH KEY
material = <result_fields>-material
mattype = '3567'
division = 'USET'
INTO wmat BINARY SEARCH. (work area)
IF SY-SUBRC = 0.
<result_fields>-RESULT = wmat-zstype.
ENDIF.
ELSEIF <result_fields>-mattype = '3567' AND <result_fields>-division <> ' '. "scenario 2 (B)
READ TABLE imat1 WITH KEY
material = <result_fields>-material
mattype = '3567'
INTO wmat BINARY SEARCH. (work area)
IF SY-SUBRC = 0.
<result_fields>-RESULT = wmat-zstype.
ENDIF .
ENDIF.
ENDLOOP.
Please let us know , if facing any further issues.
Regadrs,
Hitesh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yeah Demish .. ri8
Hello Sunil,
Please find below updated piece of code;
LOOP AT result_package ASSIGNING <result_fields>.
IF <result_fields>-mattype = '1234 '."scenario 1
READ TABLE imat WITH KEY
material = <result_fields>-material
mattype = '1324'
INTO wmat BINARY SEARCH. (work area)
IF SY-SUBRC = 0.
<result_fields>-RESULT = wmat-zstype.
ENDIF.
ELSEIF <result_fields>-mattype = '3567' .scenario 2 (A)
IF <result_fields>-division = 'USET'.
READ TABLE imat WITH KEY
material = <result_fields>-material
mattype = '3567'
division = 'USET'
INTO wmat BINARY SEARCH. (work area)
IF SY-SUBRC = 0.
<result_fields>-RESULT = wmat-zstype.
ENDIF.
ELSE. "scenario 2 (B)
READ TABLE imat1 WITH KEY
material = <result_fields>-material
mattype = '3567'
INTO wmat BINARY SEARCH. (work area)
IF SY-SUBRC = 0.
<result_fields>-RESULT = wmat-zstype.
ENDIF .
ENDIF.
ENDLOOP.
Regards,
Hitesh
You could use IF/ELSE, but a cleaner way in this example is to use RETURN. Simply, process each scenario and once the RESULT is populated for a certain scenario, issue the RETURN command. This command will end the field routine at that point, so effectively it works like an IF/ELSE by short-circuiting the rest of the unnecessary scenario processing. It is also cleaner in that you don't end up with a ladder of multiple nested IF/ELSEs.
Here's an example:
READ TABLE imat WITH KEY
material = SOURCE_FIELDS-material
mattype = '1324'
INTO wmat. (work area)
IF SY-SUBRC = 0.
RESULT = wmat-zstype.
RETURN. " End the processing here if scenario 1 is valid
ENDIF.
READ TABLE imat WITH KEY
material = SOURCE_FIELDS-material
mattype = '3567'
division = 'USET'
INTO wmat.
IF SY-SUBRC = 0.
RESULT = wmat-zmtype.
RETURN. " End the processing here if scenario 2 is valid
ENDIF.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
88 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.