In this beginner blog post we are going to see how we can use Prechecks in ABAP Restful Application Programming Model.

 

Ref:  https://help.sap.com/docs/BTP/923180ddb98240829d935862025004d6/5a24e181eb994fffb59a186a2fdb3ef2.html?q=Authorization

 

 

What is Prechecks

Prechecks is used to validate data before it can reach to Transactional Buffer.
You can prevent invalid changes from reaching the transactional buffer by prechecking modify operations.

 

Prechecks are suggested to use when we need to validate value of one control with another control on UI. More important is when controls are getting values by a complex backend logic / joins etc.

 

Instead of validating values at Save operation when Invalid values are already reached to Transactional Buffer is not a good Idea.

 

If we decide to validate these complex data at Save and Application is using Draft functionality, in this case the Invalid data is already been saved to Draft tables of Entity. It may become overhead to clean up this data from Draft tables.

 

During PRECHECK implementation, we can implement validation logic or if the validation logic is complex, Function method or FM can be triggered from Prechecks and based on result FAILED and REPORTED parameters can be updated and later can be used to display Error on Fiori frontend.

How to Implement Prechecks

 

Prechecks can be defined in Behavior Definition and implemented it in the behavior implementation class.

Below shows example how to define Prechecks for Update and Create Methods.

create ( precheck );

update ( precheck );

 

Refer below screen shot to define Precheck on Update method. We are using Update method because we want to validate entry before Save and when user navigate (Tab) from the target validation field.

 

 

Once precheck is defined with Update method, we need to Implement Update precheck method. To Implement please refer below screen shot.

 

 

Once Update precheck method is Implements, new method will be available in Behavior Implementation Class file as shown in below screen shot.

 

 

Once the Update precheck method is available use Activate all button to activate Behavior definition and Behavior Implementation class. once Activation is complete update method with below code.

 

For better understanding I have tried to put comments before every line of code.

 

METHOD precheck_update.
  
  "Loop on Entity to get current values from UI, all updated changed values will be available in <lfs_entity>
    LOOP AT entities ASSIGNING FIELD-SYMBOL(<lfs_entity>).
   
   "Check whic values are changed by user
   "01 = value is updated / changed , 00 = value is not changed

    "Check if Course or CourseDuration values is changed by User
      CHECK  <lfs_entity>-%control-Course EQ '01' OR <lfs_entity>-%control-Courseduration EQ '01'.

      "Read Entity record and collect on Internal table  
      READ ENTITIES OF zi_student_5000 IN LOCAL MODE
         ENTITY Student
         FIELDS ( Course Courseduration ) WITH VALUE #(  (  %key = <lfs_entity>-%key ) )
         RESULT DATA(lt_studentsCourse).

      IF sy-subrc IS INITIAL.
        READ TABLE lt_studentsCourse ASSIGNING FIELD-SYMBOL(<lfs_db_course>) INDEX 1.
        IF sy-subrc IS INITIAL.
        
        "Collect the updated value from Frontend. If user has updated value on Frontend then get updated value 
        "Else get the value from Database
          <lfs_db_course>-Course = COND #(  WHEN <lfs_entity>-%control-Course EQ '01' THEN
                                          <lfs_entity>-Course ELSE <lfs_db_course>-Course
          ).

          <lfs_db_course>-Courseduration = COND #(  WHEN <lfs_entity>-%control-Courseduration EQ '01' THEN
                                          <lfs_entity>-Courseduration ELSE <lfs_db_course>-Courseduration
          ).
         
         "Business logic per requirement, We are validating Course Duration for Computers can not be less then 5 Yerars   
          IF <lfs_db_course>-Courseduration < 5.
            IF <lfs_db_course>-Course = 'Computers'.
            
              "Return Error Message to Frontend.
              APPEND VALUE #(  %tky =  <lfs_entity>-%tky ) TO failed-student.
              APPEND VALUE #(  %tky =  <lfs_entity>-%tky
                               %msg = new_message_with_text(
                                  severity = if_abap_behv_message=>severity-error
                                  text = 'Invalid Course duration...'
                                )  ) TO reported-student.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDMETHOD.

 

Activate Behavior Implementation class. Once Activated run Application.

 

 

Computers Course has Duration of 5 Years as shown in below Screen shot. Any other value less then 5 Years should be Invalid for Course – Computers

 

 

User tries to update Course duration for Course – Computers as 3 Years which results in Invalid course duration Error which is shown to user on frontend.

 

Advantage

  1. If application is a draft enabled application, then these inconsistent values have already been stored in draft tables and transactional buffers.
  2. No need wait for validation to be triggered at time of Save which is too late for some complex validation.

 

Conclusion

If requirement is to validate two dependent fields which are populated by complex backend logic like Joins of multiple tables and application is having Draft capability using Prechecks for validation is suggested.

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x