Create Dictionary Objects @ ABAP RESTful Application Programming Model (On-Premise)
September 12, 2022
ABAP | CDS | RAP | SAP
  1. Design / Prototype
  2. Create Dictionary Objects
  3. Core Data Services (CDS)
  4. Business Service
  5. Business Object Behavior
  6. Create a Fiori App
  7. Source (GitHub) for Data Model/Behaviour/Service
  8. Source (GitHub) for User Interface (Fiori Elements App)

As per the requirement, we will need four tables. One each for Header & Item data for contacts. Additionally, I created a master table for the contact categories and phone number tags, this is required to supply value help for UI.
You can create a dictionary table as per your requirement.

Note:
Tables for storing Drafts will be auto-generated using the QUICK FIX (Ctrl + 1) option in ADT while defining the Behaviour

Phone Book Header (Contacts)

@EndUserText.label : 'Phone Book Header'
@AbapCatalog.enhancement.category : #EXTENSIBLE_ANY
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #ALLOWED
define table zintlra_pb_d_hdr {
  key client    : abap.clnt not null;
  key pb_uuid   : sysuuid_x16 not null;
  pb_id         : zintlra_de_phonebook_id;
  pb_owner      : zintlra_de_createdby;
  pb_tag_code   : zintlra_de_phonebook_tag;
  pb_first_name : zintlra_de_first_name;
  pb_last_name  : zintlra_de_last_name;
  pb_created_at : zintlra_de_created_at;
  pb_changed_at : zintlra_de_changed_at;
  pb_email_id   : zintlra_de_email;
  pb_favourite  : zintlra_de_isfavourite;

}

Phone Book Item (Numbers)

@EndUserText.label : 'Phone Book Item'
@AbapCatalog.enhancementCategory : #EXTENSIBLE_ANY
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #ALLOWED
define table zintlra_pb_d_itm {
  key mandt        : mandt not null;
  key pb_item_uuid : sysuuid_x16 not null;
  pb_uuid          : sysuuid_x16;
  pb_id            : zintlra_de_phonebook_id;
  pb_item_id       : zintlra_de_phonebook_item;
  pb_category      : zintlra_de_category;
  pb_telephone     : ad_tlnmbr1;
  pb_created_at    : zintlra_de_created_at;
  pb_changed_at    : zintlra_de_changed_at;
  pb_default       : zintlra_de_isdefault;

}

Phone Book Contact Category

@EndUserText.label : 'Contact Category Master Table'
@AbapCatalog.enhancementCategory : #EXTENSIBLE_ANY
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #ALLOWED
define table zintlra_pb_d_cnt {
  key mandt        : mandt not null;
  key pb_category  : zintlra_de_category not null;
  @AbapCatalog.textLanguage
  key pb_spras     : spras not null;
  pb_category_text : zintlra_de_category_text;

}

Phone Book Tags

@EndUserText.label : 'PhoneBook Tags'
@AbapCatalog.enhancementCategory : #EXTENSIBLE_ANY
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #ALLOWED
define table zintlra_pb_d_tag {
  key mandt       : mandt not null;
  key pb_tag_code : zintlra_de_phonebook_tag not null;
  @AbapCatalog.textLanguage
  key pb_spras    : spras not null;
  pb_tag_text     : zintlra_de_phonebook_tagtext;

}

<< To Top

Create ABAP Class to Fill the Data

Global Class

class ZCL_INTLRA_PB_DATA_GEN definition
  public
  final
  create public .

public section.

  interfaces IF_OO_ADT_CLASSRUN .
protected section.
private section.
ENDCLASS.



CLASS ZCL_INTLRA_PB_DATA_GEN IMPLEMENTATION.


  METHOD if_oo_adt_classrun~main.
    IF out IS BOUND.
      out->write( 'Starting generation of Data' ).
    ENDIF.
    lcl_pb_data_gen=>create( out ).
    IF out IS BOUND.
      out->write( 'Finished generating Data' ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.

Local Types

*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations

CLASS lcl_pb_data_gen DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: create
      IMPORTING out TYPE REF TO if_oo_adt_classrun_out OPTIONAL.
  PROTECTED SECTION.
    CLASS-DATA: mt_head  TYPE STANDARD TABLE OF zintlra_pb_d_hdr,
                mt_items TYPE STANDARD TABLE OF zintlra_pb_d_itm,
                mt_cat   TYPE STANDARD TABLE OF zintlra_pb_d_cnt,
                mt_tag   TYPE STANDARD TABLE OF zintlra_pb_d_tag.

    CLASS-METHODS set_data.
ENDCLASS.

CLASS lcl_pb_data_gen IMPLEMENTATION.
  METHOD create.
    " Delete
    IF out IS BOUND.out->write('Deleting Old Records').ENDIF.
    DELETE FROM zintlra_pb_d_hdr.
    DELETE FROM zintlra_pb_d_itm.
    DELETE FROM zint_pb_d_hdrtmp.
    DELETE FROM zint_pb_d_itmtmp.
    DELETE FROM zintlra_pb_d_cnt.
    DELETE FROM zintlra_pb_d_tag.

    " Set Data
    IF out IS BOUND.out->write('Building New Records').ENDIF.
    set_data( ).

    " Insert
    IF out IS BOUND.out->write('Updating New Records').ENDIF.
    INSERT zintlra_pb_d_hdr FROM TABLE mt_head.
    INSERT zintlra_pb_d_itm FROM TABLE mt_items.
    INSERT zintlra_pb_d_cnt FROM TABLE mt_cat.
    INSERT zintlra_pb_d_tag FROM TABLE mt_tag.
  ENDMETHOD.

  METHOD set_data.

    DATA(lo_uuid) = cl_uuid_factory=>create_system_uuid( ).

    CLEAR: mt_head, mt_items, mt_cat.


    APPEND INITIAL LINE TO mt_head ASSIGNING FIELD-SYMBOL(<lfs_head>).
    <lfs_head>-pb_uuid       = lo_uuid->create_uuid_x16( ).
    <lfs_head>-pb_owner      = sy-uname.
    <lfs_head>-pb_tag_code   = 'FR'.
    <lfs_head>-pb_first_name = 'JOHN'.
    <lfs_head>-pb_last_name  = 'SMITH'.
    <lfs_head>-pb_email_id   = 'JOHN.SMITH@aditheos.com'.
    GET TIME STAMP FIELD <lfs_head>-pb_created_at.
    <lfs_head>-pb_changed_at = <lfs_head>-pb_created_at.

    " Get Numbers
    TRY.
        cl_numberrange_runtime=>number_get(
          EXPORTING
            nr_range_nr       = '01'
            object            = 'ZPBID'
          IMPORTING
            number            = DATA(lv_number)
        ).
        <lfs_head>-pb_id = lv_number.
      CATCH cx_number_ranges INTO DATA(lx_number_ranges).
    ENDTRY.

    APPEND INITIAL LINE TO mt_head ASSIGNING <lfs_head>.
    <lfs_head>-pb_uuid       = lo_uuid->create_uuid_x16( ).
    <lfs_head>-pb_owner      = sy-uname.
    <lfs_head>-pb_tag_code   = 'WO'.
    <lfs_head>-pb_first_name = 'SMITH'.
    <lfs_head>-pb_last_name  = 'JOHN'.
    <lfs_head>-pb_email_id   = 'SMITH.JOHN@aditheos.com'.
    GET TIME STAMP FIELD <lfs_head>-pb_created_at.
    <lfs_head>-pb_changed_at = <lfs_head>-pb_created_at.

    " Get Numbers
    TRY.
        cl_numberrange_runtime=>number_get(
          EXPORTING
            nr_range_nr       = '01'
            object            = 'ZPBID'
          IMPORTING
            number            = lv_number
        ).
        <lfs_head>-pb_id = lv_number.
      CATCH cx_number_ranges INTO lx_number_ranges.
    ENDTRY.

    UNASSIGN <lfs_head>.
    DATA(lo_telho) = cl_abap_random_int8=>create( min = 200000000
                                                  max = 299999990 ).
    DATA(lo_telmo) = cl_abap_random_int8=>create( min = 400000000
                                                  max = 499999990 ).
    LOOP AT mt_head ASSIGNING <lfs_head>.
      APPEND INITIAL LINE TO mt_items ASSIGNING FIELD-SYMBOL(<lfs_item>).
      <lfs_item>-pb_uuid       = <lfs_head>-pb_uuid.
      <lfs_item>-pb_id         = <lfs_head>-pb_id.
      <lfs_item>-pb_item_id    = '10'.
      <lfs_item>-pb_created_at = <lfs_head>-pb_created_at.
      <lfs_item>-pb_changed_at = <lfs_head>-pb_changed_at.
      <lfs_item>-pb_category   = 'HO'.
      <lfs_item>-pb_item_uuid  = lo_uuid->create_uuid_x16( ).
      <lfs_item>-pb_telephone  = lo_telho->get_next( ).
      CONDENSE <lfs_item>-pb_telephone.
      <lfs_item>-pb_telephone  = | +61 0{ <lfs_item>-pb_telephone }|.

      APPEND INITIAL LINE TO mt_items ASSIGNING <lfs_item>.
      <lfs_item>-pb_uuid       = <lfs_head>-pb_uuid.
      <lfs_item>-pb_id         = <lfs_head>-pb_id.
      <lfs_item>-pb_item_id    = '20'.
      <lfs_item>-pb_created_at = <lfs_head>-pb_created_at.
      <lfs_item>-pb_changed_at = <lfs_head>-pb_changed_at.
      <lfs_item>-pb_category   = 'MO'.
      <lfs_item>-pb_item_uuid  = lo_uuid->create_uuid_x16( ).
      <lfs_item>-pb_telephone  = lo_telmo->get_next( ).
      CONDENSE <lfs_item>-pb_telephone.
      <lfs_item>-pb_telephone  = | +61 0{ <lfs_item>-pb_telephone }|.
    ENDLOOP.

    APPEND INITIAL LINE TO mt_cat ASSIGNING FIELD-SYMBOL(<lfs_cat>).
    <lfs_cat>-pb_category = 'HO'.
    <lfs_cat>-pb_category_text = 'HOME'.
    <lfs_cat>-pb_spras = 'E'.

    APPEND INITIAL LINE TO mt_cat ASSIGNING <lfs_cat>.
    <lfs_cat>-pb_category = 'MO'.
    <lfs_cat>-pb_category_text = 'MOBILE'.
    <lfs_cat>-pb_spras = 'E'.

    APPEND INITIAL LINE TO mt_cat ASSIGNING <lfs_cat>.
    <lfs_cat>-pb_category = 'OF'.
    <lfs_cat>-pb_category_text = 'OFFICE'.
    <lfs_cat>-pb_spras = 'E'.

    APPEND INITIAL LINE TO mt_cat ASSIGNING <lfs_cat>.
    <lfs_cat>-pb_category = 'WO'.
    <lfs_cat>-pb_category_text = 'WORK'.
    <lfs_cat>-pb_spras = 'E'.

    APPEND INITIAL LINE TO mt_tag ASSIGNING FIELD-SYMBOL(<lfs_tag>).
    <lfs_tag>-pb_tag_code = 'FM'.
    <lfs_tag>-pb_tag_text = 'Family'.
    <lfs_tag>-pb_spras = 'E'.

    APPEND INITIAL LINE TO mt_tag ASSIGNING <lfs_tag>.
    <lfs_tag>-pb_tag_code = 'FR'.
    <lfs_tag>-pb_tag_text = 'Friend'.
    <lfs_tag>-pb_spras = 'E'.

    APPEND INITIAL LINE TO mt_tag ASSIGNING <lfs_tag>.
    <lfs_tag>-pb_tag_code = 'WO'.
    <lfs_tag>-pb_tag_text = 'Work'.
    <lfs_tag>-pb_spras = 'E'.


    APPEND INITIAL LINE TO mt_tag ASSIGNING <lfs_tag>.
    <lfs_tag>-pb_tag_code = 'OT'.
    <lfs_tag>-pb_tag_text = 'Others'.
    <lfs_tag>-pb_spras = 'E'.
  ENDMETHOD.

ENDCLASS.

Execute the Class to Generate Data

Data Preview (After Fill)

<< To Top