CDS views are the core technology to build a modern extensibility solutions, whether it’s in analytics, ABAP RAP, or OData APIs. One of the challenge CDS views has that it does not support all the comparison operators like standard ABAP syntax. Below is the list of comparison expressions available and it’s missing special operators such as CO(Contain only), NA(Does not contain any), etc.

ABAP CDS – cond_expr, Relational Operators – ABAP Keyword Documentation

In this blog, we will go through steps to implement logic CO(Contain only) and NA(Does not contain any) in CDS view with the help of table function, AMDB and regular expressions.

In the exercise, we will check if accounting document item text contains numeric or characters. To test the checks, first we will create CDS view table function.

Create CDS table function

@EndUserText.label: 'Numeric check for document item text'
@ClientHandling.type: #CLIENT_DEPENDENT
@ClientHandling.algorithm: #SESSION_VARIABLE
@AccessControl.authorizationCheck: #NOT_REQUIRED
define table function ZNum_Chk_TF
  with parameters
    @Environment.systemField: #CLIENT
    p_clnt :abap.clnt
returns
{
  Client             : abap.clnt;
  SourceLedger       : fins_ledger;
  CompanyCode        : bukrs;
  FiscalYear         : gjahr;
  AccountingDocument : belnr_d;
  LedgerGLLineItem   : docln6;
  DocumentItemText   : sgtxt;
  IsNumber           : abap.char( 6 );
}
implemented by method
  ZCL_Num_Chk=>CheckNumber;

Implement check logic

Create the class ZCL_Num_Chk with method CheckNumber that implements AMDB logic.

CLASS zcl_num_chk DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_amdp_marker_hdb.

    CLASS-METHODS:
      checknumber  FOR TABLE FUNCTION znum_chk_tf.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS zcl_num_chk IMPLEMENTATION.

  METHOD checknumber
   BY DATABASE FUNCTION
    FOR HDB
    LANGUAGE SQLSCRIPT
    OPTIONS READ-ONLY
    USING I_GLAccountLineItem.

    RETURN select _GLLineItem.mandt    as client,
                        _GLLineItem.SourceLedger,
                        _GLLineItem.CompanyCode,
                        _GLLineItem.FiscalYear,
                        _GLLineItem.AccountingDocument,
                        _GLLineItem.LedgerGLLineItem,
                        _GLLineItem.DocumentItemText,
                        'NUMBER' as IsNumber
                    from I_GLAccountLineItem as _GLLineItem
*                   Only allow numerics(CO in ABAP)
                   where not _GLLineItem.DocumentItemText LIKE_REGEXPR '[^\d]'
                   and _GLLineItem.DocumentItemText <> '';

  ENDMETHOD.
ENDCLASS.

Check if the value is numeric only

Breaking down the logic here. [^\d] means anything but number = the result will get NOT digits = ONLY non-numeric character(regex101: build, test, and debug regex can be used to check the regular expression). Now the key is that there is ‘not’ right after the WHERE clause, making this expression “NOT anything that is only non-numeric character” = “Only Numeric”. The second WHERE clause is rejecting any item that is blank(can also be rejected with regular expression).

where not _GLLineItem.DocumentItemText LIKE_REGEXPR '[^\d]'
                   and _GLLineItem.DocumentItemText <> '';

With this logic, the data preview of CDS table function will return accounting items that has only numeric values.

Check if the value contains numeric

Change the WHERE clause above to below.

where _GLLineItem.DocumentItemText LIKE_REGEXPR '[\d]'
                   and _GLLineItem.DocumentItemText <> ''

With the same principle, regular expression is used here as well. [\d] is a straightforward expression that means numeric value and LIKE_REGEXPR will fetch all the items that contains numeric. The second WHERE clause is rejecting any item that is blank(can also be rejected with regular expression).

Check if the value is character only

Change the WHERE clause to below.

where not _GLLineItem.DocumentItemText LIKE_REGEXPR '[\d]'
and _GLLineItem.DocumentItemText <> ''

Check if the value contains character

Change the WHERE clause to below.

where _GLLineItem.DocumentItemText LIKE_REGEXPR '[\D]'
and _GLLineItem.DocumentItemText <> ''

Leave a Reply

Your email address will not be published. Required fields are marked *