Skip to Content
Personal Insights
Author's profile photo Joachim Rees

ABAP Know How: 3 lines of code, 2 commented out

Sometimes, “little chunks of knowledge” can be as little as a few lines of code.
And most of them are commented out.

With this blog post, I’ll give you an example for it, explain why keeping them somewhere is useful for me, and invite you to let me know if you have similar cases.

My example is this:

* data lo_report TYPE REF TO lcl_report.
* CREATE OBJECT lo_report.
DATA(lo_report) = NEW lcl_report( ).

Explanation:
Defining a reference variable to a class,
and creating an instance of that class (that is: creating an object of that class),
and assigning that object to the reference variable
used to be 2 lines of code.
But it now can be done just as well in one line of code.

I try to use the new version, but I’m not always sure on first try.
Like, all those don’t work, and I remember failing with a least a few of them:

*FALSE examples:
lo_report = new lcl_report( )
DATA(lo_report) = NEW lcl_report.
DATA(lo_report) = NEW lcl_report().
DATA(lo_report) = NEW lcl_report( )

So I think it’s clear, that we benefit from having those “3lines of code, 2 commented out” examples.

But I don’t want them in my coding!
1. I don’t want useless comments. I’ll delete them!
2. How would I find such an example again? (I know I did that somewhere, but where?)

So, this all leads to my plan: I’ll use a SAP Community blog post for those things:

1. It’s the right place
2. I don’t need screenshots, my sources can be #textOnly!
3. it still looks nice, just like in the editor, as I can put it in a code block.
4. I can add new things by editing or as a comment.
5. Maybe most important: publicly shared knowledge is good knowledge: I have a convenient place to look things up again and others can also benefit.

What do you think about that?
Best
Joachim

Assigned Tags

      10 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Michelle Crapo
      Michelle Crapo

      Awesome idea!!!  You can find it again - and we can learn from it.

      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Thanks a lot for your Feedback, Michelle Crapo !

      I got another one right here:

      *While this one line does not work, the two following lines do just fine:
      *    METHODS: get_data IMPORTING it_so_act_type TYPE range of /scwm/lagps-act_type .
          TYPES: gty_act_type TYPE RANGE OF /scwm/lagps-act_type.
          METHODS: get_data IMPORTING it_so_act_type TYPE gty_act_type .
      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Already when writeing that 'I' and 'EQ' I thought: there's probably constants for that?!.

      Now I know:

          LOOP AT lt_lgtyp_param ASSIGNING FIELD-SYMBOL(<lfs_param_lgtyp>).
      *      ls_range_line-sign = 'I'.
            ls_range_line-sign = wmegc_sign_inclusive .
      *      ls_range_line-option = 'EQ'.
            ls_range_line-option = wmegc_option_eq .
            ls_range_line-low = <lfs_param_lgtyp>-par_value .
            APPEND ls_range_line TO lr_lgtyp.
          ENDLOOP.
      Author's profile photo Tomas Buryanek
      Tomas Buryanek

      I am afraid these constants are not globally available in whole system?

      Anyway here is one line version for you 🙂

      *CLEAR ls_range_line.
      *ls_range_line-sign = 'I'.
      *ls_range_line-option = 'EQ'.
      *ls_range_line-low = 'ABC'.
      *APPEND ls_range_line TO lr_lgtyp.
      
      APPEND VALUE #( sign = 'I' option = 'EQ' low = 'ABC' ) TO lr_lgtyp.

      And here also replacing LOOP with FOR statement:

      *LOOP AT lt_lg ASSIGNING FIELD-SYMBOL(<ls_lg>).
      *  APPEND VALUE #( sign = 'I' option = 'EQ' low = <ls_lg> ) TO lr_lgtyp.
      *ENDLOOP.
      
      "NOTE: ls_lg variable should NOT be declared! It is implicitly declared by FOR statement itself 
      lr_lgtyp = VALUE #( FOR ls_lg IN lt_lg ( sign   = 'I'
                                               option = 'EQ'
                                               low    = ls_lg-par_value ) ).
      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Those constants are defined in TYPE-POOL wmegc. - to me they seem to be globally available, but I'm not sure. (I noticed, code completion in AdT does not work for them).

       

      Also, thanks for the alternate versions Tomas Buryanek , I appreciate those a lot!

      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Two ways to raise an exception with a MESSAGE: (one commented out)

      *      MESSAGE s002 DISPLAY LIKE 'E' WITH me->mv_lgnum 'lgtyp: todo'.
      *      RAISE EXCEPTION TYPE zclewm_cx_no_data_found USING MESSAGE.
            RAISE EXCEPTION TYPE zclewm_cx_no_data_found MESSAGE s002 WITH me->mv_lgnum 'lgtyp: todo'.
      

      The above is short, to keep the format.

      This is a little longer, to see some more context:

          INTO TABLE @DATA(lt_data_from_join).
      
          IF sy-subrc <> 0.
            "No data found (warehouse number: &1, storage types: &2 )
      *      MESSAGE s002 DISPLAY LIKE 'E' WITH me->mv_lgnum 'lgtyp: todo'.
      *      RAISE EXCEPTION TYPE zclewm_cx_no_data_found USING MESSAGE.
            RAISE EXCEPTION TYPE zclewm_cx_no_data_found MESSAGE s002 WITH me->mv_lgnum 'lgtyp: todo'.
          ENDIF.
      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Not sure if I explicitly said this already: this kinds of post often document my failures: I wrote some code and syntaxcheck said no.

      So those codeblocks show the way(s) something don't work (commented out), and the way(s) how it does work.

      create object lref_stock_mover EXPORTING iv_lgnum = iv_lgnum .
      * CREATE OBJECT lref_stock_mover( exporting iv_lgnum = iv_lgnum ) .
      lref_stock_mover = new zclewm_mon_stock_mover( iv_lgnum = iv_lgnum ).
      * lref_stock_mover = new zclewm_mon_stock_mover( exporting iv_lgnum = iv_lgnum ).
      * CREATE OBJECT lref_stock_mover( iv_lgnum = iv_lgnum ) .
      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Also should work: (as the lref_stock_mover is defined + type already)

      lref_stock_mover = new #( iv_lgnum = iv_lgnum ).

      Or define and type in one go:

      data(lref_stock_mover2) = new zclewm_mon_stock_mover( iv_lgnum = iv_lgnum ).
      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Problem:
      "not translated: (SLIN)

      me->check_if_table_empty_ifso_exit( it_table_to_check = me->mt_inv_lgtyp
      iv_what_is_it = 'valid targets' ).

      Solution:

      me->check_if_table_empty_ifso_exit( it_table_to_check = me->mt_inv_lgtyp
      iv_what_is_it = 'valid targets'(001) ).

      Oh no, actually another Problem:
      Syntax-Check:
      "'va'(003)" is not type-compatible with formal parameter "IV_WHAT_IS_IT".
      (Parameter if of type string).

      Solution(?) with CONV :

      me->check_if_table_empty_ifso_exit( it_table_to_check = me->mt_inv_lgtyp
      iv_what_is_it = conv #( 'valid targets'(003)) ).

      Syntax-Check:
      The text ID is not three characters long.

      Solution: Add a space and we are fine:

      me->check_if_table_empty_ifso_exit( it_table_to_check = me->mt_inv_lgtyp
      iv_what_is_it = conv #( 'valid targets'(003) ) ).

      Conclusion: Things (ATC/SLIN, Syntax-Check) are fine now, but it looks a lot less clean, compared to the initial state, with all the brackets.... )

      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Q: How to get a sub-set of an internal table?

      A1: with VALUE FOR:

      DATA(lt_via_for) = VALUE gtt_product_gty( FOR wa IN mt_product_qty_summed_up WHERE ( vltyp = gc_flavour_hrl ) ( wa ) ).
      "Strangely (to me), unsing # did not work: (why can't the type be derived from wa ?)
      *DATA(lt_via_for) = VALUE #( FOR wa IN mt_product_qty_summed_up WHERE ( vltyp = gc_flavour_hrl ) ( wa ) ).

      A2: with FILTER (needs a KEY on the field being filtered for) :

      DATA(lt_via_filter) = FILTER #( mt_product_qty_summed_up WHERE vltyp = gc_flavour_hrl ).
      *without key: Syntax-error:
      *The operation requires a key (SORTED or HASHED) on table column(s): "160".