Skip to Content

Assign Field-Symbol with new Cond # Syntax

Hey first question here, and I hope I'm doing it right and it is not redundant

I want to read a specific line of an internal table, and modify the table line afterwards.

This can be accomplished by this codeblock:
IF is_estrh_tar IS NOT INITIAL.
READ TABLE ms_spc_db-estrh WITH KEY matched = abap_false subid = is_estrh_tar-subid
ASSIGNING FIELD-SYMBOL(<fs_estrh>).
IF <fs_estrh> IS ASSIGNED.
es_estrh_db = <fs_estrh>.
<fs_estrh>-matched = abap_true.
UNASSIGN <fs_estrh>.
ENDIF. "<fs_estrh>
ENDIF. "ls_estrh_tar

Since I became an avid fan of the constructor expressions Value, Cond, etc I tried building this with the new syntax. es_estrh_db = COND #( WHEN is_estrh_tar IS NOT INITIAL THEN
VALUE #( ms_spc_db-estrh[ matched = abap_false subid = is_estrh_tar-subid ] OPTIONAL ) ).
ms_spc_db-estrh[ recn = es_estrh_db-recn ]-matched = COND #( WHEN es_estrh_db IS NOT INITIAL THEN abap_true ).

The only problem I have is that the new syntax requires two read operations on the table.

I tried using LET <fs_estrh> = ms_spc_db-estrh[ matched = abap_false subid = is_estrh_tar-subid ] in the COND #-Statment, but although that might work in the Condition there is no way to modify this field-symbol in the same expression, since the COND-Expression is specifically designed to only change es_estrh_db.

Is there any way to assign a field-symbol to the output, like:
"ASSIGN <fs_estrh> TO VALUE # / COND # ( .... )" ?

Then I could just Assign the fieldsymbol via constructor operator and modify it in the next statement.

Hope this explaines my question and thanks for any considerations to my question!

Best regards
Julian Horn

Add a comment
10|10000 characters needed characters exceeded

  • First Codeblock - "traditional" Read table:

    IF is_estrh_tar IS NOT INITIAL.
      READ TABLE ms_spc_db-estrh WITH KEY matched = abap_false subid = is_estrh_tar-subid ASSIGNING FIELD-SYMBOL(<fs_estrh>).
      IF <fs_estrh> IS ASSIGNED.
        es_estrh_db = <fs_estrh>.
        <fs_estrh>-matched = abap_true.
        UNASSIGN <fs_estrh>.
      ENDIF. "<fs_estrh>
    ENDIF. "ls_estrh_ta

    Second Codeblock - via constructor operator:

    es_estrh_db = COND #( WHEN is_estrh_tar IS NOT INITIAL THEN
                          VALUE #( ms_spc_db-estrh[ matched = abap_false subid = is_estrh_tar-subid ] OPTIONAL ) ).
    ms_spc_db-estrh[ recn = es_estrh_db-recn ]-matched = COND #( WHEN es_estrh_db IS NOT INITIAL THEN abap_true ).

    Thx for the hint, hope that formatting works out when i click submit now ;)

    Best regards
    Julian Horn

  • Please use the CODE button to format your code so that it's shown in a more user-friendly format (colorized).

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Nov 06, 2019 at 03:16 PM

    See comments under Frederic Girod 's answer:

    TRY.
        DATA(my_var) = REF #( ms_spc_db-estrh[ matched = ABAP_FALSE subid = is_estrh_tar-subid ] ).
        my_var->matched = ABAP_TRUE.
      CATCH cx_sy_itab_line_not_found ##no_handler.
    ENDTRY.
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 06, 2019 at 06:36 AM

    For my point of view, field-symbol should be used for dynamic purpose & really important optimisation need.

    And it is not recommended for Clean-Code

    (I have not tested the code)

    TRY.
         ms_spc_db-estrh[ matched = ABAP_FALSE subid = is_estrh_tar-subid ]-matched = ABAP_TRUE.
      CATCH cx_sy_itab_line_not_found.
    ENDTRY.
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 06, 2019 at 10:17 AM

    I think you don't need to check the condition of is_estrh_tar like that. but for dynamic way you may try function method inside [ ]:

    ASSIGN ms_spc_db-estrh[ matched = abap_false subid = func=>in( is_estrh_tar-subid ) ] TO <fs_estrh>.
    Add a comment
    10|10000 characters needed characters exceeded