Skip to Content

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.


Prerequisites

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:
General:

  • 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

Field Value
validitystartdate 2017-01-01
validityenddate 2017-12-31
targetamount_v 1000
targetamount_c EUR
lowbonusassignmentfactor 1
highbonusassignmentfactor 3
lowbonuspercentage 10
highbonuspercentage 20
employeeid <any>

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

field value
Low Bonus Percentage 10
High Bonus Percentage 20

5. Save the Bonus Plan. Now it will not be rejected.

To report this post you need to login first.

8 Comments

You must be Logged on to comment or reply to a post.

  1. Mohamed Gharbi

    Hi Ulrike,

    Thanks for this serie of Blogs!

    When I go to Custom Business Object and go to Fields  and Logic I cannot find any section with “Determination Logic”. 

    Am I missing anything?

    Thanks

    (0) 
    1. Ulrike Liebherr Post author

       

      Hi Mohamed,

      for logic implementations it’s essential that the custom business object is in status “Published” and a possible draft version (for a change after last publish) was already published again as well.

      As you do not get the Determination and Validation section at all, I assume you missed to publish the very first version of your custom business object.

      Regards

      Ulrike

      (0) 
  2. Mohamed Gharbi

    Hi Ulrike,

    Thanks for you reply!

    The Custom Business Object was already published! I guess you are referring to this:

    Any other tip?

     

    Thank you!

    (0) 
    1. Ulrike Liebherr Post author

      Hi Mohamed,

      you have to check “Determination and Validation” (below “Name in Plural”). To be able to do this, you have to “Edit Draft” before. Afterwards you have to publish again and then you will finally be able to do implementations.
      I am very sorry, that this essential step seems to have gone lost in the blog.

      I will update it next.

      Regards

      Ulrike

      (0) 
  3. Arun Nair

    Hi Ulrike,

    I keep getting the error “Class YY9_DAMK_B4C9A49833AF1930BEA8A could not be saved” when I try to save the Implementation. Could you please let me know when does this error occur?

    Is it related to the user authorization? OR any other syntax error? I can although see that the class mentioned in the error message is already created at the backend.

    Regards

    Arun.

    (0) 
    1. Ulrike Liebherr Post author

       

      Hi Arun,

      please excuse the late reply, I was sick last week.

      Sorry, but I cannot provide support on the shown topics, please open a ticket to BC-SRV-APS-EXT-BO in case your issue still persists. As the message is quite generic I fear it can have many reasons. I would exclude a syntax error as this should be marked in the editor already.

      Good luck!

      Regards

      Ulrike

      (0) 

Leave a Reply