Business Objects in SAP ABAP RAP

In SAP ABAP RESTful Application Programming (RAP), a Business Object (BO) represents a key entity in your application (like Sales Order, Customer, or Product) and encapsulates its data structurebusiness logic, and behavior. Business Objects form the foundation for developing Fiori apps and OData services in modern SAP systems.

Key Characteristics of Business Objects

✔ Self-contained (data + behavior in one place)
✔ Reusable across applications
✔ Transactional (support CRUD operations)
✔ Exposed as OData services for Fiori UIs

1. Components of a Business Object in RAP

A Business Object consists of three core layers:

A. Data Model (CDS Views)

  • Defines the structure of the business object using Core Data Services (CDS)
  • Maps to database tables

Example:

@AbapCatalog.sqlViewName: 'ZSO_HEADER'
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales Order Header'
define view ZI_SalesOrder as select from vbak {
  key vbeln as SalesOrder,
  erdat as OrderDate,
  kunnr as Customer
}

B. Behavior Definition

  • Specifies what operations are allowed (CRUD)
  • Defines validations, determinations, and actions

Example:

define behavior for ZI_SalesOrder alias SalesOrder {
  // Standard operations
  create;
  update;
  delete;
  
  // Field control
  field (readonly) SalesOrder;
  
  // Business logic
  validation validateCustomer on save { field Customer; }
  action approve;
}

C. Behavior Implementation

  • Contains the actual ABAP logic for operations

Example

CLASS lhc_salesorder DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.
    METHODS validateCustomer FOR VALIDATE ON SAVE
      IMPORTING keys FOR SalesOrder~validateCustomer.
    METHODS approve FOR MODIFY
      IMPORTING keys FOR ACTION SalesOrder~approve.
ENDCLASS.

CLASS lhc_salesorder IMPLEMENTATION.
  METHOD validateCustomer.
    " Check if customer exists
  ENDMETHOD.
  
  METHOD approve.
    " Approve sales order logic
  ENDMETHOD.
ENDCLASS.

2. Types of Business Objects in RAP

A. Managed Business Objects

  • SAP automatically handles persistence (database operations)
  • Simple to implement

Example

define behavior for ZI_SalesOrder managed;

B. Unmanaged Business Objects

  • Developer implements custom persistence logic
  • Required for complex scenarios

Example

define behavior for ZI_SalesOrder
implementation in class zcl_salesorder_behavior unique;

C. Draft-Enabled Business Objects

  • Supports intermediate saving of incomplete data
  • Essential for transactional apps

Example

define behavior for ZI_SalesOrder draft;

3. Key Features of Business Objects

A. Field Control

  • Manage field visibility and editability:
field (mandatory) Customer;
field (readonly) SalesOrder;

B. Validations

  • Ensure data integrity:
validation validateAmount on save { field Amount; }

C. Determinations

  • Automatic calculations:
determination calculateTax on modify { field Amount; }

D. Actions

  • Custom operations:
action approve result [1] $self;

E. Associations

  • Define relationships between BOs:
association [0..*] to ZI_SalesOrderItem as _Items;

4. Best Practices for Business Objects

  1. Start with Managed before considering Unmanaged
  2. Use meaningful names (ZI_SalesOrder, not ZT001)
  3. Leverage draft for transactional apps
  4. Reuse common behaviors via projections
  5. Test early with Fiori Elements Preview

Business Object Implementation Types in SAP ABAP RAP

In SAP ABAP RESTful Application Programming (RAP), Business Objects can be implemented in different ways depending on complexity and requirements. The three main implementation types are:

  1. Managed Implementation (Simplest)
  2. Unmanaged Implementation (Custom)
  3. Mixed Implementation (Hybrid)

Understanding these types is crucial for designing efficient RAP applications.

1. Managed Implementation

Overview

  • SAP automatically handles all database operations (CRUD)
  • Minimal coding required
  • Best for standard scenarios without complex logic

Key Characteristics

✔ Automatic persistence handling
✔ Built-in draft handling
✔ Standard validations/determinations available
✔ Quick to implement

Example –

define behavior for ZI_SalesOrder
implementation in class zcl_bp_salesorder unique
{
  // Managed mode (default)
  managed;
  
  // Standard operations
  create;
  update;
  delete;
}

2. Unmanaged Implementation

Overview

  • Developer fully controls persistence logic
  • Required for custom database operations
  • More flexible but complex

Key Characteristics

✔ Complete control over persistence
✔ Must implement all CRUD operations
✔ Required for legacy system integration
✔ Suitable for complex business logic

Syntax Example

define behavior for ZI_CustomOrder
implementation in class zcl_custom_order_behavior unique
{
  // Explicit unmanaged declaration
  unmanaged;
  
  // Still define available operations
  create;
  update;
  delete;
}

Implementation Class Structure

CLASS zcl_custom_order_behavior DEFINITION PUBLIC
  INHERITING FROM cl_abap_behavior_handler.
  
  PUBLIC SECTION.
    METHODS:
      create FOR MODIFY
        IMPORTING entities FOR CREATE CustomOrder,
        
      update FOR MODIFY
        IMPORTING entities FOR UPDATE CustomOrder,
        
      delete FOR MODIFY
        IMPORTING keys FOR DELETE CustomOrder,
        
      read FOR READ
        IMPORTING keys FOR READ CustomOrder RESULT result.
ENDCLASS.

3. Mixed (Hybrid) Implementation

Overview

  • Combines managed and unmanaged approaches
  • SAP handles some aspects, developer handles others
  • Provides balance between control and convenience

Key Characteristics

✔ Partial automatic persistence
✔ Custom logic for specific operations
✔ Managed features still available
✔ Complex implementation

define behavior for ZI_HybridOrder
implementation in class zcl_hybrid_order_behavior unique
{
  // Base is managed
  managed;
  
  // But override specific operations
  delete unmanaged;
  action customAction unmanaged;
}

Syntax Example

define behavior for ZI_HybridOrder
implementation in class zcl_hybrid_order_behavior unique
{
  // Base is managed
  managed;
  
  // But override specific operations
  delete unmanaged;
  action customAction unmanaged;
}

Implementation Example

CLASS zcl_hybrid_order_behavior DEFINITION.
  PUBLIC SECTION.
    METHODS:
      delete FOR MODIFY
        IMPORTING keys FOR DELETE HybridOrder,
        
      customAction FOR MODIFY
        IMPORTING keys FOR ACTION HybridOrder~customAction.
ENDCLASS.

When to Use

  • Mostly standard behavior with few custom operations
  • When only specific operations need special handling
  • Gradual migration from unmanaged to managed

Conclusion

Business Objects are the building blocks of RAP development, combining databehavior, and services into reusable components. By mastering BOs, you can efficiently develop enterprise-grade SAP applications.

Next Steps:

  • Explore side effects in behavior implementations
  • Learn about authorization checks in BOs
  • Experiment with custom actions and functions

Leave a Comment