Creating a Custom Business Object

Objective

After completing this lesson, you will be able to create a custom business object

The Simple Contract Business Object

The Simple Contract business object serves as an example that contains the most important structure elements. It helps demonstrate the different capabilities of SAP Cloud Applications Studio and the extension framework. It consists of:

  • Fields with different data types and pre-defined labels on the root node.
  • A sub node "Items" that contains more fields and has a 0:n relationship to the root node. Hence, we can have multiple "Item" instances per contract.
  • Dependent Objects that allow us to reuse existing objects as part of our BO.
  • Associations that represent references to other business objects which can be used for navigation and property access.
  • Actions to encapsulate business logic.
  • A message that can be shown to the user (this will be shown in the next unit).

Let’s have a look at the various fields in more detail. Let’s begin with the root node’s fields, sometimes referred to as header fields.

  • The first two fields allow us to store an ID and a description for the contract. The attribute AlternativeKey for the ContractID field ensures that the ID is unique amongst all SimpleContract instances. The system creates a technical identifier called UUID transparently in the background and uses it internally for referencing the object. The alternative key field ContractID is human readable and hence, more user friendly. Similar to the IDs of accounts and employees, it is usually used in the front end to enable end users in creating references to other business objects.
  • The fields AccountID and ContractOwnerID can store IDs of an account and an employee. To access data of the respective objects, these IDs must be resolved to object references, that are then stored in the respective associations.
  • The date fields allow users to specify different dates for the contract processing.
  • The ContractAmount field is used to store the sum of the item’s amounts. It will be calculated by a script, implemented for the CalculateReportAmount action and executed whenever the object is saved.

We will create the definition of the business object and implement the logic in the following sections and lessons. To make all of this accessible for end users, we generate screens for the object and adapt them to our needs.

Create a Custom Business Object

In the next step we create a new custom business object. It's a common approach to place it in a separate folder, to keep track of the development content and keep the solution clean. We show that in the following video. The source code for the business object definition will be copy and pasted during the video. You can find the script snippet below.

Note

The folder structure helps to keep the solution tidy. In the upcoming demos of this unit, you will see how content is added as we go along and why it makes sense to have a separate folder for each business object. 

If you have access to an SAP Sales/Service Cloud tenant and want to follow along with the demonstrations, you can use the following code snippet for the business object definition.

Code Snippet
123456789101112131415161718192021222324252627282930313233343536373839404142
import AP.Common.GDT; import AP.CRM.Global; import AP.FO.BusinessPartner.Global; import AP.PC.IdentityManagement.Global; import AP.FO.ProductDataMaintenance.Global; businessobject SimpleContract raises CalculationResultMsg { message CalculationResultMsg text "The item total is: &1" : Amount; [AlternativeKey] [Label("Contract ID")] element ContractID : ID; [Label("Contract Description")] element ContractDescription : LANGUAGEINDEPENDENT_MEDIUM_Description; [Label("Account")] element AccountID : BusinessPartnerInternalID; [Label("Contract Owner ID")] element ContractOwnerID : EmployeeID; [Label("Agreement Date")] element AgreementDate : Date; [Label("Effective Date")] element EffectiveDate : Date; [Label("Expiration Date")] element ExpirationDate : Date; [Label("Contract Amount")] element ContractAmount : Amount; node Items [0,n] { [Label("Item ID")] element ItemID : ID; [Label("Product ID")] element ProductID : ProductInternalID; [Label("Quantity")] element Quantity : Quantity; [Label("Price")] element Price : Amount; association ToProduct to Material; } [DependentObject(AttachmentFolder)] node Attachment; [DependentObject(TextCollection)] node TextCollection; association ToAccount to Customer; association ToContractOwner to Employee; action CalculateReportAmount; }

Don't forget to Save and Activate the code!