See how to develop a dynamic email distribution list to use during workflow execution. With a multilevel approval workflow, users involved in this process are added automatically into the email distribution list as soon as the approval level increases. The list defined in the workflow container is filled, and emails are sent to approvers for rejection or approval.
Key Concept
A dynamic email distribution list allows you to send email notifications to only concerned people instead of sending notifications to all workflow approvers by adjusting settings in the system.
You can fill a predefined email list during execution of workflow so that emails are sent only to those involved in a particular approval-or-rejection process. For example, if an approver at the first level rejects the document to be approved, then the rejection email is sent only to the initiator of the workflow. If the first approver approves it, then the first approver is included in this list of email recipients. At the second level if the approver rejects the document, an email is sent to the initiator and first approver. If the second-level approver approves it, then that name is also added to the distribution list for future emails.
I explain how to develop three levels of approval workflow, how to create a dynamic distribution list, and how to test it. This technique is intended for SAP workflow developers.
To develop a three-level workflow, log into the SAP system and open transaction code SWDD (Figure 1).

Figure 1
Workflow designer
Click the header icon
on the application toolbar to open a new window. Enter an abbreviation and short description of the workflow, such as workflow for three-level approval (Figure 2).

Figure 2
Header window
Click the back icon to return to the main designer screen. Save the workflow by pressing Ctrl-S. You receive a workflow number automatically and it is saved, as confirmed in Figure 3.

Figure 3
Workflow is saved
Now you define an email distribution list in the workflow container. Go back to the screen in Figure 1 and double-click the Double-Click to Create item in the Workflow Container area or right-click the container window and select Create in the context window (Figure 4).

Figure 4
Create a new container element
In my example I name my distribution list object name DistList (Figure 5). Enter a short description of this object and select ABAP Dict. Data Type. Enter SWRAGENT as the ABAP dictionary structure to store the agent type and ID.

Figure 5
Define the distribution list type used for recipients of the email
Now go to the Properties tab and select the Export and Multiline check boxes so you can store multiple email recipients and export the list to the email sending task in the workflow (Figure 6).

Figure 6
Set the properties of the distribution list
Click the green check mark icon. You now have a distribution list that can be filled dynamically, enabling you to fill it during the workflow execution. Using the Container operation, you can add the workflow initiator’s name in this list to send emails. Return to the main designer screen shown in Figure 1. Right-click the first vertical arrow line in the designer after the Workflow started box. Click the Create… option in the context menu and then select Container operation (Figure 7).

Figure 7
Add the container operation
After you click the green check mark icon, you see the properties page (Figure 8). Click the Control tab and enter the Step Name as Add_initiator. In the Operation area in the Assignment field, select the Add only to table option and press Enter. In the Result Element field, click the help icon and select the DistrList object. In the Expression field select the workflow initiator object &_WF_INITIATOR&.

Figure 8
Control tab of the container operation
Click the green check mark icon. You now see a step inserted in your workflow.
For the first approver process you have to insert a user decision step in your workflow. On the right side of the screen in Figure 7 you see different step types available. Right-click the line next to the container operation step. Click Create... and select the user decision step. A dialog box appears for properties.
In the Title field enter the title you want to display when this workflow is presented to the approving user (Figure 9). In the Agents section select the agent type US USER. Select the approving user name for the level 1 approver (e.g., UAT-PS-01). In the Decision Options table enter Approve and Reject to make these options available to the user. Click the green check mark icon.

Figure 9
The User Decision properties screen
Now your workflow looks like the example shown in Figure 10.

Figure 10
Workflow ready for user decision
After this user decision you have two paths: one for approval and one for rejection. If the first approver approves the document, his user name is added to the list. Otherwise, an email is sent to the initiator with the rejection decision.
To add the name of the decision maker of first level, you need to create an object in the workflow container called ActualAgent. You create another variable of the type swragent in the workflow container just as you created the object distribution list.

Figure 11
Create the object ActualAgent
To get the actual agent from the above user decision, double-click the user decision labeled Please approve the following in Figure 10. Go to the Control tab (Figure 12).

Figure 12
User decision properties pages
Click the Binding (Exists) button. In the binding window (Figure 13) you see workflow objects on the left side of the window, and the object you created, ActualAgent, is also visible. User decision task objects are also shown on the right side, and you can see the object Wi_Actual_Agent. This object holds the information about the agent who actually makes the decision, so you need it. Drag and drop both window objects into the lower table of the binding window. Both bind to get the actual agent value of the user decision task so you can then put it into your email recipients list.

Figure 13
Drag and drop both objects into the table to bind them
Click the green check mark icon. Right-click the approve branch of the user decision in the workflow and create a Container operation step. Name it Add_approver1 in the properties window. Figure 14 shows how to fill the fields. Your workflow now looks like the example shown in Figure 15.

Figure 14
Add first approver to the distribution list

Figure 15
Workflow layout
In the case of rejection you want the system to send an email to the filled distribution list, so you have to create an email step. Right-click the Reject line in the workflow and click the Create… option in the context menu. Then double-click the send email step. A new window appears for the subject and body of the email (Figure 16).

Figure 16
Enter the subject and body of an email
Select the recipient type Expression. In next field choose the expression &DISTRLIST& by using the F4 help and select the distList object. Enter the Subject as Document is rejected and copy it into the body. Click the green check mark icon and a new window appears. Enter sendmail as the abbreviation and description of the email step. After you click the green check mark icon, the step is inserted in the workflow (Figure 17).

Figure 17
Current workflow layout
For the second level of approval you do not have to repeat all the above steps. Instead, you can copy the user decision and paste it on the approval branch next to the Container operation step. Right-click the first user decision as shown in Figure 18.

Figure 18
Copy a whole block
Then you need to paste it in the workflow (Figure 19). Click the approval branch next to the Add_approver container operation. Right-click and then select Paste block as shown in Figure 19.

Figure 19
Paste the copied block
You have to copy the user decision standard template TS00008267 by clicking the copy task icon so you can assign agents to each user decision task as shown Figure 20.

Figure 20
Copy the task number
Click the copy task icon
. In the new window that appears enter the abbreviation of the new task and a short description. You can name it decision1 as the abbreviation. Click the green check mark icon. Press Ctrl-S to save the step. Repeat these steps for the remaining user decision tasks.
For the third level of approval repeat the copy block steps described above. The layout now looks like the example shown in Figure 21.

Figure 21
Final workflow layout
Now I describe how to assign agents to each user decision task. Double-click the first user decision task. Click the Control tab. Click the agent assignment icon (the red button) as shown in Figure 20 and a new window appears (Figure 22). Click the assign icon
on the application toolbar to bring up the screen in Figure 23.

Figure 22
User assignment to task

Figure 23
Select the agent type
Select the agent type and click the green check mark icon. Then select the user to whom this task is sent for approval purposes (Figure 24).

Figure 24
Select the user
Repeat the procedure of user assignment and assign the respective level of approver.
Now you can test the workflow. Press F8 to run the workflow in the designer. You see that a workflow is sent to the first approver’s inbox (Figure 25).

Figure 25
Workflow for first approver
If the user rejects the workflow then an email is sent only to the workflow initiator and no one else receives the rejection email. If the first approver approves it, his name is added to the distribution list. If the second approver rejects the email, the notification is sent only to the initiator and the first approver. The same procedure applies if the second approver approves the workflow, and the third one rejects it. In that case only the initiator and first and second approvers receive the notifications.
Fahim Ahmad
Fahim Ahmad is a senior ABAP consultant for Descon Engineering Ltd. He has master’s degrees in statistics and computer science from the University of Punjab in Lahore, Pakistan. He has 10 years of experience as a software engineer. Fahim specializes in ABAP enhancements and reports, workflow, RFC programming, interfacing, OLE, and SAP Web services configuration and development.
You may contact the author at fahim119@gmail.com.
If you have comments about this article or publication, or would like to submit an article idea, please contact the editor.