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

[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] Leave Request Approval Workflow with Escalation

During the leave request approval process the standard workflow WS12300111 does not include any deadlines. To archieve this you can simply copy the workflow and add a deadline at the approval task (Step 38). There are two possible ways to forward the approval step to the next processor:
1. When deadline is reached, forward the current workitem to the next approver.
2. When deadlines is reached, set current workitem to obsolete and create a new workitem and assign it to the next approver. Then you also have to add the new step ID to this BAdI Filter.

I selected the first option. In my demo the escalation is triggered after just three minutes.

The deadline generates a new exit on the approval step.

In this new exit you are able to call your own logic on finding the new approver, forward the workitem and inform him via mail if necessary.

To forward the request to the next approver, you have to forward the workitem and also set this new approver as next processor in the current leave request.
So first identify the next approver. I used RH_GET_LEADER for this.

*--------------------------------------------------*
* Get next approver
*--------------------------------------------------*
    CALL FUNCTION 'RH_GET_LEADER'
      EXPORTING
        plvar                     = '01'
        keydate                   = sy-datum
        otype                     = is_approver-otype
        objid                     = CONV realo( is_approver-objid )
      IMPORTING
        leader_type               = lv_leader_type
        leader_id                 = lv_leader_id
      EXCEPTIONS
        no_leader_found           = 1
        no_leading_position_found = 2
        OTHERS                    = 3.

    CHECK lv_leader_type EQ 'P' AND lv_leader_id IS NOT INITIAL.

    CALL FUNCTION 'HR_GET_USER_FROM_EMPLOYEE'
      EXPORTING
        pernr             = CONV pernr_d( lv_leader_id )
        iv_with_authority = abap_false
      IMPORTING
        user              = lv_userid_approver.

    ev_approver-otype = |US|.
    ev_approver-objid = lv_userid_approver.


Then set this new approver as next processor in your request. While doing this, it’s recommended to enqueue and dequeue the request. To get the current request object use class cl_pt_req_badi.

*--------------------------------------------------*
* Set new approver in request
*--------------------------------------------------*
    " Enqueue the request
    CALL FUNCTION 'ENQUEUE_EPTREQ'
      EXPORTING
        mode_ptreq_header = 'S'
        mandt             = sy-mandt
        request_id        = io_req->req_id
      EXCEPTIONS
        foreign_lock      = 1
        system_failure    = 2
        OTHERS            = 3.

    " Get the request object instance
    CALL METHOD cl_pt_req_badi=>get_request
      EXPORTING
        im_req_id  = io_req->req_id
      IMPORTING
        ex_request = DATA(lcl_request).

    CALL METHOD lcl_request->set_next_processor
      EXPORTING
        im_actor_type = 'P'
        im_plvar      = '01'
        im_otype      = 'P'
        im_objid      = CONV #( lv_leader_id ). " PERNR of Next Approver
    IF sy-subrc = 0.
      COMMIT WORK AND WAIT.
    ENDIF.

    " Dequeue the request
    CALL FUNCTION 'DEQUEUE_EPTREQ'
      EXPORTING
        mode_ptreq_header = 'S'
        request_id        = io_req->req_id.

Finally forward the workitem to the new approver. There of course you need the right workitem ID of the approving step.

*--------------------------------------------------*
* Forward workitem to next approver
*--------------------------------------------------*
    CALL FUNCTION 'SAP_WAPI_FORWARD_WORKITEM'
      EXPORTING
        workitem_id  = iv_wi_id                "woritem id of approving step
        user_id      = lv_userid_approver
        language     = sy-langu
        do_commit    = 'X'
      IMPORTING
        return_code  = lv_subrc.
    IF lv_subrc <> 0.
    ENDIF.

[ABAP] Restrict Select-Option to single values

INITIALIZATION.

  DATA: ls_restrictions TYPE sscr_restrict.

  APPEND VALUE #( name = 'CP' options-cp = abap_true ) TO ls_restrictions-opt_list_tab.

  APPEND VALUE #( kind    = 'S' name    = 'S_GRUND' sg_main = 'I'
                  sg_addy = ' ' op_main = 'CP'      op_addy = 'CP' ) TO ls_restrictions-ass_tab.

  CALL FUNCTION 'SELECT_OPTIONS_RESTRICT'
    EXPORTING
      restriction = ls_restrictions.

[FI] CALCULATE_TAX_FROM_GROSSAMOUNT

    DATA lt_mwdat TYPE TABLE OF RTAX1U15.

    CALL FUNCTION 'CALCULATE_TAX_FROM_GROSSAMOUNT'
      EXPORTING
        i_bukrs                 = lv_bukrs
        i_mwskz                 = lv_mwskz
*       I_TXJCD                 = ' '
        i_waers                 = lv_waers
        i_wrbtr                 = lv_wrbtr
*     IMPORTING
*       E_FWNAV                 =
*       E_FWNVV                 =
*       E_FWSTE                 =
*       E_FWAST                 =
      TABLES
        t_mwdat                 = lt_mwdat
      EXCEPTIONS
        bukrs_not_found         = 1
        country_not_found       = 2
        mwskz_not_defined       = 3
        mwskz_not_valid         = 4
        account_not_found       = 5
        different_discount_base = 6
        different_tax_base      = 7
        txjcd_not_valid         = 8
        not_found               = 9
        ktosl_not_found         = 10
        kalsm_not_found         = 11
        parameter_error         = 12
        knumh_not_found         = 13
        kschl_not_found         = 14
        unknown_error           = 15
        OTHERS                  = 16.
    IF sy-subrc <> 0 OR lt_mwdat IS INITIAL. 
      RAISE EXCEPTION TYPE zcx_xxx
        EXPORTING
          textid = zcx_xxx=>zcx_xxx
          msgv1  = SWITCH #( sy-subrc
         WHEN 1 THEN 'BUKRS_NOT_FOUND  '
         WHEN 2 THEN 'COUNTRY_NOT_FOUND'
         WHEN 3 THEN 'MWSKZ_NOT_DEFINED'
         WHEN 4 THEN 'MWSKZ_NOT_VALID'
         WHEN 5 THEN 'ACCOUNT_NOT_FOUND'
         WHEN 6 THEN 'DIFFERENT_DISCOUNT_BASE'
         WHEN 7 THEN 'DIFFERENT_TAX_BASE'
         WHEN 8 THEN 'TXJCD_NOT_VALID'
         WHEN 9 THEN 'NOT_FOUND'
         WHEN 10 THEN 'KTOSL_NOT_FOUND'
         WHEN 11 THEN 'KALSM_NOT_FOUND'
         WHEN 12 THEN 'PARAMETER_ERROR'
         WHEN 13 THEN 'KNUMH_NOT_FOUND'
         WHEN 14 THEN 'KSCHL_NOT_FOUND'
         WHEN 15 THEN 'UNKNOWN_ERROR' )
          mtype  = 'E'.
    ENDIF.

[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.