Sample ABAP Function for Generating Workflow DCN With Payload

This WF-DCN sample illustrates WF-DCN with payload.

FUNCTION ZSUP_MWF_PUSH_WORKITEM.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(DESTINATION) TYPE  ZSUP_MWF_CHAR20
*"     VALUE(QNAME) TYPE  ZSUP_MWF_CHAR20
*"     VALUE(QCOUNT) TYPE  ZSUP_MWF_INT4
*"     VALUE(SUPSECURITY) TYPE  ZSUP_MWF_CHAR255CS
*"     VALUE(ACTION) TYPE  ZSUP_MWF_CHAR1
*"     VALUE(WORKITEM) TYPE  ZSUP_MWF_CHAR32
*"     VALUE(COMPLETE) TYPE  ZSUP_MWF_CHAR1 OPTIONAL
*"     VALUE(PRIORITY) TYPE  ZSUP_MWF_CHAR1 OPTIONAL
*"     VALUE(MARKREAD) TYPE  ZSUP_MWF_CHAR1 OPTIONAL
*"     VALUE(SENDER) TYPE  ZSUP_MWF_CHAR255CS OPTIONAL
*"     VALUE(RECIPIENT) TYPE  ZSUP_MWF_CHAR20 OPTIONAL
*"     VALUE(SUBJECT) TYPE  ZSUP_MWF_CHAR255CS OPTIONAL
*"     VALUE(BODY) TYPE  ZSUP_MWF_STRINGCS OPTIONAL
*"  TABLES
*"      SENDERTEXTS STRUCTURE  ZSUP_MWF_PARAM OPTIONAL
*"      RECIPIENTS STRUCTURE  ZSUP_MWF_PARAM OPTIONAL
*"----------------------------------------------------------------------

  "* if complete indicator is set, we directly generate
  "* json and execute the push to the http endpoint


  IF complete = 'X'.

    DATA lv_json_notif TYPE zsup_mwf_stringcs.
    "* generate a workitem notification body
    CALL FUNCTION 'ZSUP_MWF_GEN_NOTIF_JSON'
      EXPORTING
        notif_id = workitem
        delete = action
        markread = markread
        priority = priority
        to = recipient
        from = sender
        subject = subject
        body = body
      IMPORTING
        json_out = lv_json_notif.

    "* execute http push function module using specified destination
    CALL FUNCTION 'ZSUP_MWF_HTTP_POST'
      EXPORTING
        destination = destination
        supsecurity = supsecurity
        msg_body = lv_json_notif.

    RETURN.
  ENDIF.

  DATA lv_trfcqnam TYPE trfcqnam VALUE IS INITIAL.
  DATA lv_xubname TYPE xubname VALUE IS INITIAL.
  DATA ls_userdefault TYPE bapidefaul VALUE IS INITIAL.
  DATA lv_lang_iso TYPE t002-laiso VALUE IS INITIAL.
  DATA lv_wiid TYPE sww_wiid VALUE IS INITIAL.
  DATA lv_wi_text TYPE witext VALUE IS INITIAL.
  DATA lv_return_code TYPE sysubrc VALUE IS INITIAL.
  DATA lv_subject TYPE zsup_mwf_stringcs VALUE IS INITIAL.
  DATA lv_body TYPE zsup_mwf_stringcs VALUE IS INITIAL.
  DATA lv_body2 TYPE zsup_mwf_stringcs VALUE IS INITIAL.
  DATA lv_sender TYPE zsup_mwf_char255cs VALUE IS INITIAL.
  DATA lv_sendertext TYPE zsup_mwf_param VALUE IS INITIAL.
  DATA ls_recipient TYPE zsup_mwf_param VALUE IS INITIAL.
  DATA lv_recipient TYPE zsup_mwf_char20 VALUE IS INITIAL.
  DATA lt_message_struct TYPE TABLE OF swr_mstruc.
  DATA lt_alternatives TYPE TABLE OF swr_decialts.
  DATA lt_user_parameter TYPE TABLE OF bapiparam.
  DATA ls_user_parameter TYPE bapiparam VALUE IS INITIAL.
  DATA lt_bapiret2 TYPE TABLE OF bapiret2.

  "* loop over all entries in the recipient list
  "* and determine the user language, translate
  "* the workitem text into the users language
  "* and call the function module again with the
  "* complete indicator set to push individual
  "* notifications to configured destination
  LOOP AT recipients INTO ls_recipient.

    IF ls_recipient-pkey = 'US'.
      "* read user defaults to determine user
      "* default language and if user is mobile
      "* enabled
      MOVE ls_recipient-pvalue TO lv_xubname.
      CALL FUNCTION 'BAPI_USER_GET_DETAIL'
        EXPORTING
          username = lv_xubname
        IMPORTING
          defaults = ls_userdefault
        TABLES
          parameter = lt_user_parameter
          return = lt_bapiret2.

      "* set output language to user default,
      "* if not set, set temporarily to default
      IF NOT ls_userdefault-langu IS INITIAL.
        SET LANGUAGE ls_userdefault-langu.
      ELSE.
        ls_userdefault-langu = sy-langu.
      ENDIF.

      "* check returned BAPIRET2 table for any error
      "* or abort entries, stops execution in case
      "* an error or abort entry is found
      PERFORM check_bapiret2 TABLES lt_bapiret2.

      "* initialize recipient name to username
      MOVE lv_xubname TO lv_recipient.

      "* check if user is mobile enabled (ZSUP_ENABLED)
      "* and set the recipient name to the mobile username
      lv_return_code = 1.
      LOOP AT lt_user_parameter INTO ls_user_parameter.
        IF ls_user_parameter-parid = 'ZSUP_MWF_ENABLED' AND
          ls_user_parameter-parva = 'X'.
            lv_return_code = 0.
        ENDIF.
      ENDLOOP.

      "* if the user is not mobile enabled
      "* continue with the next entry in the
      "* recipient list
      IF lv_return_code <> 0.
        CONTINUE.
      ENDIF.

      "* if the notification push is a delete skip the
      "* determination of further content for the push such
      "* as localized subject and sender, etc.
      IF NOT action = 'D'.

        "* translate user language code to iso
        CALL FUNCTION 'LANGUAGE_CODE_SAP_TO_ISO'
          EXPORTING
            sap_code  = ls_userdefault-langu
          IMPORTING
            iso_code  = lv_lang_iso
          EXCEPTIONS
            not_found = 1
            OTHERS    = 2.

        "* output MESSAGE to stop execution in case of
        "* an error during language code conversion
        IF sy-subrc <> 0.
          MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno.
        ENDIF.

        "* read decision in users configured default language
        MOVE workitem TO lv_wiid.
        CALL FUNCTION 'SAP_WAPI_DECISION_READ'
          EXPORTING
            workitem_id = lv_wiid
            language = ls_userdefault-langu
            user = lv_xubname
          IMPORTING
            return_code = lv_return_code
            decision_title = lv_wi_text
          TABLES
            alternatives = lt_alternatives
            message_struct = lt_message_struct.

        "* check returned MESSAGE_STRUCT table for any error
        "* or abort entries, stops execution in case
        "* an error or abort entry is found
        IF lv_return_code <> 0.
          PERFORM check_message_struct TABLES lt_message_struct.
        ENDIF.

        "* copy localized version of the decision title
        "* into local subject valiable
        MOVE lv_wi_text TO lv_subject.

        "* loop over all available configured l10n versions
        "* of the sender field and determine the match
        "* for the particular users default language or
        "* use the configured default sender text
        LOOP AT sendertexts INTO lv_sendertext.
          IF lv_sendertext-pkey CS lv_lang_iso.
            lv_sender = lv_sendertext-pvalue.
            EXIT.
          ELSEIF lv_sendertext-pkey = ''.
            lv_sender = lv_sendertext-pvalue.
          ENDIF.
        ENDLOOP.


        "* complete the fixed workitem notification body
        "* used for matching and paramater value extract
        "* in the SUP notification processing
        CONCATENATE body ', USER:' lv_xubname '*#END#*' INTO lv_body.

        DATA lv_decision TYPE ZSUP_MWF_CHAR255CS VALUE IS INITIAL.
        DATA lv_name TYPE ZSUP_MWF_CHAR20 VALUE IS INITIAL.
        DATA lv_description TYPE ZSUp_MWF_STRINGCS VALUE IS INITIAL.
        DATA lv_alter TYPE TABLE OF ZSUP_MWF_PARAM .
        DATA lv_return TYPE TABLE OF BAPIRET2.
        DATA ltater TYPE  ZSUP_MWF_PARAM VALUE IS INITIAL.
        Move ls_recipient-pvalue to lv_name.
        Call Function 'ZSUP_MWF_WI_GETDETAIL'
        Exporting
           USERNAME = lv_name
           WORKITEM = workitem
        IMPORTING
            DECISION = lv_decision
            Description = lv_description
        TABLES
          RETURN = lv_return
          ALTERNATIVES = lv_alter.


        CONCATENATE lv_body '","data":[{"id":"'workitem'","pkg":"sup_mwf:1.2","messages":[{"id":"'workitem'","mbo":"Workitem","op":":upsert","cols":{"WORKITEM":"'workitem
        '","USERNAME":"'lv_xubname'","DESCRIPTION":"'lv_description'","DECISION":"'lv_decision'"}}'Into lv_body.
        loop at lv_alter into ltater.

        Concatenate lv_body  ',{"id":"'workitem'","mbo":"Alternatives","op":":upsert","cols":{"WORKITEM":"'workitem
        '","USERNAME":"'lv_xubname'","PKEY":"'ltater-PKEY'","PVALUE":"'ltater-PVALUE'"}}' Into lv_body.
        EndLoop.
        Concatenate lv_body ']}],'  INTO lv_body.

      ENDIF.

      "* if qname was not set skip
      "* and execute as tRFC
      IF qname <> ''.
        "* if qcount was specified hash username
        "* into one of the possible queues before
        "* setting the queue name for qRFC
        IF qcount > 0.
          PERFORM hash_user_to_queue USING lv_xubname qcount CHANGING qname.
        ENDIF.
        MOVE qname TO lv_trfcqnam.
        CALL FUNCTION 'TRFC_SET_QUEUE_NAME'
          EXPORTING
            QNAME = lv_trfcqnam.
      ENDIF.

      "* execute push function module in the background
      "* with complete flag set
      CALL FUNCTION 'ZSUP_MWF_PUSH_WORKITEM' IN BACKGROUND TASK AS SEPARATE UNIT
        EXPORTING
          destination = destination
          qname = qname
          qcount = qcount
          supsecurity = supsecurity
          action   = action
          workitem = workitem
          complete = 'X'
          priority = priority
          markread = markread
          sender = lv_sender
          recipient = lv_recipient
          subject = lv_subject
          body = lv_body.

    ENDIF.
  ENDLOOP.

  "* tRFC/qRFC requires explicit commit
  COMMIT WORK.

ENDFUNCTION.