Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Field validation in table maintenance generator before saving

former_member685807
Discoverer
0 Likes
7,699

Hi guys,

im rather new to ABAP and want to implement the following:

Within the maintenance View in which one can enter data, I want to have a check if the sum() consisting to a specific Company code is => 1.00 (100 percent).

The table Looks as follows:


Company; CodeValue
01;0.3
01;0.4
01;0.3
02;0.2
02;0.2
02;0.2
02;0.2
02;0,199

The user should only be able to fill up values consisting to one Company code up to 1.00. For example, if one now tries to enter a row with Company Code 2; 0.3 -> it should not be savable and and error should Show that the value can only be up to one.

I was thinking about adding a before_save check to the maintenance view, something like this:


FORM form_03_CHECK_VALUES.

DATA: l_value TYPE value.
SELECT sum(value)
FROM table
INTO l_value
group by Company Code

IF l_value > 1.
MESSAGE 'ERROR' TYPE 'E' DISPLAY LIKE 'S'.
ENDIF.

ENDFORM.



But since I am rather new to ABAP, I guess I misunderstand some concept here. Can anyone help me here please?

Highly appreciated,

Hi guys,

im rather new to ABAP and want to implement the following:

Within the maintenance View in which one can enter data, I want to have a check if the sum() consisting to a specific Company code is => 1.00 (100 percent).

The table Looks as follows:


Company; CodeValue
01;0.3
01;0.4
01;0.3
02;0.2
02;0.2
02;0.2
02;0.2
02;0,199

The user should only be able to fill up values consisting to one Company code up to 1.00. For example, if one now tries to enter a row with Company Code 2; 0.3 -> it should not be savable and and error should Show that the value can only be up to one.

I was thinking about adding a before_save check to the maintenance view, something like this:


FORM form_03_CHECK_VALUES.

DATA: l_value TYPE value.
SELECT sum(value)
FROM table
INTO l_value
group by Company Code

IF l_value > 1.
MESSAGE 'ERROR' TYPE 'E' DISPLAY LIKE 'S'.
ENDIF.

ENDFORM.



But since I am rather new to ABAP, I guess I misunderstand some concept here. Can anyone help me here please?

Highly appreciated,

4 REPLIES 4
Read only

FredericGirod
Active Contributor
0 Likes
5,627

Your code looks good, You could play with message type Error to change the sy-subrc or directly impact the variable SPAN vim_abort_saving

(vim_abord_saving = abap_true )

Read only

former_member1716
Active Contributor
0 Likes
5,627

Hello Franziskus Heep,

Am doubtful on the event that you have selected, Check if it is working fine if not try with event 05 as well and let us know the success case.

Read only

Former Member
5,627

Hi Franziskus,

Several ways to do this. I just briefly describe one, assuming you go with 'classic' ABAP since you mention a FORM.

* Assume your internal table of incoming data is something like:
TYPES: BEGIN OF tys_val_buk,
         bukrs TYPE bukrs,
         val   TYPE p DECIMALS 2,
       END OF tys_val_buk,
       tyt_val_buk TYPE STANDARD TABLE OF tys_val_buk.

* You may define a result set: TYPES: tyt_res_buk TYPE SORTED TABLE OF bukrs WITH KEY bukrs. DATA: lt_val_buk TYPE tyt_val_buk, lt_res_buk TYPE tyt_res_buk. * You may call your FORM like this: PERFORM check_buk_val USING lt_val_buk CHANGING lt_res_buk. * Do whatever you want to evaluate the results list of BUKRS / FLAG combo. * Your FORM may look like this: FORM check_buk USING it_val_buk TYPE tyt_val_buk CHANGING ct_res_buk TYPE tyt_res_buk. DATA lt_val TYPE tyt_val_buk. DATA lf_val TYPE p DECIMALS 2. FIELD-SYMBOLS: <val> TYPE tys_val_buk. * Make sure all Company Codes are together lt_val = it_val_buk. SORT lt_val BY bukrs. * Evaluate
LOOP AT lt_val ASSINGING <val>. * Group change. Remember results for all Companies with sum val >=1. AT NEW bukrs. IF lf_val >= 1.
INSERT <val>-bukrs INTO ct_res_buk.
ENDIF.
CLEAR lf_val. ENDAT. * Accumulate the values lf_val = lf_val + <val>-val. ENDLOOP. ENDFORM.

Of course this can be solved much more elegantly using ABAP OO, but we are not there yet 😉... Also, I don't guarantee buglessness... 😊

Also, for future reference, and to better familiarize yourself with the ABAP concepts, check the ABAP help, e.g.:

https://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm

Hope that gets you started,
Mike

Read only

Sandra_Rossi
Active Contributor
5,627

You can implement the event 01 (before save), but the data is not saved in the database yet, it's in the internal table TOTAL. It has a special format that you must first transfer to an internal table with usable format, as shown below. I use the same names (LT_VAL_BUK) as proposed by Mike.

FORM z_before_saving.
 field-symbols <ls_val_buk> type name_of_your_table.
 data lt_val_buk type name_of_your_table.

 LOOP AT total.
   assign total to <ls_val_buk> casting.
   append <ls_val_buk> to lt_val_buk.
 ENDLOOP.

 " your algorithm to find error - do as Mike said or algo you want

 IF error = abap_true.
   MESSAGE 'ERROR' TYPE 'S' DISPLAY LIKE 'E'.
   VIM_ABORT_SAVING = 'X'. " do not leave in case of error
 ENDIF.

 sy-subrc = 0. " save is done only if sy-subrc = 0
ENDFORM.