Sap Abap Workflow

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Workflow Tutorial

In this tutorial, we are using budget application for approvals.

There are following points will be cover,

1. Create BO Object (SWO1)

2. Create Workflow and Assign to BO it.

3. Trigger Workflow Code

4. Copy Standard Task and Adding Description & Attachment to User Decision Task (PFTC_COP)

5. Call Transaction with Key Field (Budget Request#) Attachment Number.

6. Creating Approval Table & Add entries(se11)

7. Creating Method for Getting Agent (SWO1)

8. Creating Background Task for Getting Agents List(Approver List) (SWO1)

9. Assigning Agent to User Decision Task(SWO1)

10. Creating Background Task /Methods (Update Approved Status at the End of All Approvals)(SWO1)
Tcode: ZBUDG01 -> Create Budget Request, ZBUDG03 -> Display Data .

Table: zbudg_req

First of all we are creating Business object (TCode: SWO1 - Object-Name: ZBUSBUDGR) for this
application's workflow, we will use this in the workflow event to trigger the workflow.

When creating BO: ZBUSBUDGR, Fill the required data as well as like,

Click ok. Assign a relevant package and Transport.

1. First of all, we created Fields key (ReqID) using Dictionary Table like (ZBUDG_REQ).
2. Now we created Event ID like Created.
3. Now we need to change the status of BO, Event form Modeled to Implement or Released.

But here the difference between that Implement and Released is that, Implement object can be
edit/modify but Released object can't modify. so here we need to choose Implement until our BO do
not final.

To Implement BO , Select BO , Choose from Menu EDIT-> Change Release Status -> Object Type -> To
Implemented.

To Implement Methods/Events Select , Choose From Menu EDIT-> Change Release Status -> Object
Type Components -> To Implemented.
After that Press Button to generate the BO. Here no need to implement the key field. Just go back and
Display / Edit BO. The status of Key-Field from Modeled to Implemented automatically changed.

4. Now we creating Workflow (zwf_budgreqt) using (TCode: SWDD) with user Decision Task.

First of all, we assigned event (Created) which is used to trigger workflow. Menu -> Goto -> Basic Date
and then assign BO (ZBUSBUDGR) with event(Created) and Save it.

5. Now we created Workflow element same like BO(ZBUSBUDGR) to pass data from event to Workflow
and workflow to event. Means this element will have Import and export characterises. After that Bind
the Event Binding and active it.
5. Now we want to insert SAP Standard Screen for approvals, we need to use User Decision task. Right
Click on the blank place and select User Decision task.
Click Save and Activate it.

Triggering Workflow
Now we just need to trigger Workflow. So added the logic of code in Application code.

DATA: ld_object_type LIKE swr_struct-object_typ,


ld_object_key LIKE swr_struct-object_key,
ld_event LIKE swr_struct-event,
ld_ret TYPE sy-subrc.

ld_object_type = 'ZBUSBUDGR'. " BO Object


ld_object_key = zstbudg_req-budg_reqid. " Key Field in which workflow
baseed on.
ld_event = 'CREATED'. " BO Event

CALL FUNCTION 'SAP_WAPI_CREATE_EVENT'


EXPORTING
object_type = ld_object_type
object_key = ld_object_key
event = ld_event
* COMMIT_WORK = 'X'
* EVENT_LANGUAGE = SY-LANGU
* LANGUAGE = SY-LANGU
* USER = SY-UNAME
* IFS_XML_CONTAINER =
IMPORTING
return_code = ld_ret
* EVENT_ID =
* TABLES
* INPUT_CONTAINER =
* MESSAGE_LINES =
* MESSAGE_STRUCT =
.
IF LD_RET <> 0.
ROLLBACK WORK.
MESSAGE 'Error When Starting Work Flow' TYPE 'E'.
ENDIF.

Save the entry from application and check the workflow is triggered or not. Enter TCode: SWIA and
check the desired result like this.

Also we can check the workflow log or workflow item container values etc. Enter TCode: SBWP and
check the workflow inbox. click workflow and select desired item and double click on it. after that we
can Approve or Reject etc.

Copy Standard Task and Adding Description & Attachment to User Decision Task
Now we need to change the description of User-Decision task. But we can't change the standard. So we
need to copy the Standard User Decision Task.
Copy the User Decision Task or workflow template, Use the TCode: PFTC_DIS => Display, PFTC_COP =>
Copy.

Enter the Standard Task number & Select the Standard Task from Task Type and press Copy Button.
After Copied we got Custom Task with Number Now we can change it according to our requirements.
Now changes from the copied task, Enter Tcode: PFTC_CHG. Click the Edit Button and

make the required changes.

Click the Description Tab and press the EDIT and make the description as per your requirements like this.
Now need to add Attachment of Budget Request # to Task so here we created the BO Object only with
Import Parameter in the container of the Task.

After created the task press save button.

Now Copied the Custom Task Number and replace standard task number with custom task number.
Here only need Task <=> Windows Binding elements, Select these Binding elements and unselect other
Binding elements.
Press OK button. After that Press the Task binding Button.
Press Ok button and activate the workflow. Now we have to go assign the Possible Agents to task.
Click on Agent Assignment Button from the Task Properties. Select the Task and Press Attribute Button,
Select General Task and Press Transfer. And go back to the task. Save and activate the Workflow.

Again Save new entry from the ZBUDG01. Workflow will automatically trigger at the Save Time.

Now we seen, Attachment is shown but only display the Field Key (Request Number).
Call Transaction with Key Field (Budget Request#) Attachment Number.
Enter Transaction: SWO1 and open the BO which is created for Workflow. Clicked on Display Method
and Press the Redefine Method. It will be while from red mean it is available for code. Press the Program
Button.

Write the following code in the Display Method.

DATA: ID_REQID TYPE ZBUDG_REQ-BUDG_REQID.

ID_REQID = OBJECT-KEY-REQID.

SET PARAMETER ID 'ZBUDGID' FIELD ID_REQID.

CALL TRANSACTION 'ZBUDG03' AND SKIP FIRST SCREEN. " Transaction to display data from
attachment

SET PARAMETER ID 'ZBUDGID' FIELD SPACE.

Add Module in PBO (ZBUDG03). and write the following code in this module to get the Key-Field Value
from Workflow Attachment.

DATA: id_reqid TYPE zbudg_req-budg_reqid.


GET PARAMETER ID 'ZBUDGID' FIELD id_reqid.
zstbudg_req-budg_reqid = id_reqid.
Now we checked, we got our desired output.

Creating Approval Table


We created the Approval Table for Approver List.

We added data into Table using SM30.


Creating Method for Getting Agent
Open BO from Transaction: SWO1, Select Methods, then Select Create Button, then Press No from Pop-
up Screen.

Now created the Method, GetApprover to get the approver list. It is background task so uncheck the
Dialog and remain the synchronous checked. Select the GetApprover Method and Press the Parameters
Button.

Now Created the Parameters, one is for to get the agents list and 2nd for the counting for Agents. Both
are exported Parameters and Both are Created without the Reference of Dictionary.
After Creating these Parameters, Select Method GetApprover, then Select Program, then a pop-up
window will appear, "Create Program with Reference to Functional Etc., Click yes. and Paste the Below
code into the GetApprover Method. After that Implement the method and Generate the BO.

DATA:
get_approv LIKE swhactor OCCURS 0,
numb_appr TYPE swastdtyp-wfcounter,
wa_agent LIKE LINE OF get_approv.

DATA: it_mapper TYPE TABLE OF zbudgreq_mapper,


lwa_mapper LIKE LINE OF it_mapper.

SELECT * INTO CORRESPONDING FIELDS OF TABLE it_mapper


FROM zbudgreq_mapper.

SORT it_mapper BY appr_level.

LOOP AT it_mapper INTO lwa_mapper.


CLEAR: wa_agent.

wa_agent-otype = 'US'.
wa_agent-objid = lwa_mapper-approver.
APPEND wa_agent TO get_approv.
ENDLOOP.

DESCRIBE TABLE get_approv LINES numb_appr.

swc_set_table container 'Get_approv' get_approv.


swc_set_element container 'numb_appr' numb_appr.

Creating Background Task for Getting Agents List(Approver List)


Create New Task (Background) using Right Click, New Create andSelect before the User-
Decision Task like this. Performs the automatic Binding.

When we have to go back, This pop-up screen will appear. Select ok same as it is.. After that save the
workflow and activate it.
Assigning Agent to User Decision Task
Now need to pass tab_agents (Approver List) to user Decision Task for Approvals. for this we have to
need implement Loop structure. So First Created Looping Variable (Loop_Inc) With INT1 with Default
Value 1 and 2nd we Created Rejected With CHAR1, Which will be use in the condition.

Now Right Click on User-Decision Task and Select Create , then Select , Press In-Loop
Button from Pop-up.

Add the condition according to your requirements like this , and Press Ok Button.
After Created Loop, Right Click on the Reject Condition, Right Click on the Create, and Select
and set the Condition like this and Press OK Button.

Again Click on the Loop Agents Node and Process the previous steps , Select and
Set the Conditions Like this and Press Ok Button.
Now Double Clicked on the User-Decision Task. and Add the Agents Table like this.

Click OK Button. Save the Workflow and Activate it. Save the Entry in Application. And Checks the
workflow. It will be Working fine.

Creating Background Task /Methods (Update Approved Status at the End of All
Approvals)
Now created the Method, UpdateStatusApproved to update the Status for Approval Like In-Process to
Approved. It is background task so uncheck the Dialog and remain the synchronous checked. There is no
need to Import and Export Parameters.
Now Click the Menthod, UpdateStatusApproved and Press the Program Button. This Window will Pop-
UP. Click the Yes.

Write the following Code into the Method Templalte like this.

UPDATE ZBUDG_REQ SET REQ_STATUS = 'A'

WHERE BUDG_REQID EQ OBJECT-KEY-REQID.

IF SY-SUBRC EQ 0.

COMMIT WORK.

ELSE.
ROLLBACK WORK.

EXIT_RETURN 1000 'Updating Status' SPACE SPACE SPACE.

ENDIF.

After that Change the status of method from modeled to Implement. Now here need to generate the
error in case of updating failed, we will do it using method exception.

Click on the Method, UpdateStatusApproved, then Pressed the Exceptions Button. Add the Exception
Like this and we will use it already in the above code. After that Generate BO.

Now Created Background Task at the End of Workflow, which will update the Approved/Rejected Status
at the end of final Approver action. Select the end of Loop, Press Right Click and Click Create and select
Button.
Press Ok Button. Now Select Approved and Press Click Right and Select and Create New
Background Task and set the method like this.
Click Ok Button and Save the Workflow and Activate It. After that we tested it. It's Working Fine.

You might also like