Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[ABAP] ALV – Enable Excel Export

The “Excel export” functionality can easily be enabled using get_functions( )->set_all( abap_true )

SELECT * FROM sflight INTO TABLE @DATA(flights) UP TO 100 ROWS.

TRY.
    cl_salv_table=>factory( IMPORTING r_salv_table = DATA(lo_salv)
                            CHANGING  t_table      = flights ).

    lo_salv->get_functions( )->set_all( abap_true ).
    lo_salv->get_columns( )->set_optimize( abap_true ).
    lo_salv->get_display_settings( )->set_list_header( 'Flights' ).
    lo_salv->get_display_settings( )->set_striped_pattern( abap_true ).
    lo_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).

    lo_salv->display( ).

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] Debugging as other user

Imagine the following situation:

USER_A: Allowed to run a specific report, but not allowed to debug
USER_B: Is allowed to debug

In such a case, you can do the following:

  • USER_B: Sets external breakpoint for USER_A
  • USER_A: Enters the following in the command field: /hext user=USER_B
  • USER_A: Starts his report
  • USER_B: Debugger will be opened

If you check SY-UNAME in the debugger, it will have the value USER_A

[ABAP] Get MOLGA for PERNR

" Option 1
TRY.
    cl_hrpa_masterdata_factory=>get_read_molga( IMPORTING read_molga = DATA(lr_molga) ).
    DATA(molga) = lr_molga->read_molga_by_pernr( pernr ).
  CATCH cx_hrpa_violated_assertion.
ENDTRY.

" Option 2
TRY.
    DATA(employee_api) = cl_hcmfab_employee_api=>get_instance( ).
    DATA(molga)        = employee_api->get_molga( pernr ).
  CATCH cx_hcmfab_common.
ENDTRY.

[ABAP] xstring to binary (RAW)

* Option 1
  DATA lt_data TYPE STANDARD TABLE OF x255.
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer     = lv_xstring
    TABLES
      binary_tab = lt_data.

* Option 2
  cl_scp_change_db=>xstr_to_xtab( EXPORTING im_xstring = lv_xstring
                                  IMPORTING ex_xtab    = DATA(lt_data) ).

* Option 3
  DATA(lt_data) = cl_bcs_convert=>xstring_to_solix( lv_xstring ).

[ABAP] Read E-Mail-Template

Addition to https://nocin.eu/abap-e-mail-templates-in-s-4hana/

I had to create an E-Mail template where I did not need a CDS view or any variable at all. I simply needed the defined mail template without any rendering of class cl_smtg_email_api.
Solution: Reading the plain mail template can be achieved using class cl_smtg_email_template and method get_tmpl_cont:

DATA(ls_mail_content) = cl_smtg_email_template=>get( p_mailtx )->get_tmpl_cont( iv_langu = 'D' ).

Full sample:

 PARAMETERS: p_mailtx TYPE smtg_tmpl_id OBLIGATORY.
   
 TRY.
        " get mail template
        DATA(ls_mail_content) = cl_smtg_email_template=>get( p_mailtx )->get_tmpl_cont( iv_langu = 'D' ).

        " optional: dynamic manipulation of html email body

        " create mail document
        DATA(lo_mail_document) = cl_document_bcs=>create_document( i_type    = 'HTM'
                                                                   i_subject = conv #( ls_mail_content-subject )
                                                                   i_text    = cl_bcs_convert=>string_to_soli( ls_mail_content-body_html ) ).

        " create Sender & Receiver
        DATA(lo_sender)    = cl_cam_address_bcs=>create_internet_address( i_address_string = 'noreply@example.com'
                                                                          i_address_name   = 'Test' ).

        DATA(lo_recipient) = cl_cam_address_bcs=>create_internet_address( i_address_string = 'max.mustermann@example.com').

        " create Business Communication Service
        DATA(lo_bcs) = cl_bcs=>create_persistent( ).
        lo_bcs->set_document( lo_mail_document ).
        lo_bcs->set_sender( lo_sender ).
        lo_bcs->add_recipient( lo_recipient ).
        lo_bcs->send( ).
        COMMIT WORK.

      CATCH cx_smtg_email_common INTO DATA(ls_cx).
        DATA(lv_message) = ls_cx->get_text( ).
        MESSAGE e899(id) WITH 'Unable to send message:'(004) lv_message.
    ENDTRY.

[ABAP] Quick way to get last year’s BEGDA and ENDDA

Using method get_current_year of class cl_hcmfab_reporting_utility you can get BEGDA and ENDDA of the current year, e.g. 01.01.2025 and 31.12.2025

  CALL METHOD cl_hcmfab_reporting_utility=>get_current_year
    EXPORTING
      iv_date  = sy-datlo
    IMPORTING
      ev_begda = DATA(begda)
      ev_endda = DATA(endda).

I couldn’t find a similar method to get the same for the last year. But I could solve the problem with three simple lines.

DATA(last_year) = conv d( sy-datlo - 365 ).
DATA(begda)     = last_year(4) && '0101'.
DATA(endda)     = last_year(4) && '1231'.

After that, I asked Perplexity and it gave me a similar result.

DATA(lv_last_year) = sy-datum+0(4) - 1.
DATA(lv_begda)     = |{ lv_last_year }0101|.
DATA(lv_endda)     = |{ lv_last_year }1231|.

Using this method, you could even reduce it to two lines, and it would still be easy to read.

DATA(begda)  = |{ sy-datum(4) - 1 }0101|.
DATA(endda)  = |{ sy-datum(4) - 1 }1231|.