Wednesday, 17 January 2024

How to add a new field to the RAP BO of a customizing table

Introduction


In this blog post, you will learn how to adapt a RAP business object of a customizing table when you add a new non-key field to the table. Removing a field works very similar.

This blog is relevant for:

  • SAP S/4HANA On-Premises 2022 or higher
  • SAP S/4HANA Cloud, Public Edition
  • SAP S/4HANA Cloud, Private Edition
  • SAP BTP, ABAP Environment

Example RAP BO


You have created a RAP BO based on the following customizing table by using the BC Maintenance Object ADT Wizard as described in this tutorial.

Note that this ADT Wizard does not support overwriting existing objects.

@EndUserText.label : 'Error Code'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #C
@AbapCatalog.dataMaintenance : #ALLOWED
define table zerrcode {
  key client            : abap.clnt not null;
  key error_code        : z_error_code not null;
  last_changed_at       : abp_lastchange_tstmpl;
  local_last_changed_at : abp_locinst_lastchange_tstmpl;
}

Add new field to Data Model & Behavior


You have added the new field reference_id to the table definition:

@EndUserText.label : 'Error Code'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #C
@AbapCatalog.dataMaintenance : #ALLOWED
define table zerrcode {
  key client            : abap.clnt not null;
  key error_code        : z_error_code not null;
  reference_id          : abap.numc(3);
  last_changed_at       : abp_lastchange_tstmpl;
  local_last_changed_at : abp_locinst_lastchange_tstmpl;
}

Add the field to the CDS entity definition of the table and, if available, to the projection entity definition:

@EndUserText.label: 'Error Code'
@AccessControl.authorizationCheck: #CHECK
define view entity ZI_ErrorCode
  as select from ZERRCODE
  association to parent ZI_ErrorCode_S as _ErrorCodeAll on $projection.SingletonID = _ErrorCodeAll.SingletonID
  composition [0..*] of ZI_ErrorCodeText as _ErrorCodeText
{
  key ERROR_CODE as ErrorCode,
  reference_id as ReferenceID,
  @Semantics.systemDateTime.lastChangedAt: true
  LAST_CHANGED_AT as LastChangedAt,
  @Semantics.systemDateTime.localInstanceLastChangedAt: true
  LOCAL_LAST_CHANGED_AT as LocalLastChangedAt,
  1 as SingletonID,
  _ErrorCodeAll,
  _ErrorCodeText
}

@EndUserText.label: 'Maintain Error Code'
@AccessControl.authorizationCheck: #CHECK
@Metadata.allowExtensions: true
define view entity ZC_ErrorCode
  as projection on ZI_ErrorCode
{
  key ErrorCode,
  ReferenceID,
  LastChangedAt,
  @Consumption.hidden: true
  LocalLastChangedAt,
  @Consumption.hidden: true
  SingletonID,
  _ErrorCodeAll : redirected to parent ZC_ErrorCode_S,
  _ErrorCodeText : redirected to composition child ZC_ErrorCodeText,
  _ErrorCodeText.Description : localized
}

If the RAP BO is draft-enabled, add the field to the draft table.:

@EndUserText.label : 'ZI_ErrorCode - Draft'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zerrcode_d {
  key client         : mandt not null;
  key errorcode      : z_error_code not null;
  referenceid        : abap.numc(3);
  lastchangedat      : abp_lastchange_tstmpl;
  locallastchangedat : abp_locinst_lastchange_tstmpl;
  singletonid        : abap.int1;
  "%admin"           : include sych_bdl_draft_admin_inc;
}

Add the field to the mapping definition in the behavior definition:

define behavior for ZI_ErrorCode alias ErrorCode
persistent table ZERRCODE
draft table ZERRCODE_D
etag master LocalLastChangedAt
lock dependent by _ErrorCodeAll
authorization dependent by _ErrorCodeAll

{
  [...]
  
  mapping for ZERRCODE
  {
    ErrorCode = ERROR_CODE;
    ReferenceID = reference_id;
    LastChangedAt = LAST_CHANGED_AT;
    LocalLastChangedAt = LOCAL_LAST_CHANGED_AT;
  }

}

(Optional) Add the field to the Metadata Extension. If the field does not have a data element, also define a field label.

annotate view ZC_ErrorCode with
{
  [...]
  ErrorCode;
  
  @UI.identification: [ {
    position: 2,
    label: 'Reference ID'
  } ]
  @UI.lineItem: [ {
    position: 2,
    label: 'Reference ID'
  } ]
  ReferenceID;
  
}

The new field is now exposed in the OData service. If you are using the Custom Business Configurations app, the field is displayed on refresh:

How to add a new field to the RAP BO of a customizing table
New field in CUBCO app

No comments:

Post a Comment