Friday 5 July 2024

How to Create a Custom Validation from Fiori App Manage Substitution and Validation Rules

As we are moving to Fiori first yet cloud ready approach for any development, why restrict us to GGB0 or GGB1 for creating validation or substitution rules?

We can utilize the Fiori application - Manage Substitution and Validation Rules to create the rules which will be applied to all the standard processes such as standard interfaces, GUI T-codes and managing standard processes in custom program.

In this blog, we will create a custom-defined function in the validation rule on asset creation or modification done through the Fiori app - ‘Manage Fixed Assets’. The rule will validate against the supplier assigned to the asset master record.

Note that the same validation will work through any means of creating or modifying asset master data as mentioned earlier.

Steps to create a custom validation rule


1. Open the Fiori app – “Manage Substitution and Validation Rules” and click on ‘Create Rule’.

How to Create a Custom Validation from Fiori App Manage Substitution and Validation Rules

2. Enter Business Context, Event, select Rule Type and click on Create.

How to Create a Custom Validation from Fiori App Manage Substitution and Validation Rules

3. Enter the Rule Name and Description. Decide what should happen post validation in control level. You could give a warning or an error, along with a message defined in the message class assigned here.

How to Create a Custom Validation from Fiori App Manage Substitution and Validation Rules

4. If there are preconditions, you can assign them. But in this blog, we will create only a validation by clicking on F4 help.

How to Create a Custom Validation from Fiori App Manage Substitution and Validation Rules

5. Upon clicking the help, we get all the CDS entities list with respect to the business context chosen when we created the rule in step 2. But we also get a list of functions which we can utilize to create our validation rule. Based on our requirement, we can also extend the CDS views from the list through the Fiori app – “Custom Fields and Logic” and we can also create a custom function rather than relying on the four functions we see in the list above.

How to Create a Custom Validation from Fiori App Manage Substitution and Validation Rules

6. In this demonstration, we will create a customer-defined function and use it for our validation. For that, we will create a class by implementing the interface - IF_FIN_RE_CUSTOM_FUNCTION.
When we implement this interface, it allows us to integrate custom ABAP logic into the Manage Substitution/Validation Rules app. Once the implementing ABAP class is activated, the implementation is made available as a function inside the Manage Substitution/Validation Rules app in the functions list.

How to Create a Custom Validation from Fiori App Manage Substitution and Validation Rules

To implement the required methods, you can take help from the SAP provided reference class - CL_SUBVAL_WEEKDAY. The class documents the functionality of each method with logic.

Below is the brief explanation of each method -

IS_DISABLED: Remove the method for visibility in all events or use IV_EVENT_ID for specific event visibility in Manage Substitution and Validation Rules app.
GET_NAME: Returns the name of the customer-defined function
GET_RETURNTYPE: Define the ABAP type for the return value of IF_FIN_RE_CUSTOM_FUNCTION~EXECUTE method. Update the return type based on your needs.
GET_RETURN_VALUEHELP: Returning a CDS view for value help and a field for the function's return value.
GET_PARAMETERS: Returns a list of function parameters with unique names, specifying technical ABAP types.
GET_DESCRIPTION: Return a message ID/No. for the text description of customer-defined function that is to be displayed in the functions list.
CHECK_AT_RULE_ACTIVATION: Implement this method to enforce checks on parameters passed to a substitution/validation rule, ensuring only specified types or values are accepted for activation.
EXECUTE: Method to actually implement the logic of the function at hand.

Below is the code for the class defined -

CLASS zcl_test_asset_val DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .
  PUBLIC SECTION.
    INTERFACES if_fin_re_custom_function .
  PROTECTED SECTION.
  PRIVATE SECTION.
    TYPES:    gty_country TYPE c LENGTH 2.

ENDCLASS.

CLASS zcl_test_asset_val IMPLEMENTATION.

  METHOD if_fin_re_custom_function~execute.

*   Data declaration
    DATA lv_supplier TYPE c LENGTH 10.

*   Get supplier from parameters
    lv_supplier = is_runtime-parameters[ 1 ]-value->*.

*   Get supplier's country
    SELECT SINGLE FROM i_supplier WITH PRIVILEGED ACCESS
      FIELDS Country WHERE Supplier = _supplier INTO (lv_country).

*   Pass space if country is DE, else 'X'
    IF lv_country EQ 'DE'.
      ev_result = REF #( abap_false ).
    ELSE.
      ev_result = REF #( abap_true ).
    ENDIF.
  ENDMETHOD.

  METHOD if_fin_re_custom_function~get_description.

    rs_msg = VALUE symsg( msgid = 'ZTEST' msgno = '000' ).

  ENDMETHOD.

  METHOD if_fin_re_custom_function~get_name.

    rv_name = 'ZCHECK_ASSET'.

  ENDMETHOD.

  METHOD if_fin_re_custom_function~get_parameters.

    rt_parameters = VALUE #( ( name = 'SUPPLIER' ) ).

  ENDMETHOD.

  METHOD if_fin_re_custom_function~get_returntype.

    ro_type = CAST cl_abap_elemdescr( cl_abap_typedescr=>describe_by_name( 'ZCL_TEST_ASSET_VAL=>GTY_COUNTRY' ) ).

  ENDMETHOD.
ENDCLASS.

7. Once the implementing class is activated, we can see the custom function appearing in the list.

How to Create a Custom Validation from Fiori App Manage Substitution and Validation Rules

8. Once the function is selected, it will pop-up the importing parameters that we have specified in the implementing class’s method GET_PARAMETERS. We can assign the value of parameter as a constant, a field from available CDS views, or another function.

How to Create a Custom Validation from Fiori App Manage Substitution and Validation Rules

9. We will select supplier from the CDS view - C_ASSETTP and click on save.

10. In our logic we would validate the supplier country. If it is ‘DE’, we pass nothing and the validation is passed, else we pass ABAP_TRUE and its failed.

How to Create a Custom Validation from Fiori App Manage Substitution and Validation Rules

Now, save the rule and activate it with customizing TR. That’s it. Now, we can go to the Fiori app – “Manage Fixed Assets” and test the validation. Upon saving asset, we get below error if country of supplier is not DE.

How to Create a Custom Validation from Fiori App Manage Substitution and Validation Rules

Notes & Lessons:

  1. The facility to create rules from Fiori app is available from SAP S/4HANA 2020.
  2. The facility to create a customer-defined function is available from SAP S/4HANA 2022.
  3. Never keep the implementing class inactive which implements the interface IF_FIN_RE_CUSTOM_FUNCTION. Always remember to activate the class without any errors or incorrect datatype assignments, otherwise it would break the Fiori app - “Manage Substitution and Validation Rules”.
  4. Never create more than one implementing class implementing the interface IF_FIN_RE_CUSTOM_FUNCTION having same customer-defined function. If class A and class B, both are returning the same function name then Fiori app - “Manage Substitution and Validation Rules” would break.
  5. In the method GET_PARAMETERS of the interface IF_FIN_RE_CUSTOM_FUNCTION, make sure to follow below rules –
    1. Do not keep space in the name of parameter.
    2. Keep the order of parameters same when assigning the CDS view’s field to the customer-defined function and when passing it back in this method.
  6. Message class used at control level during rule creation must not be assigned to the local package. It will not reflect in the value help while creating the rule if assigned to the local package.
  7. Try keeping the implementing class in ABAP Language Version - 'ABAP for Cloud Development' to keep the core clean and cloud ready.
  8. The validation or substitution created from Fiori also works through standard interfaces and GUI transactions.

No comments:

Post a Comment