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

[ABAP] Access leave request attributes

cl_pt_req_badi=>get_request( EXPORTING im_req_id  = request_id
                             IMPORTING ex_request = DATA(request) ).

request->get_all_attribs( IMPORTING ex_attribs_struc = DATA(req_attribs) ).

DATA(req_begda) = req_attribs-version-item_tab[ 10 ]-value.

Take a look in the item_tab to see the specific request values.

[ABAP] Add custom message in workflow log

In a function exit of a workflow task, you can copy the sample coding out of class CL_SWH_WORKITEM_EXIT_LOG to append a custom message

METHOD if_swf_ifs_workitem_exit~event_raised .
  DATA: ls_msg TYPE swr_mstruc.
  DATA: ls_por TYPE sibflporb.
  DATA: l_cnt TYPE REF TO if_swf_ifs_parameter_container.

  me->m_ctx = im_workitem_context.
  IF im_event_name EQ swrco_event_after_creation.
    ls_msg-msgid = 'SWH'.
    ls_msg-msgty = 'S'.
    ls_msg-msgno = '100'.
    ls_msg-msgv1 = 'CL_SWH_WORKITM_EXIT_LOG'.
    ls_msg-msgv2 = 'swrco_event_after_creation'.
    l_cnt = m_ctx->get_wi_container( ).
    TRY.
        CALL METHOD l_cnt->get
          EXPORTING
            name  = swrco_wi_leading_object
          IMPORTING
            value = ls_por.
        CALL METHOD m_ctx->set_message_to_log
          EXPORTING
            im_message    = ls_msg
            im_object_por = ls_por.
      CATCH cx_swf_cnt_container .
    ENDTRY.
  ENDIF.
ENDMETHOD.                    "IF_SWF_IFS_WORKITEM_EXIT~EVENT_RAISED

But to add a message in general, only the workitem context is needed. If you got the context, just use method set_message_to_log.

* context type: im_workitem_context TYPE REF TO if_wapi_workitem_context   

" text in im_function will be displayed in workitem log (max char30),
" message in im_message will give further information when clicking on the traffic light (max char 70).
im_workitem_context->set_message_to_log( im_function = |Forward: Mail { var }|
                                         im_message = VALUE #( msgid = '00'
                                                               msgty = 'S'
                                                               msgno = '000'
                                                               msgv1 = |var 1|
                                                               msgv2 = |var 2| ) ).
* In some cases "COMMIT WORK" is needed.

You’ll find the log entry in workitem flog view:

[ABAP] User <-> Employeenumber

Class:
– CL_HCMFAB_EMPLOYEE_API
Methods:
– GET_USERID_FROM_EMPLOYEENUMBER
– GET_EMPLOYEENUMBER_FROM_USER

How to use the class:

TRY.
    DATA(lv_pernr) = cl_hcmfab_employee_api=>get_instance( )->get_employeenumber_from_user( sy-uname ).
  CATCH cx_hcmfab_common.
ENDTRY.

[ABAP] Find employee subtitutes

cl_swl_substitution=>get_pers_substitutes( EXPORTING  i_substituted_person     = ls_substituted_person
                                                      i_start_date             = sy-datum
                                                      i_end_date               = sy-datum
                                           IMPORTING  et_substitutes           = DATA(lt_substitutes)
                                           EXCEPTIONS user_not_found           = 1 " Benutzer existiert nicht
                                                      plan_variant_not_found   = 2 " Planvariante ist nicht gepflegt
                                                      time_period_not_valid    = 3 " Zeitraum ungültig
                                                      no_personal_substitution = 4 " Es handelt sich um keine persönliche Vertretung
                                                      OTHERS                   = 5 ).

[ABAP] BAPI_PR_CREATE – extensionin

METHOD fill_extension_struc 
    RETURNING value(rt_extin) TYPE bapiparex_t.

    APPEND INITIAL LINE TO rt_extin ASSIGNING FIELD-SYMBOL(<ls_extin>).

    <ls_extin>-structure    = 'BAPI_TE_MEREQITEM'.

    CALL METHOD cl_abap_container_utilities=>fill_container_c
      EXPORTING
        im_value               = is_bapi_te_mereqitem
      IMPORTING
        ex_container           = <ls_extin>+30
      EXCEPTIONS
        illegal_parameter_type = 1
        OTHERS                 = 2.
    IF sy-subrc <> 0.
      CLEAR <ls_extin>.
    ENDIF.

    APPEND INITIAL LINE TO rt_extin ASSIGNING FIELD-SYMBOL(<ls_extinx>).

    <ls_extin>-structure    = 'BAPI_TE_MEREQITEMX'.

    CALL METHOD cl_abap_container_utilities=>fill_container_c
      EXPORTING
        im_value               = is_bapi_te_mereqitemx
      IMPORTING
        ex_container           = <ls_extinx>+30
      EXCEPTIONS
        illegal_parameter_type = 1
        OTHERS                 = 2.
    IF sy-subrc <> 0.
      CLEAR <ls_extin>.
    ENDIF.

  ENDMETHOD.

[ABAP] Send mail using CL_BCS

Find related example reports in package SBCOMS.

 TRY.

        DATA(lo_sender) = cl_sapuser_bcs=>create( sy-uname ).

        DATA(lo_recipient) = cl_cam_address_bcs=>create_internet_address( i_address_string = lv_mailaddress ).

        DATA(lv_html_text) = |<BODY> | &&
                             |<p>Guten Tag,<br>| &&
                             |<br>| &&
                             |Es ist ein Fehler aufgetreten.<br>| &&
                             |Bitte schauen Sie in das Application Log.<br>| &&
                             |<br>| &&
                             |Vielen Dank!</p>| &&
                             |</BODY>|.

        DATA(lo_document) = cl_document_bcs=>create_document( i_type    = 'HTM'
                                                              i_text    = cl_document_bcs=>string_to_soli( ip_string = lv_html_text )
                                                              i_subject = |Fehler| ).

        " Erzeuge Business Communication Service und setze Objekte
        DATA(lo_send_request) = cl_bcs=>create_persistent( ).
        lo_send_request->set_sender( lo_sender ).           " Sender
        lo_send_request->add_recipient( lo_recipient ).     " Empfänger
        lo_send_request->set_document( lo_document ).       " Mailtext
        lo_send_request->set_send_immediately( abap_true ). " Mail sofort senden
        lo_send_request->send( ).                           " Sende Mail!
        COMMIT WORK.                                        " Ohne Commit wird keine Mail in der SOST auftauchen

        " Schreibe Fehler in das Log
      CATCH cx_address_bcs  INTO DATA(lx_address_bsc).
        mr_log->add_exception( i_exception = lx_address_bsc ).
      CATCH cx_send_req_bcs INTO DATA(lx_send_req_bcs).
        mr_log->add_exception( i_exception = lx_send_req_bcs ).
      CATCH cx_document_bcs INTO DATA(lx_document_bcs).
        mr_log->add_exception( i_exception = lx_document_bcs ).

  ENDTRY.

[ABAP] Log using CL_BAL_LOGOBJ

 DATA(lr_log) = NEW cl_bal_logobj( i_log_object        = 'ZMM'
                                   i_default_subobject = 'ZMM_LOGGING' ).

 lr_log->add_statustext( i_statustext = |Write a text.| ).

 lr_log->add_exception( lo_excp ).

 lr_log->add_errortext( lo_excp->get_longtext( ) ).

 lr_log->add_msg( i_probclass = '1' ).   "will use sy-msgty sy-msgid sy-msgno sy-msgv1 ...

 lr_log->save( i_client = sy-mandt ).

 lr_log->display( ).

Note: When using on EHP 7.40 you will get some confusing & & around your message texts.
https://tricktresor.de/blog/ausnahmen-mit-t100-nachricht-abap750/

Possible values for i_probclass:

[ABAP] SALV

    TRY.
        cl_salv_table=>factory( IMPORTING r_salv_table = DATA(alv_table)
                                CHANGING  t_table      = lt_display_data ).
        alv_table->display( ).

      CATCH cx_salv_msg.
    ENDTRY.