Setting default values when creating a new entity is a common requirement in RAP-based application development. Currently, there are three options available: Default Values Function, Default Factory Action, and Augmentation. In this blog post, I will walk through the implementation of each technique and explain when to use them.
Quick Comparison of Defaulting Options in RAP
Here is a summary of the three options for setting default values in RAP entity creation.
Default Values Function
Default Factory Action
Augmentation
Can set default values for the root entity?
✔
✔
✔
Can accept parameters?
✖
✔
✖
Can create child entities along with the root entity?
✖
✔
✔
Can set defaults when creating a child entity independently?
✔
✖
✔
When to Use Each Approach
Default Values Function: Best when you want to set static default values. “Static” here still allows values depending on runtime context, such as the logged-in user, the current date, values from related entities.
Default Factory Action: Use this when defaults need to be determined dynamically based on user input. It can also create child entities together with the root entity. However, it cannot be used for creating child entities independently; in that case, use a Default Values Function or Augmentation.
Augmentation: This is defined at the projection layer, not at the base behavior definition. It is mainly useful when you cannot or do not want to change the base behavior.
Notes on Combining the Methods
These methods cannot be combined for the same entity.
Default Values Function + Augmentation → Both are called, but only the values from the Default Values Function are applied.
Default Values Function + Default Factory Action → Only the action is executed, but no default values are set.
Default Factory Action + Augmentation → Only the action is executed, but no default values are set.
👉In practice, always choose one method per entity.
⚠️The above results are based on my own testing, not on official documentation. There may be inaccuracies.
Sample Code
In all of the following examples, the root entity is Employee and the child entity is Address.
Each method demonstrates how to set default values for Employee as well as for Address (when applicable).
1. Default Values Function
Behavior Definition
Define default values functions.
Note: GetDefaultsForCreate is the predefined function name and can be omitted.
// Default values for the root entity
create {default function GetDefaultsForCreate; }
// Default values for the child entity (via create-by-association)
association _Address {create {default function GetDefaultsForCBA;} with draft;}
Behavior Projection
Expose default values functions.
use function GetDefaultsForCreate;
use function GetDefaultsForCBA;
Behavior Implementation
METHOD GetDefaultsForCreate.
result = value #( for key in keys (
%cid = key-%cid
%param-LastName = ‘Default Last Name’
) ).
ENDMETHOD.
METHOD GetDefaultsForCBA.
result = value #( for key in keys (
%tky = key-%tky
%param-Purpose = ‘Default Purpose’
) ).
ENDMETHOD.
2. Default Factory Action
Behavior Definition
Define a default factory action. In this example, an input parameter ZABS_EMPLOYEE_INPUT is used. If no input is required, the parameter can be omitted.
static default factory action MyDefaultActionForCreate parameter ZABS_EMPLOYEE_INPUT [1];
ZABS_EMPLOYEE_INPUT is defined as a CDS abstract entity.
@EndUserText.label: ‘Input for employee default action’
define abstract entity ZABS_EMPLOYEE_INPUT
{
@EndUserText.label: ‘Employee Type’
EmployeeType: abap.int1;
}
Behavior Projection
Expose the default factory action.
use action MyDefaultActionForCreate;
Behavior Implementation
METHOD MyDefaultActionForCreate.
DATA employee_create TYPE TABLE FOR CREATE zr_employee_2508.
DATA employee_create_line TYPE STRUCTURE FOR CREATE zr_employee_2508.
DATA address_create TYPE TABLE FOR CREATE zr_employee_2508_Address.
DATA address_create_line TYPE STRUCTURE FOR CREATE zr_employee_2508_Address.
LOOP AT keys INTO DATA(key).
” Set default data for Employee
employee_create_line = VALUE #(
%cid = key-%cid
%is_draft = key-%param-%is_draft
FirstName = SWITCH #( key-%param-EmployeeType
WHEN 1 THEN ‘Default 1’
WHEN 2 THEN ‘Default 2’
WHEN 3 THEN ” )
%control = VALUE #( FirstName = if_abap_behv=>mk-on )
).
APPEND employee_create_line TO employee_create.
” Set default data for Address
address_create_line = VALUE #(
%cid_ref = key-%cid
%is_draft = key-%param-%is_draft
%target = VALUE #( ( %cid = sy-tabix
%is_draft = key-%param-%is_draft
Purpose = ‘Default’
%control = VALUE #( Purpose = if_abap_behv=>mk-on ) ) )
).
APPEND address_create_line TO address_create.
ENDLOOP.
“Create new instance of Employee
MODIFY ENTITIES OF zr_employee_2508 IN LOCAL MODE
ENTITY Employee
CREATE FROM employee_create
CREATE BY _Address
FROM address_create
MAPPED DATA(mapped_create).
mapped-employee = mapped_create-employee.
mapped-address = mapped_create-Address.
ENDMETHOD.
3. Augmentation
Behavior Projection
Augmentations are defined only in the Behavior Projection. The keyword “augment” is used to define them.
// Default values for the root entity
use create (augment);
// Default values for the child entity (via create-by-association)
use association _Address {create (augment); with draft;}
Behavior Implementation
METHOD augment_create.
DATA employee_create TYPE TABLE FOR CREATE zr_employee_2508.
DATA address_create TYPE TABLE FOR CREATE zr_employee_2508_Address.
DATA address_create_line TYPE STRUCTURE FOR CREATE zr_employee_2508_Address.
DATA relates_create TYPE abp_behv_relating_tab.
DATA tabix TYPE sy-tabix.
employee_create = CORRESPONDING #( entities ).
LOOP AT employee_create ASSIGNING FIELD-SYMBOL(<employee>).
” Count table index for generating unique %cid for Address
tabix = sy-tabix.
APPEND tabix TO relates_create.
” Set Default Data for Employee
<employee>-DateOfBirth = ‘19900101’.
<employee>-%control-DateOfBirth = if_abap_behv=>mk-on.
” Set Default Data for Address
address_create_line = VALUE #( %cid_ref = <employee>-%cid
%is_draft = <employee>-%is_draft
%key = <employee>-%key
%target = VALUE #( ( %cid = tabix
%is_draft = <employee>-%is_draft
Purpose = ‘Augmented Address’
%control = VALUE #( Purpose = if_abap_behv=>mk-on )
) )
).
APPEND address_create_line TO address_create.
ENDLOOP.
MODIFY AUGMENTING ENTITIES OF zr_employee_2508
ENTITY Employee
CREATE FROM employee_create
CREATE BY _Address
FROM address_create
RELATING TO entities BY relates_create.
ENDMETHOD.
METHOD augment_cba_Address.
DATA address_create TYPE TABLE FOR CREATE zr_employee_2508_Address.
DATA relates_update TYPE abp_behv_relating_tab.
address_create = CORRESPONDING #( DEEP entities ).
LOOP AT address_create ASSIGNING FIELD-SYMBOL(<address>).
LOOP AT <address>-%target ASSIGNING FIELD-SYMBOL(<target>).
<target>-Purpose = ‘Augmented Address CBA’.
<target>-%control = VALUE #( Purpose = if_abap_behv=>mk-on ).
ENDLOOP.
ENDLOOP.
MODIFY AUGMENTING ENTITIES OF zr_employee_2508
ENTITY Employee
CREATE BY _Address
FROM address_create.
ENDMETHOD.
Conclusion
When setting default values in RAP, you can choose between Default Values Function, Default Factory Action, and Augmentation.
Use Default Values Function for static defaults.
Use Default Factory Action for dynamic defaults or when creating child entities together with the root.
Use Augmentation only at the projection layer, when the base behavior cannot be changed.
Since these methods cannot be combined for the same entity, always pick the one that best matches your scenario.
References
Operation Defaulting (Default Values Function) | SAP Help PortalRAP – Default Factory Action | ABAP Keyword DocumentationEditing Text for Supplements (Augmentation) | SAP Help Portal
Setting default values when creating a new entity is a common requirement in RAP-based application development. Currently, there are three options available: Default Values Function, Default Factory Action, and Augmentation. In this blog post, I will walk through the implementation of each technique and explain when to use them. Quick Comparison of Defaulting Options in RAPHere is a summary of the three options for setting default values in RAP entity creation. Default Values FunctionDefault Factory ActionAugmentationCan set default values for the root entity?✔✔✔Can accept parameters?✖✔✖Can create child entities along with the root entity?✖✔✔Can set defaults when creating a child entity independently?✔✖✔ When to Use Each ApproachDefault Values Function: Best when you want to set static default values. “Static” here still allows values depending on runtime context, such as the logged-in user, the current date, values from related entities.Default Factory Action: Use this when defaults need to be determined dynamically based on user input. It can also create child entities together with the root entity. However, it cannot be used for creating child entities independently; in that case, use a Default Values Function or Augmentation.Augmentation: This is defined at the projection layer, not at the base behavior definition. It is mainly useful when you cannot or do not want to change the base behavior. Notes on Combining the MethodsThese methods cannot be combined for the same entity. Default Values Function + Augmentation → Both are called, but only the values from the Default Values Function are applied.Default Values Function + Default Factory Action → Only the action is executed, but no default values are set.Default Factory Action + Augmentation → Only the action is executed, but no default values are set.👉In practice, always choose one method per entity.⚠️The above results are based on my own testing, not on official documentation. There may be inaccuracies. Sample CodeIn all of the following examples, the root entity is Employee and the child entity is Address.Each method demonstrates how to set default values for Employee as well as for Address (when applicable).1. Default Values FunctionBehavior DefinitionDefine default values functions. Note: GetDefaultsForCreate is the predefined function name and can be omitted. // Default values for the root entity
create {default function GetDefaultsForCreate; }
// Default values for the child entity (via create-by-association)
association _Address {create {default function GetDefaultsForCBA;} with draft;}Behavior ProjectionExpose default values functions. use function GetDefaultsForCreate;
use function GetDefaultsForCBA;Behavior Implementation METHOD GetDefaultsForCreate.
result = value #( for key in keys (
%cid = key-%cid
%param-LastName = ‘Default Last Name’
) ).
ENDMETHOD.
METHOD GetDefaultsForCBA.
result = value #( for key in keys (
%tky = key-%tky
%param-Purpose = ‘Default Purpose’
) ).
ENDMETHOD. 2. Default Factory ActionBehavior DefinitionDefine a default factory action. In this example, an input parameter ZABS_EMPLOYEE_INPUT is used. If no input is required, the parameter can be omitted.static default factory action MyDefaultActionForCreate parameter ZABS_EMPLOYEE_INPUT [1];ZABS_EMPLOYEE_INPUT is defined as a CDS abstract entity.@EndUserText.label: ‘Input for employee default action’
define abstract entity ZABS_EMPLOYEE_INPUT
{
@EndUserText.label: ‘Employee Type’
EmployeeType: abap.int1;
}Behavior ProjectionExpose the default factory action. use action MyDefaultActionForCreate;Behavior Implementation METHOD MyDefaultActionForCreate.
DATA employee_create TYPE TABLE FOR CREATE zr_employee_2508.
DATA employee_create_line TYPE STRUCTURE FOR CREATE zr_employee_2508.
DATA address_create TYPE TABLE FOR CREATE zr_employee_2508_Address.
DATA address_create_line TYPE STRUCTURE FOR CREATE zr_employee_2508_Address.
LOOP AT keys INTO DATA(key).
” Set default data for Employee
employee_create_line = VALUE #(
%cid = key-%cid
%is_draft = key-%param-%is_draft
FirstName = SWITCH #( key-%param-EmployeeType
WHEN 1 THEN ‘Default 1’
WHEN 2 THEN ‘Default 2’
WHEN 3 THEN ” )
%control = VALUE #( FirstName = if_abap_behv=>mk-on )
).
APPEND employee_create_line TO employee_create.
” Set default data for Address
address_create_line = VALUE #(
%cid_ref = key-%cid
%is_draft = key-%param-%is_draft
%target = VALUE #( ( %cid = sy-tabix
%is_draft = key-%param-%is_draft
Purpose = ‘Default’
%control = VALUE #( Purpose = if_abap_behv=>mk-on ) ) )
).
APPEND address_create_line TO address_create.
ENDLOOP.
“Create new instance of Employee
MODIFY ENTITIES OF zr_employee_2508 IN LOCAL MODE
ENTITY Employee
CREATE FROM employee_create
CREATE BY _Address
FROM address_create
MAPPED DATA(mapped_create).
mapped-employee = mapped_create-employee.
mapped-address = mapped_create-Address.
ENDMETHOD. 3. AugmentationBehavior ProjectionAugmentations are defined only in the Behavior Projection. The keyword “augment” is used to define them. // Default values for the root entity
use create (augment);
// Default values for the child entity (via create-by-association)
use association _Address {create (augment); with draft;}Behavior Implementation METHOD augment_create.
DATA employee_create TYPE TABLE FOR CREATE zr_employee_2508.
DATA address_create TYPE TABLE FOR CREATE zr_employee_2508_Address.
DATA address_create_line TYPE STRUCTURE FOR CREATE zr_employee_2508_Address.
DATA relates_create TYPE abp_behv_relating_tab.
DATA tabix TYPE sy-tabix.
employee_create = CORRESPONDING #( entities ).
LOOP AT employee_create ASSIGNING FIELD-SYMBOL(<employee>).
” Count table index for generating unique %cid for Address
tabix = sy-tabix.
APPEND tabix TO relates_create.
” Set Default Data for Employee
<employee>-DateOfBirth = ‘19900101’.
<employee>-%control-DateOfBirth = if_abap_behv=>mk-on.
” Set Default Data for Address
address_create_line = VALUE #( %cid_ref = <employee>-%cid
%is_draft = <employee>-%is_draft
%key = <employee>-%key
%target = VALUE #( ( %cid = tabix
%is_draft = <employee>-%is_draft
Purpose = ‘Augmented Address’
%control = VALUE #( Purpose = if_abap_behv=>mk-on )
) )
).
APPEND address_create_line TO address_create.
ENDLOOP.
MODIFY AUGMENTING ENTITIES OF zr_employee_2508
ENTITY Employee
CREATE FROM employee_create
CREATE BY _Address
FROM address_create
RELATING TO entities BY relates_create.
ENDMETHOD.
METHOD augment_cba_Address.
DATA address_create TYPE TABLE FOR CREATE zr_employee_2508_Address.
DATA relates_update TYPE abp_behv_relating_tab.
address_create = CORRESPONDING #( DEEP entities ).
LOOP AT address_create ASSIGNING FIELD-SYMBOL(<address>).
LOOP AT <address>-%target ASSIGNING FIELD-SYMBOL(<target>).
<target>-Purpose = ‘Augmented Address CBA’.
<target>-%control = VALUE #( Purpose = if_abap_behv=>mk-on ).
ENDLOOP.
ENDLOOP.
MODIFY AUGMENTING ENTITIES OF zr_employee_2508
ENTITY Employee
CREATE BY _Address
FROM address_create.
ENDMETHOD. ConclusionWhen setting default values in RAP, you can choose between Default Values Function, Default Factory Action, and Augmentation.Use Default Values Function for static defaults.Use Default Factory Action for dynamic defaults or when creating child entities together with the root.Use Augmentation only at the projection layer, when the base behavior cannot be changed. Since these methods cannot be combined for the same entity, always pick the one that best matches your scenario.ReferencesOperation Defaulting (Default Values Function) | SAP Help PortalRAP – Default Factory Action | ABAP Keyword DocumentationEditing Text for Supplements (Augmentation) | SAP Help Portal Read More Technology Blog Posts by Members articles
#SAP
#SAPTechnologyblog