转 Capture Rejection Reason Text from User Decision in Workflow

Capture Rejection Reason Text from User Decision in Workflow

By Anirban Bhattacharjee, KPIT Cummins and Infosystems from Link

Purpose: The purpose of this document is to capture the rejection 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 the older SAP Releases, the standard User Decision step did not have a process to capture the rejection reason. The latest release of SAP includes this feature.

In this document we will see how to use this feature and capture the rejection reason text and also pass it to an e-mail sending step. This e-mail we can send to the original requestor so that he gets to know the cause of the rejection and resubmit for approval after the necessary corrections.

Process: This demo creation will involve the following steps

·         Create a user decision step with APPROVE and REJECT options and model the Rejection Reason only for the REJECT Branch.

·         Capture the rejection reason text and pass to an e-mail sending step via a container.

Pre-requisites: There are a few pre-requisites before doing this demo.

·         You should know how to build a basic workflow with the send e-mail step and user decision step. (SAP Technical.Com already has these tutorials)

·         SCOT and SAP Connect must be configured for e-mail sending.

·         All other workflow configurations via SWU3 are already done.

·         You know how to create methods in a custom BOR and call in workflows. (These will not be covered in details)

Create Custom Workflow and Method to read the REJECTION Reason for Demo

(Please read the tutorials on creation of Custom BOR/Method and Custom Workflow with user decisions and e-mail sending steps in SAP Technical.Com to get the details)

We create the user decision step and mark the two branches APPROVE and REJECTED as shown

转 Capture Rejection Reason Text from User Decision in Workflow

Notice that in the latest SAP version, we have an option to configure the Rejection / Approval reasons.

You can use this for any reason you want as per the branch you have created.

转 Capture Rejection Reason Text from User Decision in Workflow

We can mark the Reason as Mandatory or Required.

转 Capture Rejection Reason Text from User Decision in Workflow

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.

Here we will mark this Reason as MANDATORY for our REJECT Branch as shown

转 Capture Rejection Reason Text from User Decision in Workflow

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.

转 Capture Rejection Reason Text from User Decision in Workflow

The parameters for this method are shown below

转 Capture Rejection Reason Text from User Decision in Workflow

The details of the parameters are

Parameter WORKITEMID:

转 Capture Rejection Reason Text from User Decision in Workflow

Parameter REASON_TXT:

转 Capture Rejection Reason Text from User Decision in Workflow

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.

Note that the method we created, READ_REJECTION_REASON can be executed even without instantiating the BOR

转 Capture Rejection Reason Text from User Decision in Workflow

The above method will be called in a standard task and called in the REJECT branch of the user decision step.

The standard task will look like as shown below

转 Capture Rejection Reason Text from User Decision in Workflow

The binding in this standard task

The binding in the workflow to this task is shown as below

转 Capture Rejection Reason Text from User Decision in Workflow

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

转 Capture Rejection Reason Text from User Decision in Workflow

Now we will create an e-mail step after this “Read Rejection Reason Text”. Here we will pass our text and send as an e-mail.

For simplicity of the demo, I will be hard-coding a dummy e-mail ID to the send e-mail step.

转 Capture Rejection Reason Text from User Decision in Workflow

After this process, the complete workflow template will look as shown below

转 Capture Rejection Reason Text from User Decision in Workflow

We have not modeled anything for APPROVED branch, since we are show casing only the Rejection Reason.

You can model the Approve or any other branch as per your business need.

We are now ready to test the workflow.

We will run transaction SWUS to test the workflow

转 Capture Rejection Reason Text from User Decision in Workflow

We run the user decision from business workplace SBWP

转 Capture Rejection Reason Text from User Decision in Workflow

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

转 Capture Rejection Reason Text from User Decision in Workflow

(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

转 Capture Rejection Reason Text from User Decision in Workflow

Now looking at the e-mail step in the log, we will check the task description to see if our text is captured

转 Capture Rejection Reason Text from User Decision in Workflow

You can see the Rejection Reason is captured. We can see this in the container also

转 Capture Rejection Reason Text from User Decision in Workflow

Now running the SOST transaction to see if the same appears in the e-mail body as well

转 Capture Rejection Reason Text from User Decision in Workflow

转 Capture Rejection Reason Text from User Decision in Workflow

So we have successfully captured the REJECTION REASON TEXT and displayed it in the e-mail body.

You can use this functionality in multitude of other ways as per your business need in your project.

你可能感兴趣的:(workflow)