In the previous blog (In-app extensibility in S/4HANA Cloud – Part 3 – Working with custom business objects), we saw how a custom business object is created, a UI generated for it and data inserted into it.
In this blog, we will see how some custom logic can be incorporated in the custom business object.
The customer has created a custom business object, but they want to ensure that the employee ID field is not left blank before the data is saved into the object.
A custom logic needs to be implemented to ensure that the key field Employee ID is not left blank while saving data in the custom business object.
To be able to carry out in-app extensions, you must have one of the following roles assigned to your user in the S/4 HANA Cloud system:
1) Navigate to the Custom fields and logic app and open the custom business object created earlier (YY1_EMPLOYEEATTRIBUTES)
2) As we observed in the previous blog, in order to be able to implement custom logic in a custom business object, the “Determination and Validation” feature should be checked. Ensure that this is the case in the custom business object. Also ensure that the object is in “Published” status as only then will the logic implementation be available.
3) Navigate to the “Logic” tab of the custom business object. You will see that the “Determination and Validation” feature will bring two options for custom logic – After modification and Before Save.
4) In our example, we need to put in a validation for ensuring the employee ID is not left blank before saving the data in the custom business object. Therefore, click on the “Before Save” option under the “Determination and Validation” section. This will open up a code editor as shown in the screenshot below
5) Since there is no logic create as yet, the system shows the published logic. To create a new custom logic, click on the “Create Draft” button.
6) The system will show separate editor blocks for the already published and the draft logic that we are going to code now.
7) Note that the draft logic shows the importing and exporting parameters available in this logic section. These can be used to make code changes as necessary. Write your required piece of code in the “Draft Logic” section. Please note that though the code is written in ABAP programming language, the rules of restricted ABAP apply in the custom business object logic. For the list of allowed ABAP statements in the editor of custom business object logic, refer to SAP Documentation here.
8) You can test the logic that you write without even saving or publishing the changes. The “Test” button allows you to do so. In order to test your logic, you will need to simulate the values that you will enter while entering data in the custom business object. Click on the text box against the “EMPLOYEEATTRIBUTES” structure. It already mentions “Click to add values”
9) The system will present a popup to enter the values in the “EmployeeAttributes” node. Enter the values for the fields, leaving the key field employee id blank. Press OK to continue.
10) Back on the editor screen, click on the “Test” button to test the coded logic, with the values entered in the previous step.
11) The system will test both the published as well as the draft logics. Scroll down to see the results for both pieces of logic. Observe the draft logic results as that is what we are interested in. You will see that the code takes effect and the relevant message is displayed.
12) Now, as a further test, provide a value in the employee id field in the employee attributes node and repeat the test process. Observe that this time there is no message and the values are acceptable as valid values (valid flag is true).
13) As the test results are satisfactory, we need to save the changes to the custom business object. Go ahead and publish the object for the logic changes to take effect.
14) The system might take a few minutes to publish the custom business object. Ensure that the “Published Version” of the logic has the code you wrote once this is done.
15) Now that the custom business object is published, we can test the logic with actual insert of data in the object. So, navigate to the custom business object’s tile (Employee Attributes) under the Extensibility catalog and try to insert a new node of data in the object – without an employee ID to ensure that the code works as expected.
16) Though the system will display standard error messages that the mandatory key field was left blank, note that the message from the custom logic is also displayed.
In a similar way, logic for other fields can be incorporated as required. This example shows a relatively simple logic but you can of course write more complex logic as per your requirement referring the example shown above.
You have now seen how a custom logic can be incorporated in a custom business object.
Hope this blog was useful.