Skip to Content
Technical Articles
Author's profile photo Anmol Bhatia

Create Workflow Attachment Enhancement for Multiple Business Objects in SAP C4C

The design of sales and service processes is always a key challenge. To make the business process more robust and efficient, we all need to automate certain tasks. Here, I will be highlighting a key feature of SAP C4C for sending out automated e-mails using workflow with dynamically selected attachments and adding them in e-mail notification for multiple business objects.

Scenario: Trigger E-mail Notification on the creation of a new contact or lead with attachments

Workflow Implementation : Create a Workflow Rule for the Business Object required:

Step 1: Go to Administrator –> Workflow Rules

Step 2: In the first step, select business object as “Contact”

Step 3: Define Conditions as per you requirement.

Note: I am defining a random condition. You can define it as per your requirements.

Define%20Conditions

Define Conditions

 

Step 4: In this Step, select Rule Type as E-mail & define other mandatory conditions

Select%20Rule%20Type%20%3A%20E-Mail

Select Rule Type : E-Mail

 

Step 5: After filling up other details such as Recipient Determination, Placeholders, Sender Name, Sender E-mail, Subject, Template File.

To receive notification when you attach an attachment to an email, check the box Add Attachment.  

Attachment%20CheckBox%20Set%20to%20YES

Attachment Checkbox Set to YES

Note: As soon as you check the check box to true, there is no change in the UI of the workflow.

No where you can specify a static URL or attachment implementation name. Checking this check box will execute the SDK enhancement : ExitForGettingWorkflowAttachment 

                                                             Activate Rule and Finish
Follow the Same STEP 1 to 6 for implementing workflow rule for Lead Object.
Note: Please check checkbox for attachment in step 5.

 

SDK Enhancement Implementation:

To implement the enhancement, you must have a development solution or patch solution in status In-Development. Details of the enhancement we will be implementing can be found under Repository Explorer. PDI%20BADI%20for%20Attachement

 

Step 1: Right Click on solution -> Add -> New Item

Provide%20Enhancement%20Name

Step 2: As soon as you click on the Add button, a popup window opens requesting details of the enhancement to be implemented:

Note: For all enhancements, the details of the namespace and deployment unit can be found in the Repository Explorer (as shown in the attached screenshot). 

Select%20Namespace%20and%20Enhacement%20Name                                                        Select Namespace and Enhancement Option

 

No business object is selected, as the above enhancement is specifically defined for workflows and is not specific to any business object.

 

The enhancement is created as soon as you click OK.

Enhancement

On Left:

The namespace, option description, and type show the enhancement details. Below you can also define messages that can be raised during enhancement execution.

 

On Right: 2 sub-items:

In .fltr file is created where we will be defining filter for the Business object.

In .absl file we will be writing the logic to fetch the attachment.

 

Step 3: Open .fltr file and select the business object

Note: This enhancement has a TYPE of “Multiple Use”. That means the enhancement can be implemented for multiple business objects.

In this scenario, I will be implementing a single enhancement for 2 different business objects.

Step 4: Open .absl file for implementing logic. 

Logic Explanation:

The Input Structure in BADI has 2 field:

1. TriggerBOUUID => This provides the UUID of the instance of a business object for which workflow is being triggered.

2. TriggerBOESRName => This provides Namespace name of the business object for which workflow is being triggered.

In the logic, I have compared the Name to identify for which business object the workflow is triggered.

(Name and Namespace can be easily found for any Business Object from repository explorer: For example: Contact in C4C refers to BusinessPartner as Technical Object. Therefore in Repository Explorer you can check the details of BusinessPartner Name and Namespace.)

 

Name%20and%20Namespace%20example

After identifying the BusinessObject for which workflow was triggered, I have executed a query on the Library Object and fetch a pdf document to be sent as attachment. (Note: This is just sample code, you can write your own logic).

/*
	Add your SAP Business ByDesign scripting language implementation for:
		Enhancement Option: ExitForGettingWorkflowAttachment
		Operation: GET_EXT_ATTACHMENT

		Script file signature
		----------------------------
		Parameter: InputData of type InputStructureWorkflowsGetPdiAttachment
		Returns: OutputStructureforWorkflowGetAttachment

	Note: 
	  - To use code completion, press CTRL+J.
*/
import AP.Workflow.Global;
import AP.Common.GDT;
import DocumentServices.Global as DS;
import AP.CRM.Global;
import ABSL;

var result : OutputStructureforWorkflowGetAttachment;
var lv_attachmentname : OutputStructureforWorkflowGetAttachment.AttachmentName;
var lv_attachmentContent : OutputStructureforWorkflowGetAttachment.AttachmentContent;
 
if (!InputData.TriggerBOUUID.IsInitial()) 
{
	if (InputData.TriggerBOESRName.Name == "BusinessPartner")
	{
		var query = DocumentFacade.QueryByElements;
		var resultData;
		// 2. Selection
		var selectionParams = query.CreateSelectionParams();
		selectionParams.Add(query.Name, "I", "EQ", "Contact.docx");
		// Result
		resultData = query.Execute(selectionParams);
		if (resultData.Count() > 0)
		{
			foreach (var iterator in resultData)
			{
				lv_attachmentname = iterator.Name;
				if (iterator.ToDocument.IsSet())
				{
					var ToDocument = iterator.ToDocument;
					if (ToDocument.CurrentVersion.IsSet())
					{
						var CurrentVersion = ToDocument.CurrentVersion.VersionFileVariant;
						if (CurrentVersion.Count() > 0)
						{
							foreach (var iterator2 in CurrentVersion)
							{
								if (!iterator2.FileContentURI.IsInitial())
								{
									if (iterator2.VersionFileVariantContent.IsSet()) 
									{
										var VersionFileVariantContent = iterator2.VersionFileVariantContent;
										lv_attachmentContent.content = VersionFileVariantContent.BinaryObject.content;
										result.AttachmentName.Add(lv_attachmentname);
										result.AttachmentContent.Add(lv_attachmentContent);
									}
								}
							}
						}
					}
				}
			}
		}
		
	}
	else if (InputData.TriggerBOESRName.Name == "Lead")
	{
		
		var query = DocumentFacade.QueryByElements;
		var resultData;
		// 2. Selection
		var selectionParams = query.CreateSelectionParams();
		selectionParams.Add(query.Name, "I", "EQ", "Lead.xlsx");
		// Result
		resultData = query.Execute(selectionParams);
		if (resultData.Count() > 0)
		{
			foreach (var iterator in resultData)
			{
				lv_attachmentname = iterator.Name;
				if (iterator.ToDocument.IsSet())
				{
					var ToDocument = iterator.ToDocument;
					if (ToDocument.CurrentVersion.IsSet())
					{
						var CurrentVersion = ToDocument.CurrentVersion.VersionFileVariant;
						if (CurrentVersion.Count() > 0)
						{
							foreach (var iterator2 in CurrentVersion)
							{
								if (!iterator2.FileContentURI.IsInitial())
								{
									if (iterator2.VersionFileVariantContent.IsSet()) 
									{
										var VersionFileVariantContent = iterator2.VersionFileVariantContent;
										lv_attachmentContent.content = VersionFileVariantContent.BinaryObject.content;
										result.AttachmentName.Add(lv_attachmentname);
										result.AttachmentContent.Add(lv_attachmentContent);
									}
								}
							}
						}
					}
				}
			}
		}
	}

}

return result;

 

With this, all the configurations and enhancements are in completed.

 

Testing the enhancement

 

After Creating a new contact:

After Creating a new lead:

 

Some Important Points:

Here I have implemented a single BADI (ExitForGettingWorkflowAttachment ) running for multiple business objects.

You can also implement the same BADI (ExitForGettingWorkflowAttachment ) again for a different business object and write your own logic. 

If multiple implementations are done for the same business object, all will be executed for the workflow rule scheduled for that particular instance of a business object.

For example, if 2 workflow rules are implemented for a single business object (suppose : ticket), Both workflow rules are executed after meeting the necessary conditions and having the Add Attachment check box checked. Both implementations will be executed and fetch the attachments and add them to the e-mail.

This is kind of a flaw in the implementation as we cannot control which implementation to trigger, particularly for a workflow rule.

I request all readers to please share your feedback or thoughts in comments.

 

Follow the below pages for more information:

SAP Cloud Applications Studio environment Topic page

To post and answer questions on SAP Cloud Application Studio

To read other blog posts on SAP Cloud Application Studio

 

Thanks

Best Regards.

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Amit maurya
      Amit maurya

      Nice Anmol.
      Thank you for sharing this workflow enhancement which can be used in variety of applications.

      Q. Do we have any size limitations for attachment when sending mail to designated user ?

      Author's profile photo Anmol Bhatia
      Anmol Bhatia
      Blog Post Author

      Hello Amit,

      Thanks for asking!

      Basically, the file content that is parsed for email notification is fetched from C4C tenants itself. This means that the standard limit that SAP provides can be considered the limit of the file size that can be parsed. (which is 100 MB)

      You can read more on this below in the SAP KBA:

      2562017 - Size Limit For Attachments In C4C (sap.com)

       

      Thanks

      Anmol