Application Development and Automation Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
xSAGARx
Explorer
814

What is determination in SAP?

  • Determinations are used to execute specific business logic automatically whenever data in a business object changes.
  • They help ensure that dependent values and calculated fields are correctly set when data is created, modified, or saved.
  • We have 2 types in determination
    1. On Save: It will trigger during the time of saving the data into DB.
    2. On modify: It will trigger before saving the data into DB.

Steps to achieve determination Scenario: I have to calculate the total price based on car price and quantity field, If the car quantities are edited by user, the total price should be calculated. Based on the car price and quantity by using determination. We declare determination in behavior definition of interface view.

Behavior Definition:

 

unmanaged implementation in class zbp_sag_i_car unique; 
strict ( 2 ); 
with draft; 
define behavior for ZSAG_i_CAR 
draft table zsag_dr_car 
lock master total etag LastChangedAt 
authorization master ( instance ) 
early numbering 
{ 
create; 
update; 
delete; 
determination total_price on modify { field Quantity; field CarPrice; } 
side effects 
{ field quantity affects field totalprice; 
field CarPrice affects field TotalPrice; } 
draft determine action Prepare { } 
draft action Activate optimized; 
draft action Discard; 
draft action Edit; 
draft action Resume; 
field ( readonly ) totalprice, CarId; 
 
mapping for zsag_dt_car control zsg_s_car 
{ 
CarId          =  car_id;
CarBrand       =  car_brand;
CarModel       =  car_model; 
CarPrice       =  car_price; 
CukyField      =  cuky_field; 
quantity       =  quantity; 
totalprice     =  total_price; 
LastChangedAt  =  last_changed_at; 
} 
} 

 

  • Note: It is not mandatory to specify the CRUD operation inside {}, If we specify any CRUD operation such as create or update along with the fields as trigger conditions then the determination will be triggered multiple times on each create/update of any other fields in the same entity. Specifying only the fields as trigger condition will limit the call of determination on change of that of field only and will save the extra computation time and improve the application performance. 
  • We will get the warning after declaring determination so implement the method Implementation. 

 

determination total_price on modify { field Quantity; field CarPrice; } 

 

Database Table:

 

@EndUserText.label : 'Car table' 
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE 
@AbapCatalog.tableCategory : #TRANSPARENT 
@AbapCatalog.deliveryClass : #A 
@AbapCatalog.dataMaintenance : #RESTRICTED 

define table zsag_dt_car { 
key clnt : abap.clnt not null; 
key car_id      : abap.char(8) not null; 
car_brand       : abap.char(20); 
car_model       : abap.char(20); 
@Semantics.amount.currencyCode : 'zsag_dt_car.cuky_field' 
car_price       : abap.curr(7,2); 
cuky_field      : abap.cuky; 
quantity        : int1; 
@Semantics.amount.currencyCode : 'zsag_dt_car.cuky_field' 
total_price     : abap.curr(7,2); 
last_changed_at : abp_locinst_lastchange_tstmpl; 
} 

 

Projection View:

 

projection; 
strict ( 2 ); 
use draft; 
use side effects; 
define behavior for ZSAG_C_CAR  
{ 
use create; 
use update; 
use delete; 
use action Prepare; 
use action Activate; 
use action Discard; 
use action Edit; 
use action Resume;
} 

 

Read Method:

 

METHOD read. 
SELECT FROM zsag_DT_CAR 
FIELDS * 
FOR ALL ENTRIES IN  
WHERE car_id = -CarId 
INTO 
TABLE (lt_result). 
result = CORRESPONDING #( lt_result MAPPING TO ENTITY ). 
ENDMETHOD.

 

For Calculating 'Total Price' method:

 

METHOD for_total_price. 
READ ENTITY IN LOCAL MODE zsag_i_car 
ALL FIELDS WITH 
CORRESPONDING #( keys ) 
RESULT DATA(lt_res). 
DATA(ls_res) = VALUE #( lt_res[ 1 ] OPTIONAL ). 
IF ls_res-Quantity IS NOT INITIAL. 
ls_res-TotalPrice = ls_res-Quantity * ls_res-CarPrice. 
MODIFY ENTITY IN LOCAL MODE zsag_i_car 
UPDATE 
FIELDS ( TotalPrice ) 
WITH VALUE #( ( %tky = ls_res-%tky 
TotalPrice = ls_res-TotalPrice ) ). 
ENDIF.
ENDMETHOD. 
 

 

Save:

 

METHOD save. 
IF lhc_ZSAG_i_CAR=>lt_create IS NOT INITIAL. 
INSERT zsag_dt_car FROM TABLE @lhc_ZSAG_i_CAR=>lt_create. 
ENDIF. 
IF lhc_ZSAG_i_CAR=>lt_delete IS NOT INITIAL. 
DELETE zsag_dt_car FROM TABLE @lhc_ZSAG_i_CAR=>lt_delete. 
ENDIF. 
IF lhc_ZSAG_i_CAR=>lt_update IS NOT INITIAL. 
UPDATE zsag_dt_car FROM TABLE @lhc_ZSAG_i_CAR=>lt_update. 
ENDIF. 
ENDMETHOD. 

 

OUTPUT:

  • While creating a record:

undefined.png

  • If user click on enter, the determination logic will get triggered based ON MODIFY and side effects statements:

undefined (1).png

  • After clicking on 'Create' button the record will store in database. 

undefined (2).png

NOTE: If you are using Determination 'on save', you need to include Determination method in the Draft prepare action.

xSAGARx_0-1737722426609.png

Conclusion: 

Use determinations for automatic field derivations and validations during lifecycle operations instead of coding logic manually in the application layer. 

2 Comments
AmveshKumar
Explorer
0 Kudos

Thankyou sagar it help alots to boost my knowledge

 

Faizan_khan1
Explorer
0 Kudos

Great work

Labels in this area