abapcds

Call BRF+ rule from ABAP CDS view?


First of all, I'm developing in ECC with ABAP 7.50 and Oracle DB. I have a BRF+ rule that for a given postal code returns me a specific contractor ID.

There are only 2 possible contractor IDs, but the postal codes for each one are a set of rules based on ranges and unique values.

Simplified rule example:

IF postalCode >= 1000 and postalCode <= 2000 THEN Contractor1
ELSE postalCode = 2001 THEN Contractor2
ELSE postalCode = 2002 THEN Contractor1
ELSE Contractor2

Now I have an ABAP CDS that returns a Work Order information and I need it to also return the vendor ID in the header. Can I somehow call the BRF+ with the work order postal code and get the vendor ID using only CDS?

I would like to do that because the CDS is directly exposed as an ODATA service. Otherwise I can use OpenSQL to query the DB and then fill the remaining field.

Edit: I marked Haojie answer as correct, even though it's only possible from 7.51 version. For lower versions -to my knowledge- there is no solution other than adding the logic after reading from the CDS view (in Gateway or not creating the ODATA service directly from the view, but use ABAP to add the missing information.


Solution

  • You can use ABAP CDS Virtual Element.

    You need to build another CDS view on top of the Work Order CDS and create a new artificial Field like VendorID and annotate it.

     define view my_cds_view 
        as select from WorkOrder
    {
        ...
         @ObjectModel.readOnly: true
         @ObjectModel.virtualElement
         @ObjectModel.virtualElementCalculatedBy: 'cl_brf_plus_vendor_id'
        cast('' as abap.lifnr ) as VendorID       
        ...
     } 
    

    Create a Class cl_brf_plus_vendor_id to implement if_sadl_exit_calc_element_read

    CLASS cl_brf_plus_vendor_id DEFINITION
        PUBLIC
        FINAL
        CREATE PUBLIC .
    
        PUBLIC SECTION.
        INTERFACES:
        if_sadl_exit_calc_element_read.
    
        PROTECTED SECTION.
        PRIVATE SECTION.
    ENDCLASS.
    
    
    CLASS cl_brf_plus_vendor_id IMPLEMENTATION.
    
       METHOD if_sadl_exit_calc_element_read~get_calculation_info.
    
       ENDMETHOD.
    
    
       METHOD if_sadl_exit_calc_element_read~calculate.
    
           CHECK NOT it_original_data IS INITIAL.
    
           DATA lt_calculated_data TYPE STANDARD TABLE OF my_cds_view WITH DEFAULT KEY.
    
           MOVE-CORRESPONDING it_original_data TO lt_calculated_data.
    
           LOOP AT lt_calculated_data ASSIGNING FIELD-SYMBOL(<ls_calculated_data>).
             **"Get the postal code and call BRF+ to 
               "have the value of artificial field VendorID.** 
    
           ENDLOOP.
    
           MOVE-CORRESPONDING lt_calculated_data TO ct_calculated_data.
    
        ENDMETHOD.
    
      ENDCLASS.
    

    Add my_cds_view as data source to your Gateway project.