cancel
Showing results for 
Search instead for 
Did you mean: 

BW Routine issue.

Former Member
0 Kudos

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.

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member183519
Contributor
0 Kudos

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

0 Kudos

Hi Hitesh,

A little suggestion here:

When you are deleting the records with division = '' in Internal table imat1, than I believe their is no need to keep the division condition in ELSEIF for Scenario 2(B).

Please let me know if any concerns.

Best Regards,

Demish Maniyar

former_member183519
Contributor
0 Kudos

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

former_member185132
Active Contributor
0 Kudos

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.