Sunday, June 26, 2016

Get the Reason Text from User Decision in Workflow

Purpose: The purpose of this document is to capture the reason text from the user decision step in workflow.

Business Scenario: When a user decision with the APPROVE and REJECT options are sent to the approver, there always comes a business need to allow the user to enter a “Rejection Reason” text, when the approver does a rejection.

In this document we will see how to use this feature and capture the rejection reason text:

Create Custom Workflow and Method to read the REJECTION Reason for Demo
We create the user decision step and mark the two branches APPROVE and REJECTED as shown:

We can mark the Reason as Mandatory or Required.
Mandatory: This means, that the Reason has to be mandatorily entered when the corresponding action is performed, else the user decision work item cannot be completed.

Required: This means, that entering the Reason is optional. The pop-up to enter the reason will appear, but it is not needed to enter anything to complete the work item.



The step is saved. When the user enters the Reason, it will get appended into the _ATTACH_OBJECTS standard container of the workflow.

This multiline element points to the SOFM Business Object.
We will now write a method to read this attachment and transfer to a text string.
This method will be an instance independent method. This means, it can be called without instantiating the BOR.
The parameters for this method are shown below

The details of the parameters are
Parameter WORKITEMID:
Parameter REASON_TXT:
We will be passing the work item ID of the workflow and this method will read the container _ATTACH_OBJECTS and pass the Rejection Text to variable REASON_TXT.

The code that you will write in the method is given below: (You can write your own code to read and process the SOFM Attachment Object as per your need)

begin_method read_rejection_reason changing container.DATA: reason_txt TYPE swcont-value,
      reason 
TYPE swc_object OCCURS 0,
      object_content 
LIKE solisti1 OCCURS 0,
      workitemid 
LIKE swr_struct-workitemid,
      subcontainer_all_objects 
LIKE TABLE OF swr_cont,
      lv_wa_reason 
LIKE LINE OF subcontainer_all_objects,
      lv_no_att 
LIKE  sy-index,
      document_id 
LIKE sofolenti1-doc_id,
      return_code 
LIKE  sy-subrc,
      ifs_xml_container 
TYPE  xstring,
      ifs_xml_container_schema 
TYPE  xstring,
      simple_container 
LIKE TABLE OF swr_cont,
      message_lines 
LIKE TABLE OF swr_messag,
      message_struct 
LIKE TABLE OF swr_mstruc,
      subcontainer_bor_objects 
LIKE TABLE OF swr_cont.

swc_get_table container 
'REASON' reason.
swc_get_element container 
'WORKITEMID' workitemid.

* Read the work item container from the work item ID
CALL FUNCTION 'SAP_WAPI_READ_CONTAINER'
  
EXPORTING
    workitem_id              = workitemid
    
language                  = sy-langu
    user                         = sy-uname
  
IMPORTING
    return_code              = return_code
    ifs_xml_container      = ifs_xml_container
    ifs_xml_container_schema = ifs_xml_container_schema
  
TABLES
    simple_container         = simple_container
    message_lines            = message_lines
    message_struct          = message_struct
    subcontainer_bor_objects = subcontainer_bor_objects
    subcontainer_all_objects = subcontainer_all_objects.

* Initialize
lv_no_att = 
0.
* Read the _ATTACH_OBJECTS element
LOOP AT subcontainer_all_objects INTO lv_wa_reason
                                                    
WHERE element = '_ATTACH_OBJECTS'.
  lv_no_att = lv_no_att + 
1.
  document_id = lv_wa_reason-
value.
ENDLOOP.

* Read the SOFM Document
CALL FUNCTION 'SO_DOCUMENT_READ_API1'
  
EXPORTING
    document_id    = document_id
  
TABLES
    object_content = object_content.


* Pass the text to the exporting parameter
IF sy-subrc = 0.
  
READ TABLE object_content INTO reason_txt INDEX 1.
  
SHIFT reason_txt BY 5 PLACES LEFT.
  swc_set_element container 
'REASON_TXT' reason_txt.
ENDIF.

end_method.

Predecessor work item will contain the Work Item ID of the User Decision Step.
The user decision step is just the previous step to this background step in this workflow template.
The workflow will look like as shown below after adding the above step in the REJECT branch of the User Decision


We run the user decision from business workplace SBWP
Now select REJECT to process rejection. Normally the work item would get completed here, but the system will generate a POP-UP to prompt for the Reason.
This pop-up will appear due to our configuration. We enter the REJECTION Reason text and press OK
(Please note, if you cancel this POP-UP, the work item will still remain in your inbox and not get completed. This is because we marked the Reason as MANDATORY)
Looking at the workflow log, we can see that the element _ATTACH_OBJECTS (Attachments) contains the SOFM entry




1 comment:

  1. You have written explicit code to remove the 1st five characters from the rejection reason. This is because the length gets captured when rejecting from SBWP. However, when rejecting from Fiori Inbox, the length is not being captured as a result, the text is being truncated. Any thoughts on this?

    ReplyDelete

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...