So far, we created a custom business object “Bonus Plan” with a simple data structure and application UI. This business object is there to save employee specific rules for bonus entitlement.
Task: Enhance the Custom Business Object’s application with business logic
Example: Bonus plan gets an ID automatically and is checked for consistent data.
Having completed all preceding parts of SAP S/4HANA Extensibility Tutorial.
Step 1: Enabling logic implementation
1. Open the business object “Bonus Plan” in Custom Business Objects application
2. Start Edit Mode by executing the edit draft action.
3. Check the box for “Determination and Validation”
4. Publish the business object definition.
This enables you to implement determination logic which is called after each modification to a Bonus Plan instance as well as validation logic which is called before each save of an instance.
Step 2: Implementing the Bonus Plan’s Business logic
1. “Go to Fields and Logic”.
2. Enter the After Modification Event Logic which is a “Determination Logic”.
3. In the logic view you initially see the not editable empty published version.
Click the “Create Draft” action. An editable copy of the published version appears left to it. With the “Draft Version” and “Published Version” actions you can decide what coding to see.
4. Implement After Modification event with following functionality:
- Set the key field “ID” if still initial.
Hint: You can read existing Bonus Plan data via the CDS View that is named as the Business Object’s Identifier (here: YY1_BONUSPLAN).
Hint: With the key combination CTRL + Space you can access the very helpful code completion.
* set ID IF data-id IS INITIAL. SELECT MAX( id ) FROM yy1_bonusplan INTO @DATA(current_max_id). data-id = current_max_id + 1. ENDIF.
- Set the Unit of Measure for the Bonus Percentages to ‘P1’ which is the code for % (percent)
* set percentage unit data-lowbonuspercentage_u = data-highbonuspercentage_u = 'P1'.
- Determine and set the Employee Name from the Employee ID
Hint: Extensibility offers Helper class CL_ABAP_CONTEXT_INFO with method GET_USER_FORMATTED_NAME that needs a user ID to return its formatted name
* set Employee Name IF data-employeeid IS NOT INITIAL. data-employeename = cl_abap_context_info=>get_user_formatted_name( data-employeeid ). ENDIF.
Consistency Check: In dependence on following checks, set the isconsistent property.
- Check that ValidityStartDate and ValidityEndDate are set and that ValidityStartDate is earlier in time than ValidityEndDate.
- Check that Factors and Percentages are set correctly (all > 0, Percentages < 100, LowBonusAssignmentFactor < HighBonusAssignmentFactor)
- Check that Employee ID is set
* consistency check START IF data-validitystartdate IS INITIAL OR data-validityenddate IS INITIAL OR data-validitystartdate GE data-validityenddate OR data-lowbonusassignmentfactor IS INITIAL OR data-highbonusassignmentfactor IS INITIAL OR data-lowbonuspercentage IS INITIAL OR data-highbonuspercentage IS INITIAL OR data-lowbonuspercentage GE 100 OR data-highbonuspercentage GE 100 OR data-lowbonusassignmentfactor GE data-highbonusassignmentfactor OR data-employeeid IS INITIAL. data-isconsistent = abap_false. ELSE. data-isconsistent = abap_true. ENDIF. * consistency check END
5. On top of the coding you can maintain runtime data for the current node structure which represents the data before running the test functionality. This data can also be saved as variant for later usages.
Enter following data
employeeid <any> shall be the one of a sales person that created sales orders with a Net Amount of more than 3000.00 EUR in 2016 and that are completed.
This will look as follows.
6. Execute the “Test” action and you can see the node data after your logic was executed.
You can see that your logic works as id, *percentage_u fields and employename are filled and isconsistent is X.
7. Publish the After Modification Logic
8. Implement Before Save event with following functionality
- If the bonus plan is consistent, it can be continued to save, if not save shall be rejected. In case of save no further processing is needed and logic can be left.
Hint: Exporting parameter valid must be set to true for save and to false for save rejection
* decide about save rejection IF data-isconsistent EQ abap_true. valid = abap_true. RETURN. ELSE. valid = abap_false. ENDIF.
- If the bonus plan is not consistent, write the first found error into the message and end the logic processing.
These are the possible errors in detail:
- ValidityStartDate and ValidityEndDate must be set
- ValidityStartDate must be earlier in time than ValidityEndDate
- Factors and Percentages must be > 0
- Percentages must be < 100
- LowBonusAssignmentFactor must be < HighBonusAssignmentFactor
- Empoyee ID must be set
* consistency error message START IF data-validitystartdate IS INITIAL OR data-validityenddate IS INITIAL. message = 'Validity Period must not be empty.'. RETURN. ELSEIF data-validitystartdate GE data-validityenddate. CONCATENATE 'Validity End Date' data-validityenddate 'must be later than Validity Start Date' data-validitystartdate '!' INTO message SEPARATED BY space. RETURN. ENDIF. IF data-targetamount_v IS INITIAL. message = 'Target Amount must be over 0!'. RETURN. ENDIF. IF data-targetamount_c IS INITIAL. message = 'Target Amount Currency must be set!'. RETURN. ENDIF. IF data-lowbonusassignmentfactor IS INITIAL OR data-highbonusassignmentfactor IS INITIAL. message = 'Assignment Factors must be over 0!'. RETURN. ENDIF. IF data-lowbonuspercentage IS INITIAL OR data-highbonuspercentage IS INITIAL. message = 'Percentages must be over 0!'. RETURN. ENDIF. IF data-lowbonuspercentage GE 100 OR data-highbonuspercentage GE 100. message = 'Percentage must be below 100!'. RETURN. ENDIF. IF data-lowbonusassignmentfactor GE data-highbonusassignmentfactor. message = 'Low Bonus Factor must be smaller than High Bonus Factor!'. RETURN. ENDIF. IF data-employeeid IS INITIAL. message = 'Employee ID must be set!'. RETURN. ENDIF. * consistency error message END
9. Publish the Before Save Logic
Step 3: Testing via the UI
Once ensured that both logic implementations were successfully published you can start testing the Application like an end user via the UI.
1. Open the Bonus Plan application
2. Open the Bonus Plan, created in Part I tutorial
3. Save the Bonus plan. You can see that your business logic works as the ID gets filled, but save fails due to the validation error messages for missing percentages.
4. Enter following data
|Low Bonus Percentage||10|
|High Bonus Percentage||20|
5. Save the Bonus Plan. Now it will not be rejected.