Validations in RAP using Unmanaged scenario.

  • Validations are used to check the consistency and correctness of the data before it is saved to the database.
  •  If the data is invalid, appropriate error messages are returned to the user.

Managed Scenario

  • In Managed Scenario, the final check for all involved BOs is done via validations.
  • In Managed Scenario VALIDATIONS are called during CHECK_BEFORE_SAVE method.
Unmanaged Scenario
  • In unmanaged Scenario, the final check for all involved BOs is done in CHECK_BEFORE_SAVE method.
  • FAILED
  • REPORTED
  • CHECK_BEFORE_SAVE method gets called during the SAVE sequence.
  • If the method returns an error in the failed parameter, the save sequence is terminated and the CLEANUP_FINALIZE method is called.
                            Refer:- Etag in RAP Unmanaged

Step 1: Change the API class and change the student internal table to public.

CLASS zcl_ab_stuh_13 DEFINITION

PUBLIC

FINAL

CREATE PUBLIC .


PUBLIC SECTION.

     data: gt_student TYPE STANDARD TABLE OF ztl_ab_stuh_13.

TYPES:

tt_create_student TYPE TABLE FOR CREATE ziv_ab_stuh_13,

tt_map_early TYPE RESPONSE FOR MAPPED EARLY ziv_ab_stuh_13,

tt_failed_early TYPE RESPONSE FOR FAILED EARLY ziv_ab_stuh_13,

tt_report_early TYPE RESPONSE FOR REPORTED EARLY ziv_ab_stuh_13,

tt_response_late TYPE RESPONSE FOR REPORTED LATE ziv_ab_stuh_13,


tt_keys_student TYPE TABLE FOR READ IMPORT ziv_ab_stuh_13, "\\student,

tt_result_student TYPE TABLE FOR READ RESULT ziv_ab_stuh_13, "\\student,

tt_update_student TYPE TABLE FOR UPDATE ziv_ab_stuh_13, "\\student,


tt_delete_student TYPE TABLE FOR DELETE ziv_ab_stuh_13,

tt_cba_results TYPE TABLE FOR CREATE ziv_ab_stuh_13\\student\_result.

CLASS-METHODS: get_instance

RETURNING VALUE(r_instance)

TYPE REF TO zcl_ab_stuh_13.

methods:

create_student

IMPORTING

entities TYPE tt_create_student

CHANGING

mapped TYPE tt_map_early

failed TYPE tt_failed_early

reported TYPE tt_report_early,

get_next_id

RETURNING VALUE(rv_id) TYPE ziv_ab_stuh_13-StuId,

earlynumbering_create

IMPORTING entities TYPE tt_create_student

CHANGING mapped TYPE tt_map_early

failed TYPE tt_failed_early

reported TYPE tt_report_early

RAISING

cx_uuid_error,

save_data

CHANGING

reported TYPE tt_response_late,

read_student

IMPORTING

keys TYPE tt_keys_student

CHANGING

result TYPE tt_result_student

failed TYPE tt_failed_early

reported TYPE tt_report_early,

update

IMPORTING

entities TYPE tt_update_student

CHANGING

mapped TYPE tt_map_early

failed TYPE tt_failed_early

reported TYPE tt_report_early,

delete

IMPORTING

keys TYPE tt_delete_student

CHANGING

mapped TYPE tt_map_early

failed TYPE tt_failed_early

reported TYPE tt_report_early,

earlynumbering_cba_result

IMPORTING

entities TYPE tt_cba_results

CHANGING

mapped TYPE tt_map_early

failed TYPE tt_failed_early

reported TYPE tt_report_early,

cba_result_create

IMPORTING

entities_cba TYPE tt_cba_results

CHANGING

mapped TYPE tt_map_early

failed TYPE tt_failed_early

reported TYPE tt_report_early.

PROTECTED SECTION.

PRIVATE SECTION.

CLASS-DATA: mo_instance TYPE REF TO zcl_ab_stuh_13,

"gt_student TYPE STANDARD TABLE OF ztl_ab_stuh_13, " WITH EMPTY KEY,

gs_student TYPE ztl_ab_stuh_13,

gs_mmaped TYPE tt_map_early,

gr_student_d TYPE RANGE OF ztl_ab_stuh_13-id,

gt_results TYPE STANDARD TABLE OF ztl_ab_stui_13,

lv_date type timestampl.

ENDCLASS.


CLASS zcl_ab_stuh_13 IMPLEMENTATION.

METHOD get_instance.

mo_instance = r_instance = COND #( WHEN mo_instance IS BOUND

THEN mo_instance

ELSE NEW #( ) ).

ENDMETHOD.


METHOD get_next_id.

SELECT MAX( stu_id ) FROM ztl_ab_stuh_13 INTO @DATA(lv_id1).

rv_id = lv_id1 + 1.

ENDMETHOD.


METHOD earlynumbering_create.

DATA(ls_mapped) = gs_mmaped.

DATA(lv_id) = cl_uuid_factory=>create_system_uuid( )->create_uuid_x16( ).

DATA(lv_stuid) = get_next_id( ).

READ TABLE gt_student ASSIGNING FIELD-SYMBOL(<gs_student>) INDEX 1.

IF <gs_student> IS ASSIGNED.

<gs_student>-id = lv_stuid.

UNASSIGN <gs_student>.

ENDIF.


mapped-student = VALUE #(

FOR ls_entities IN entities WHERE ( id IS INITIAL )

(

%cid = ls_entities-%cid

%is_draft = ls_entities-%is_draft

Id = lv_id ) ) .

ENDMETHOD.


METHOD create_student.

LOOP AT entities ASSIGNING FIELD-SYMBOL(<ls_entities>).

gt_student = CORRESPONDING #( entities MAPPING FROM ENTITY ).

IF gt_student IS NOT INITIAL.

gt_student[ 1 ]-stu_id = get_next_id( ).

get TIME STAMP FIELD lv_date.

gt_student[ 1 ]-last_ch_date = lv_date.

"gt_student[ 1 ]-last_ch_time = lv_date.


mapped-student = VALUE #( (

%cid = <ls_entities>-%cid

%key = <ls_entities>-%key

%is_draft = <ls_entities>-%is_draft

) ) .

ENDIF.

ENDLOOP.

ENDMETHOD.


METHOD save_data.

IF NOT gt_student[] IS INITIAL.

MODIFY ztl_ab_stuh_13 FROM TABLE @gt_student.

ENDIF.


IF NOT gr_student_d IS INITIAL.

DELETE FROM ztl_ab_stuh_13 WHERE id IN @gr_student_d.

ENDIF.


IF NOT gt_results IS INITIAL.

MODIFY ztl_ab_stui_13 FROM TABLE @gt_results.

ENDIF.

ENDMETHOD.


METHOD read_student.

SELECT * FROM ztl_ab_stuh_13

FOR ALL ENTRIES IN @keys

WHERE id = @keys-Id

INTO TABLE @DATA(lt_read_student).


result = CORRESPONDING #( lt_read_student MAPPING TO ENTITY ).

ENDMETHOD.


METHOD update.

DATA: lt_stu_update TYPE STANDARD TABLE OF ztl_ab_stuh_13,

lt_stu_update_x TYPE STANDARD TABLE OF zstr_up_stuh.


lt_stu_update = CORRESPONDING #( entities MAPPING FROM ENTITY ).

lt_stu_update_x = CORRESPONDING #( entities MAPPING FROM ENTITY USING CONTROL ).


IF NOT lt_stu_update IS INITIAL.

SELECT * FROM ztl_ab_stuh_13

FOR ALL ENTRIES IN @lt_stu_update

WHERE id = @lt_stu_update-Id

INTO TABLE @DATA(lt_stu_old).

ENDIF.

get TIME STAMP FIELD lv_date.

gt_student = VALUE #(

"FOR idx=1 idx <= lines( lt_stu_update )

LET

ls_control_flag = VALUE #( lt_stu_update_x[ 1 ] OPTIONAL )

ls_student_new = VALUE #( lt_stu_update[ 1 ] OPTIONAL )

ls_student_old = VALUE #( lt_stu_old[ id = ls_student_new-id ] OPTIONAL )

IN

(

id = ls_student_new-id

stu_id = COND #( WHEN ls_control_flag-stu_id IS NOT INITIAL

THEN ls_student_new-stu_id

ELSE ls_student_old-stu_id )

stu_name = COND #( WHEN ls_control_flag-stu_name IS NOT INITIAL

THEN ls_student_new-stu_name

ELSE ls_student_old-stu_name )

stu_age = COND #( WHEN ls_control_flag-stu_age IS NOT INITIAL

THEN ls_student_new-stu_age

ELSE ls_student_old-stu_age )

stu_course = COND #( WHEN ls_control_flag-stu_course IS NOT INITIAL

THEN ls_student_new-stu_course

ELSE ls_student_old-stu_course )

stu_cour_dur = COND #( WHEN ls_control_flag-stu_cour_dur IS NOT INITIAL

THEN ls_student_new-stu_cour_dur

ELSE ls_student_old-stu_cour_dur )

stu_status = COND #( WHEN ls_control_flag-stu_status IS NOT INITIAL

THEN ls_student_new-stu_status

ELSE ls_student_old-stu_status )

stu_gender = COND #( WHEN ls_control_flag-stu_gender IS NOT INITIAL

THEN ls_student_new-stu_gender

ELSE ls_student_old-stu_gender )

stu_gen_desc = COND #( WHEN ls_control_flag-stu_gen_desc IS NOT INITIAL

THEN ls_student_new-stu_gen_desc

ELSE ls_student_old-stu_gen_desc )


stu_dob = COND #( WHEN ls_control_flag-stu_dob IS NOT INITIAL

THEN ls_student_new-stu_dob

ELSE ls_student_old-stu_dob )


last_ch_time = lv_date

last_ch_date = ls_student_old-last_ch_date"lv_date

) ).


ENDMETHOD.


METHOD delete.

DATA: lt_stu_delete TYPE TABLE OF ztl_ab_stuh_13.


lt_stu_delete = CORRESPONDING #( keys MAPPING FROM ENTITY ).


gr_student_d = VALUE #(

FOR ls_student IN lt_stu_delete

sign = 'I'

option = 'EQ'

( low = ls_student-id )


).


ENDMETHOD.


METHOD earlynumbering_cba_result.

LOOP AT entities ASSIGNING FIELD-SYMBOL(<ls_entities>).

LOOP AT <ls_entities>-%target ASSIGNING FIELD-SYMBOL(<ls_results>).

mapped-result = VALUE #( (

%cid = <ls_results>-%cid

%is_draft = <ls_results>-%is_draft

%key = <ls_results>-%key ) ) .

ENDLOOP.

ENDLOOP..

ENDMETHOD.


METHOD cba_result_create.

gt_results = VALUE #(

FOR ls_entities IN entities_cba

FOR ls_result IN ls_entities-%target

LET

ls_rap_results = CORRESPONDING ztl_ab_stui_13( ls_result MAPPING FROM ENTITY )

IN

(

ls_rap_results

)

).


mapped = VALUE #(

result = VALUE #(

FOR x = 1 WHILE x <= lines( entities_cba )

LET

lt_results = VALUE #( entities_cba[ x ]-%target OPTIONAL )

IN

FOR y = 1 WHILE y <= lines( lt_results )

LET

ls_curr_res = VALUE #( lt_results[ y ] OPTIONAL )

IN

(

%cid = ls_curr_res-%cid

%key = ls_curr_res-%key

Id = ls_curr_res-Id

)

) ).

ENDMETHOD.


ENDCLASS.

Step 2: Change the behavior implementation class and add logic in check_before_save method.

CLASS lhc_Student DEFINITION INHERITING FROM cl_abap_behavior_handler.

PRIVATE SECTION.


METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION

IMPORTING keys REQUEST requested_authorizations FOR Student RESULT result.


METHODS get_global_authorizations FOR GLOBAL AUTHORIZATION

IMPORTING REQUEST requested_authorizations FOR Student RESULT result.


METHODS create FOR MODIFY

IMPORTING entities FOR CREATE Student.


METHODS earlynumbering_create FOR NUMBERING

IMPORTING entities FOR CREATE Student.


METHODS update FOR MODIFY

IMPORTING entities FOR UPDATE Student.


METHODS delete FOR MODIFY

IMPORTING keys FOR DELETE Student.


METHODS read FOR READ

IMPORTING keys FOR READ Student RESULT result.


METHODS lock FOR LOCK

IMPORTING keys FOR LOCK Student.


METHODS rba_Result FOR READ

IMPORTING keys_rba FOR READ Student\_Result FULL result_requested RESULT result LINK association_links.


METHODS cba_Result FOR MODIFY

IMPORTING entities_cba FOR CREATE Student\_Result.


METHODS earlynumbering_cba_Result FOR NUMBERING

IMPORTING entities FOR CREATE Student\_Result.


ENDCLASS.


CLASS lhc_Student IMPLEMENTATION.


METHOD get_instance_authorizations.

ENDMETHOD.


METHOD get_global_authorizations.

ENDMETHOD.


METHOD create.

zcl_ab_stuh_13=>get_instance( )->create_student(

EXPORTING

entities = entities

CHANGING

failed = failed

mapped = mapped

reported = reported ).

ENDMETHOD.


METHOD earlynumbering_create.

TRY.

zcl_ab_stuh_13=>get_instance( )->earlynumbering_create(

EXPORTING

entities = entities

CHANGING

failed = failed

mapped = mapped

reported = reported ).

CATCH cx_uuid_error.

"handle exception

ENDTRY.


ENDMETHOD.


METHOD update.

zcl_ab_stuh_13=>get_instance( )->update(

EXPORTING

entities = entities

CHANGING

mapped = mapped

failed = failed

reported = reported

).

ENDMETHOD.


METHOD delete.

zcl_ab_stuh_13=>get_instance( )->delete(

EXPORTING

keys = keys

CHANGING

mapped = mapped

failed = failed

reported = reported

).

ENDMETHOD.


METHOD read.

zcl_ab_stuh_13=>get_instance( )->read_student(

EXPORTING

keys = keys

CHANGING

result = result

failed = failed

reported = reported

).

ENDMETHOD.


METHOD lock.

TRY.

DATA(lv_lock) = cl_abap_lock_object_factory=>get_instance( iv_name = 'EZSTUDENT_13' ).

CATCH cx_abap_lock_failure INTO DATA(exception).

RAISE SHORTDUMP exception.

ENDTRY.

LOOP AT keys ASSIGNING FIELD-SYMBOL(<ls_keys>).

TRY.

lv_lock->enqueue(

it_parameter = VALUE #( ( name = 'ID' value = REF #( <ls_keys>-Id ) ) )


).

CATCH cx_abap_foreign_lock INTO DATA(foreign_lock).

APPEND VALUE #(

id = keys[ 1 ]-Id

%msg = new_message_with_text(

severity = if_abap_behv_message=>severity-error

text = 'Record is Locked By' && foreign_lock->user_name

)

) TO reported-student.

APPEND VALUE #(

id = keys[ 1 ]-Id

) TO failed-student.

CATCH cx_abap_lock_failure INTO exception.

RAISE SHORTDUMP exception.

ENDTRY.

ENDLOOP.

ENDMETHOD.


METHOD rba_Result.

ENDMETHOD.


METHOD cba_Result.

zcl_ab_stuh_13=>get_instance( )->cba_result_create(

EXPORTING

entities_cba = entities_cba

CHANGING

mapped = mapped

failed = failed

reported = reported

).


ENDMETHOD.


METHOD earlynumbering_cba_Result.

zcl_ab_stuh_13=>get_instance( )->earlynumbering_cba_result(

EXPORTING

entities = entities

CHANGING

mapped = mapped

failed = failed

reported = reported

).

ENDMETHOD.



ENDCLASS.


CLASS lhc_Result DEFINITION INHERITING FROM cl_abap_behavior_handler.

PRIVATE SECTION.


METHODS update FOR MODIFY

IMPORTING entities FOR UPDATE Result.


METHODS delete FOR MODIFY

IMPORTING keys FOR DELETE Result.


METHODS read FOR READ

IMPORTING keys FOR READ Result RESULT result.


METHODS rba_Student FOR READ

IMPORTING keys_rba FOR READ Result\_Student FULL result_requested RESULT result LINK association_links.


ENDCLASS.


CLASS lhc_Result IMPLEMENTATION.


METHOD update.

ENDMETHOD.


METHOD delete.

ENDMETHOD.


METHOD read.

ENDMETHOD.


METHOD rba_Student.

ENDMETHOD.


ENDCLASS.


CLASS lsc_ZIV_AB_STUH_13 DEFINITION INHERITING FROM cl_abap_behavior_saver.

PROTECTED SECTION.


METHODS finalize REDEFINITION.


METHODS check_before_save REDEFINITION.


METHODS save REDEFINITION.


METHODS cleanup REDEFINITION.


METHODS cleanup_finalize REDEFINITION.


ENDCLASS.


CLASS lsc_ZIV_AB_STUH_13 IMPLEMENTATION.


METHOD finalize.

ENDMETHOD.


METHOD check_before_save.

DATA: lt_student TYPE STANDARD TABLE OF ztl_ab_stuh_13.

lt_student = zcl_ab_stuh_13=>get_instance( )->gt_student.

IF lt_student IS NOT INITIAL.

READ TABLE lt_student ASSIGNING FIELD-SYMBOL(<ls_student>) INDEX 1.

IF <ls_student> IS ASSIGNED.


IF <ls_student>-stu_age < 18.

APPEND VALUE #( id = <ls_student>-id

%msg = new_message_with_text(

severity = if_abap_behv_message=>severity-error

text = 'Age is less than 18. Creation is not allowed'

) ) TO reported-student.

APPEND VALUE #( id = <ls_student>-id ) TO failed-student.

ENDIF.


IF <ls_student>-stu_status EQ abap_false.

APPEND VALUE #( id = <ls_student>-id

%msg = new_message_with_text(

severity = if_abap_behv_message=>severity-error

text = 'Please select status'

) ) TO reported-student.

APPEND VALUE #( id = <ls_student>-id ) TO failed-student.

ENDIF.


ENDIF.

ENDIF.


ENDMETHOD.


METHOD save.

zcl_ab_stuh_13=>get_instance( )->save_data(

CHANGING

reported = reported ).

ENDMETHOD.


METHOD cleanup.

ENDMETHOD.


METHOD cleanup_finalize.

ENDMETHOD.


ENDCLASS.


Testing: 
            * Preview the application.    
            * Click on create button.
            * Give values and click on create.
            * Now I given age as 10 and not selecting status.

    
    * Changed the age (25) and selected status- No error- Created.
    


******************************Thank you*******************************

Comments

Popular posts from this blog

New syntax for append- VALUE (new syntax 7.4+) in ABAP

Read statement new syntax in ABAP. (7.4+).

Concatenation new syntax( 7.4+ ).