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

[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] Workflow mail step -> Add attachment

Recently I got the demand to enhance the leave request approvel workflow with some mail steps. These mails should also include the attachments, which can be uploaded in the “Leave Request” Fiori App.

To achive this, I used the exit functionality in the mail step.

There you have to provide a class name. The class has to implement the following interface:

IF_SWF_IFS_WORKITEM_EXIT

You will get a method you can implement.

This Method has two parameters, the “Event Name” and the “Workitem Context”.

With the event, you are able to control, in which situations your exit should be fired. In my case, I just need my code run when the workitem is created (event “CREATED”). You’ll find all usable events in domain SWW_EVTTYP.

To get and pass back the attachment to the mail step, a few things need to be done.
First, get the curent workitem container and check, if there is already any attachment. If not, read the request id from the leave request. With this request id, you are able to find the attachment you need. I’ve already written a method for that, you’ll find below. Then convert the xstring to a solix table, create the attachment document with function module “SO_DOCUMENT_INSERT_API1” and “SWO_CREATE” and finally pass it to the container.

 METHOD if_swf_ifs_workitem_exit~event_raised.

    DATA: lv_container     TYPE REF TO if_swf_ifs_parameter_container,
          lv_attach        TYPE TABLE OF obj_record,
          lv_folder_id     TYPE soodk,
          wa_document_info TYPE sofolenti1,
          lv_data          TYPE sodocchgi1,
          lv_objtype       TYPE swo_objtyp,
          lv_objkey        TYPE swo_typeid,
          lv_return        TYPE swotreturn,
          lv_sofm          TYPE swo_objhnd,
          lv_objject       TYPE obj_record,
          tb_obj           TYPE TABLE OF obj_record,
          it_solix_tab1    TYPE solix_tab,
          req_id           TYPE tim_req_id,
          lv_doc_type      TYPE so_obj_tp.

    CHECK im_event_name = 'CREATED'.

* Fetch Container

    lv_container = im_workitem_context->get_wi_container( ).

* Read attachment to confirm that there is no duplication

    TRY.
        lv_container->get(
          EXPORTING
            name  = '_ATTACH_OBJECTS'
          IMPORTING
            value = lv_attach ).

      CATCH: cx_swf_cnt_elem_not_found,
             cx_swf_cnt_elem_type_conflict,
             cx_swf_cnt_unit_type_conflict,
             cx_swf_cnt_container.
    ENDTRY.

    CHECK lv_attach IS INITIAL.

    CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
      EXPORTING
        owner     = sy-uname
        region    = 'B'
      IMPORTING
        folder_id = lv_folder_id.

* Get Request ID

    lv_container->get(
      EXPORTING
        name       = 'RequestId'        
      IMPORTING
        value      = req_id ).          

* Get XSTRING of attachment

    zcl_xxx=>get_req_attachment(
      EXPORTING
        iv_req_id     = req_id                
      IMPORTING
        ev_xstring    = DATA(xstring)
        es_attachment = DATA(ls_attachment) ).

    CHECK xstring IS NOT INITIAL.

* Get document type

    SELECT SINGLE doc_type FROM toadd INTO @lv_doc_type
       WHERE mimetype EQ @ls_attachment-file_type.
    IF sy-subrc NE 0.
      lv_doc_type = 'PDF'. "If doc_type not found, at least try with pdf.
    ENDIF.

* Create and set document

    it_solix_tab1 = cl_document_bcs=>xstring_to_solix( xstring ).

* Creating First attachment

    lv_data-obj_name   = ls_attachment-file_name.
    lv_data-obj_descr  = ls_attachment-file_name.
    lv_data-obj_langu  = sy-langu.
    lv_data-sensitivty = 'P'.
*    lv_data-doc_size   = ls_attachment-file_size.

    CALL FUNCTION 'SO_DOCUMENT_INSERT_API1'
      EXPORTING
        folder_id                  = lv_folder_id
        document_data              = lv_data
        document_type              = lv_doc_type
      IMPORTING
        document_info              = wa_document_info
      TABLES
        contents_hex               = it_solix_tab1
      EXCEPTIONS
        folder_not_exist           = 1
        document_type_not_exist    = 2
        operation_no_authorization = 3
        parameter_error            = 4
        x_error                    = 5
        enqueue_error              = 6
        OTHERS                     = 7.

* Populate object type and object key for create an instance

    lv_objtype = 'SOFM'.
    lv_objkey  = wa_document_info-doc_id.

    CALL FUNCTION 'SWO_CREATE'
      EXPORTING
        objtype           = lv_objtype
        objkey            = lv_objkey
      IMPORTING
        object            = lv_sofm
        return            = lv_return
      EXCEPTIONS
        no_remote_objects = 1
        OTHERS            = 2.

* Prepare for attaching the object to container

    lv_objject-header  = 'OBJH'.
    lv_objject-type    = 'SWO'.
    lv_objject-handle  = lv_sofm.
    APPEND lv_objject TO tb_obj.

*—can be used for other workitems

    lv_container->set(
      EXPORTING
        name  = '_ATTACH_OBJECTS'
        value = tb_obj[] ).

*–this will add the attachment to email

    lv_container->set(
      EXPORTING
        name  = 'ATTACHMENTS'
        value = tb_obj[] ).

* Commit the changes

    im_workitem_context->do_commit_work( ).

  ENDMETHOD.

The following function modules are needed to read the leave request attachment:

  METHOD get_req_attachment.

    DATA: lv_has_errors     TYPE  ptreq_has_error_flag,
          lt_messages       TYPE  ptarq_uia_messages_tab,
          lt_attachments    TYPE  hress_t_ptarq_att_info,
          ex_has_attachment TYPE  boole_d.

    CALL FUNCTION 'PT_ARQ_ATTACHMENT_GET'
      EXPORTING
        im_request_id     = iv_req_id
*       IM_VERSION_NO     =
*       IM_DOCUMENT_TYPE  =
      IMPORTING
        ex_has_errors     = lv_has_errors
        ex_messages       = lt_messages
        et_attachments    = lt_attachments
        ex_has_attachment = ex_has_attachment.

    CHECK ex_has_attachment EQ abap_true.

    TRY.
        es_attachment = lt_attachments[ 1 ].
      CATCH cx_sy_itab_line_not_found.
    ENDTRY.


    CALL FUNCTION 'PT_ARQ_ATTACHMENT_DETAIL_GET'
      EXPORTING
        iv_arc_doc_id   = es_attachment-archiv_doc_id
*        iv_doc_type     =
      IMPORTING
        ex_file_content = ev_xstring
        ex_has_errors   = lv_has_errors
        ex_messages     = lt_messages.

  ENDMETHOD.

[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] 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] BAPI_ACC_DOCUMENT_POST

How to handle BAPI_ACC_DOCUMENT_POST with a document split at position 980. The method create_document fills the necessary tables and calls post_document, if it reaches position 980, to post the current document. Then the method create_document calls itself recursiv for the next positions.

  METHOD create_document.

    DATA: lt_pos                    TYPE TABLE OF bapiacgl09,
          lt_pos_amt                TYPE TABLE OF bapiaccr09.

    gr_listlog->append_log_entry( iv_type = gr_listlog->gc_log_type_info
                                  iv_text = |Beginne Belegerstellung|
                                  iv_alog = abap_true ).

    "Belegkopf
    DATA(ls_documentheader) = VALUE bapiache09( comp_code   = gs_sel-p_bukrs
                                                header_txt  = |header text|
                                                pstng_date  = sy-datum
                                                doc_date    = sy-datum
                                                fisc_year   = sy-datum(4)
                                                fis_period  = sy-datum+4(2)
                                                doc_type    = gs_sel-p_blart
                                                username    = sy-uname ).
    "Fülle Positionen
    LOOP AT it_xxx ASSIGNING FIELD-SYMBOL(<ls_xxx>).

      APPEND VALUE #( itemno_acc     = lines( lt_pos ) + 1
                      gl_account     = <ls_xxx>-hkont
                      stat_con       = abap_false
                      acct_type      = 'S'                      "Sachkonto
                      bus_area       = '0030'
                      fis_period     = sy-datum+4(2)
                      fisc_year      = sy-datum(4)
                      pstng_date     = sy-datum
                      value_date     = sy-datum
                      item_text      = |item text|
                      alloc_nmbr     = <ls_xxx>-zuonr
                      doc_type       = <ls_xxx>-blart
                      comp_code      = <ls_xxx>-bukrs
                      func_area      = '0030'
                      costcenter     = <ls_xxx>-kostl
                      tax_code       = <ls_xxx>-mwskz ) TO lt_pos.

      APPEND VALUE #( itemno_acc   = lines( lt_pos_amt ) + 1
                      curr_type    = '00'                       "Belegwährung
                      currency     = |EUR|
                      amt_doccur   = <ls_xxx>-betrg ) TO lt_pos_amt.

      "Belegsplit notwendig?
      IF lines( lt_pos ) = 980.
        gr_listlog->append_log_entry( iv_type = gr_listlog->gc_log_type_statistic
                                      iv_text = |Belegsplit notwendig!|
                                      iv_alog = abap_true ).

        "Verbuche die aktuellen Positionen
        post_document( is_documentheader = ls_documentheader
                       it_pos            = lt_pos
                       it_pos_amt        = lt_pos_amt ).

        "Rekursiver Aufruf der Methode! Nur mit den Sätzen die noch nicht verarbeitet wurden
        create_document( it_xxx = FILTER #( it_xxx USING KEY sort_key WHERE xxx = xxx ) ).

        "Verlasse bei Rekursion hier
        RETURN.
      ENDIF.



    ENDLOOP.


    "Wenn alle Positionen hinzugefügt, lege den Beleg an
    post_document( is_documentheader = ls_documentheader
                   it_pos            = lt_pos
                   it_pos_amt        = lt_pos_amt ).

  ENDMETHOD.

Calling the BAPI:

  METHOD post_document.

    DATA lt_return            TYPE STANDARD TABLE OF bapiret2.

    IF gs_sel-p_test = abap_true.
      gr_listlog->append_log_entry( iv_type = gr_listlog->gc_log_type_info
                                    iv_text = |Testlauf: Keine Belegbuchung|
                                    iv_alog = abap_true ).
      RETURN. "Im Testlauf hier verlassen
    ENDIF.

    SET UPDATE TASK LOCAL.

    CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
      EXPORTING
        documentheader = is_documentheader
      TABLES
        accountgl      = it_pos
        currencyamount = it_pos_amt
        return         = lt_return.

    "Protokollzeilen mit BAPI Meldungen hinzufügen
    LOOP AT lt_return ASSIGNING FIELD-SYMBOL(<ls_return>).
      gr_listlog->append_log_entry( iv_type = <ls_return>-type
                                    iv_text = CONV #( <ls_return>-message )
                                    iv_alog = abap_true ).
    ENDLOOP.

    "Prüfen, ob eine Fehlermeldung vorliegt
    IF line_exists( lt_return[ type = 'A' ] ) OR line_exists( lt_return[ type = 'E' ] ).
      "Rollback
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    ELSE.
      "Alles gut -> commit
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.

      "Protokollzeile mit der Erzeugten Belegnummer nochmal explizit ins Log
      DATA(lv_documentnumber) = lt_return[ 1 ]-message_v2(10). "hier steht laut Doku die Belegnummer drin
      gr_listlog->append_log_entry( iv_type = gr_listlog->gc_log_type_success
                                    iv_text = |Belegnummer: { lv_documentnumber }|
                                    iv_alog = abap_true ).
    ENDIF.

  ENDMETHOD.

[ABAP] Clean Code

ABAP Clean Code


DRY – Don’t repeat yourself

KISS – Keep it simple, stupid

YAGNI – You ain’t gonna need it

PEBKAC – Problem Exist Between Keyboard And Chair

SoC – Separation of concerns


Issues generally come in three forms:

  1. syntax errors that prevent a program from running
  2. runtime errors when code fails to execute or has unexpected behavior
  3. semantic (or logical) errors when code doesn’t do what it’s meant to