Creating Custom Business Objects

Objectives

After completing this lesson, you will be able to:
  • Create custom business objects
  • Create a UI for a custom business object
  • Add custom logic to a custom business object

Custom Business Objects

Custom Business Objects is a tool used to create your own business objects for use as database tables, or as the basis for custom applications.

Amongst other things, you can:

  • Create your own business objects and the corresponding database tables
  • Add fields to business objects
  • Generate OData services for custom business objects to work with and to modify custom business objects externally
  • Publish business objects
  • Edit business objects that have already been published
  • Create multiple subnodes for one business object
  • Create associations between custom business objects for reuse
  • Implement custom logic on node level
  • Reset business objects to their last published version
temp

To create a custom business object, proceed as follows:

  1. Open the app Custom Business Objects.
  2. Choose New.

Don't forget to publish your custom business object. Optionally, you can decide later if you want to add custom logic to your custom business object. You can add custom logic after you published your custom business object.

temp

In the Fields section of the Custom Business Objects app, add fields to your custom business object and thus define what the columns of your database table are called.

temp

In the General Information section, you can activate the check-box Service Generation. Activate this check-box, if you want to use an OData service to modify your custom business object from externally.

You can also optionally activate the check-box System Administrative Data. This automatically generates and fills fields that are used to store administrative data, relevant for the custom business objects. The following fields are created:

  • Created On
  • Created By
  • Last Changed On
  • Last Changed By

Using the check-box Can be Associated, you enable your custom business object to be associated with another custom business object for reference and for use as value help.

How to Create a Custom Business Object

For the steps and data of this demonstration, refer to the exerciseCreate a Custom Business Object.

Create a Custom Business Object

Business Scenario

In this exercise, you create a custom business object. This business object will be enhanced in later exercises. You will generate an SAP Fiori UI and add custom logic.

Note

In this exercise, when the values include ##, replace ## with your group number.

Steps

  1. Open the app Custom Business Objects.

    1. To start the SAP Fiori launchpad of the S4D system, choose the path 10 Developments4dhost10 S4D Fiori Launchpad in the bookmarks bar of the browser.

    2. In the SAP Fiori launchpad, choose the Custom Business Objects tile.

  2. In the Custom Business Objects app, create a new business object with the following values:

    NameIdentifierName in PluralScenario
    Customer Invoice ##ZZ1_CUSTOMERINVOICE##Customer Invoices ##Standard
    1. On the Custom Business Objects tab in the Custom Business Objects app, choose New from the toolbar of the displayed table.

    2. On the New Custom Business Object dialog that displays, fill out the form with the values from the table above and choose Create.

  3. Activate the check-box Service Generation for the new business object.

    Note

    The activated Service Generation check-box ensures that an OData service is created for your business object. This OData service is required by the SAP Fiori UI to access the business object data. You will generate this SAP Fiori UI for the business object in the next exercise.
    1. In the details view for editing the new custom business object, make sure that the General Information tab is displayed.

    2. Tick the Back End Service check-box.

  4. Add the following fields to the node CUSTOMERINVOICE## of your business object:

    LabelIdentifierTypeKeyProperties
    IDIDNumeric IdentifierXLength: 10
    Customer IDCustomerIDNumeric Identifier Length: 8
    Customer NameCustomerNameText Length: 25, Read Only: X
    AmountAmountAmount with Currency  
    Discount (%)DiscountNumber Length: 5, Decimals: 2, Read Only: X
    Discount AmountDiscountAmountAmount with Currency Read Only: X
    is PaidisPaidCheckbox  

    Note

    Make sure that the fields Customer Name, Discount (%), and Discount Amount are read only. Their value will be set in a later exercise by means of custom logic.
    1. Switch to the Fields tab.

    2. Choose New from the toolbar of the displayed table, to create the new fields. Set the Label, Identifier, Type, and Key properties as well as additional Properties of the added fields as indicated in the table above.

  5. Add the standardized fields Created On, Created By, Last Changed On, and Last Changed By to your business object.

    1. Switch to the General Information tab.

    2. Tick the System Administrative Data check-box.

      Note

      The editor now automatically adds the fields Created On, Created By, Last Changed On, and Last Changed By to your business object. You can see these fields in the field list when you switch back to the Fields tab.
  6. Save a draft of your custom business object.

    1. Choose Save Draft in the footer of the page.

  7. Publish your custom business object.

    1. Choose Publish in the footer of the page.

UI Generation

To work with a business object, a user needs a user interface (UI). Using the UI, custom business object entities can be displayed, created, updated, and deleted.

How to Expose an Application based on a Custom Business Object:

Procedure to expose an application

SAP S/4HANA Cloud:SAP S/4HANA, On-Premise-Edition:
1. Activate the checkboxes for UI Generation andService Generation in the Custom Business Objects app:1.Register the OData service that was generated for the custom business object on the SAP Gateway2.Create and deploy an SAP Fiori application based on the activated OData service using the SAP Business Application Studio
2.Create and deploy an SAP Fiori application based on the activated OData service using the SAP Business Application Studio
2.Create a custom catalog extension in the app Custom Catalog Extensions.3.In the SAP Fiori launchpad designer, create a target mapping and a tile for the deployed application in an appropriate Business Catalog.

In SAP S/4HANA Cloud, UI generation is an in-app extensibility feature, done completely inside the SAP S/4HANA system. It is a key user functionality, with limited possibilities. Alternatively, the more powerful side-by-side extensibility, via SAP Business Technology Platform, could be used to develop a custom UI for SAP S/4HANA Cloud with all SAPUI5 options.

To generate an application from a custom business object in SAP S/4HANA Cloud, activate the two check-boxes for UI Generation and Service Generation in the Custom Business Objects app. Afterward publish the business object to trigger the generation of UI (master and detail) and OData Service.

To give users permission to access the generated UI, you have to make it available as an SAP Fiori launchpad application, by assigning it to a business catalog using the Custom Catalog Extensions app.

Note

Your user must have a business role, containing the corresponding business catalog, to view the created application.

In the on-premise version of SAP S/4HANA, there is no check-box to generate the UI. To get an UI, you must perform the steps listed in the figure. To create an SAP Fiori application in the SAP Business Application Studio, you can use the floorplan, for example, List Report Page.

Note

You must still assign the business catalog with the created target mapping and tile to your users via a business role so that they can access the created application. You must also add start authorization for the OData service that was generated for the custom business object to the business role in the Role Maintenance transaction (PFCG).

How to Create a UI for the Custom Business Object

For the steps and data of this demonstration, refer to the exercise Create a UI for the Custom Business Object.

Create a UI for the Custom Business Object

Business Scenario

In this exercise, you provide a UI for the custom business object created in the previous exercise. To do this:

  1. Register the OData service that was generated for your custom business object on the SAP Gateway.
  2. Create an SAP Fiori application based on this OData service using the SAP Business Application Studio.
  3. Deploy it to the ABAP server.
  4. Make the new application available to your user via a tile in the SAP Fiori launchpad.

Note

In this exercise, when the values include ##, replace ## with your group number.

Steps

  1. Register the OData service ZZ1_CUSTOMERINVOICE##_CDS that was generated for your custom business object on the SAP Gateway using your package ZS4D425_##.

    1. If you are not yet logged into the system S4D, select the S4D SAP GUI SNC [PAS] system entry in SAP Logon.

    2. Choose Log On.

    3. Enter /n/IWFND/MAINT_SERVICE in the command field of the SAP GUI window and press Enter.

      Result

      The Activate and Maintain Services transaction is opened.
    4. Choose Add Service.

    5. On the Add Selected Services screen, select the Co-Deployed check-box.

    6. In the Technical Service Name field, enter *##*.

    7. Choose Get Services.

    8. In the displayed table, select the row that belongs to the OData service ZZ1_CUSTOMERINVOICE##_CDS.

    9. Choose Add Selected Services.

    10. On the dialog that appears, enter your package ZS4D425_## in the Package Assignment field.

    11. Choose Continue.

    12. Make sure that the transport request that belongs to the training course is selected on the following two pop-ups and confirm these pop-ups with Continue.

    13. Close the Information dialog that appears by choosing Continue.

  2. Open your dev space S4D425_## in the SAP Business Application Studio.

    1. Open the SAP Business Application Studio using the URL you received from your instructor.

    2. If you have not been logged on automatically, click the <xxxxxxxxx>.accounts.ondemand.com link that appears on the displayed browser page. If then prompted, enter your E-Mail address followed by the Password in the relevant input fields in the browser. You will receive the required e-mail address and password from your instructor. Finally choose the Log On button to log on to the SAP Business Application Studio.

      Result

      The Dev Space Manager is displayed in the browser.
    3. Make sure that your dev space is in the RUNNING state. If it is not, start it.

    4. Click the name S4D425_## of your dev space as soon as it is in the RUNNING state.

      Result

      The SAP Business Application Studio is opened in your SAP Fiori dev space S4D425_##.
  3. Now create an SAP Fiori elements application using the List Report Page floorplan in the SAP Business Application Studio. The application is to be based on your customer invoice OData service that you have just registered at the SAP Gateway. To do this, use the following values in the respective wizard steps:

    Select Template and Target Location

    FieldValue
    TemplateSAP Fiori application

    Template Selection

    FieldValue
    TemplateList Report Page

    Data Source and Service Selection

    FieldValue
    Data sourceConnect to a System
    SystemS4D_100
    ServiceZZ1_CUSTOMERINVOICE##_CDS (1) - OData V2

    Entity Selection

    FieldValue
    Main entityZZ1_CUSTOMERINVOICE##
    Navigation entityNone

    Project Attributes

    FieldValue
    Module names4d425_cust_invoice_##
    Application titleCustomer Invoices
    Application namespace
    DescriptionManage Invoices
    Project folder path/home/user/projects
    Add deployment configurationYes
    Add FLP configurationNo
    Configure advanced options
    • UI5 version
    • UI5 theme
    • Add Eslint configuration to the project.
    • Add javascript code assist libraries to your project.
    • Enable telemetry data...
    Yes
    • <use proposed value>
    • <use proposed value>
    • No
    • No
    • No

    Deployment Configuration

    FieldValue
    Please choose the targetABAP
    DestinationS4D_100 - http://s4d.virtual:443
    SAP UI5 ABAP Repository (Maximum length: 15 characters)Z_CUST_INV_##
    PackageZS4D425_##
    Transport Request<transport request belonging to the training course>
    Deployment DescriptionStudent ##

    Note

    The deployment configuration can be edited after creating the project via the file ui5-deploy.yaml in the project directory.
    1. Choose FileNew Project from Template from the SAP Business Application Studio menu.

    2. In the Project From Template wizard that appears, select the SAP Fiori application tile and choose Start.

    3. In the Select Template and Target Location step, select SAP Fiori Application as Template. Press Next.

    4. In the Template Selection step, choose the List Report Page tile and press Next.

    5. In the Data Source and Service Selection step, enter the values from the corresponding table above and choose Next.

    6. In the Entity Selection step, enter the values from the corresponding table above and choose Next.

    7. In the Project Attributes step, enter the values from the corresponding table above and choose Next.

    8. In the Deployment Configuration step, enter the values from the corresponding table above and choose Finish.

      Result

      Your SAP Fiori elements project is generated and saved for future use.
  4. Open the s4d425_cust_invoice_## project you just created in a workspace.

    Caution

    Please perform this step only after you see the message in the SAP Business Application Studio that the new SAP Fiori elements project has been generated and saved for future use.
    1. Select FileOpen Workspace... from the SAP Business Application Studio menu.

    2. In the Open Workspace dialog box that appears, select the s4d425_cust_invoice_## folder and choose Open.

      Result

      The s4d425_cust_invoice_## project is opened in the SAP Business Application Studio.
  5. Test your application by starting it from SAP Business Application Studio.

    1. Right-click on the webapp folder in your project s4d425_cust_invoice_## and select Preview Application from the context menu that appears.

    2. Select the npm script named start in the dialog that appears.

      Result

      The application will now be displayed in a new tab.
    3. Choose Create from the toolbar of the displayed table to create a new customer invoice.

    4. On the page that appears, fill the form fields with meaningful values.

      Note

      Note that there is an input help for the currency field.
    5. To save your entries choose the Create button.

    6. Navigate back to the List Report page.

    7. On the List Report page, choose Go.

    8. Select your newly created invoice in the displayed table to navigate to the corresponding object page. Note that the fields Created On, Created By, Last Changed On, and Last Changed By were filled automatically.

      Note

      Please note that the Created By and Last Changed By fields do not show your TRAIN-## user. This is due to the fact that the destination used for the connection to the S4D system uses Basic Authentication. I.e. for the sake of simplicity, the user and associated password for authentication to the S4D system are hard-coded at the destination. Exactly this user is now displayed in the fields Created By and Last Changed By.
  6. Now deploy your project s4d425_cust_invoice_## from the SAP Business Application Studio to the SAP S/4HANA system. To do this, use the npm run deploy command in the terminal.

    1. Choose TerminalNew Terminal from the SAP Business Application Studio menu.

    2. At the command prompt in the terminal, enter the npm run deploy command and press ENTER:

      Code Snippet
      1
      user: $

      Note

      Make sure that the terminal is opened in the project directory. The $ character must be preceded by user:s4d425_cust_invoice_##.

      Note

      If an error occurs during deployment with the message fiori: not found, enter npm install at the command prompt in the terminal and press ENTER:
      Code snippet
      user:  $ 
      Expand
      Then enter npm run deploy again.
    3. When prompted, confirm the deployment configuration displayed and then press y.

      Result

      The deployment to the SAP S/4HANA system is now executed. At the end of the deployment process, you should see the Deployment Successful message in the terminal.
  7. Make the deployed application available to your user via the SAP Fiori launchpad. To do this, create an appropriate target mapping in the catalog S4D425 Catalog Student ## (Z_S4D425_BC_STUDENT_##) in the SAP Fiori launchpad designer. This catalog was created in a previous exercise and is already assigned to your user via a role. Use the following values for the target mapping:

    FieldValue
    Semantic ObjectInvoice
    Actionmanage##
    Application TypeSAPUI5 Fiori App
    TitleManage Customer Invoices
    URL/sap/bc/ui5_ui5/sap/z_cust_inv_##
    IDs4d425_cust_invoice_##
    1. Open the SAP Fiori launchpad designer, by choosing the path 10 Developments4dhost10 S4D Fiori Launchpad Designer Configuration in the bookmarks bar of the browser.

    2. In the SAP Fiori launchpad designer, choose Catalogs.

    3. In the Search for catalogs field, enter S4D425 and press Enter.

    4. Select your catalog S4D425 Catalog Student ## (Z_S4D425_BC_STUDENT_##) in the hit list.

    5. Choose Target Mappings at the top of the page.

    6. Choose Create Target Mapping.

    7. Enter the values from the table in the displayed form and choose Save.

  8. For the intent Invoice-manage## used in the just created target mapping, now create a static tile Manage Customer Invoices with subtitle Student ## in the catalog S4D425 Catalog Student ## (Z_S4D425_BC_STUDENT_##) in the SAP Fiori launchpad designer.

    1. In the SAP Fiori launchpad designer, choose Tiles next to Target Mappings.

    2. Choose Create Tile.

    3. Choose App Launcher - Static.

    4. In the General pane, enter the following values:

      FieldValue
      TitleManage Customer Invoices
      SubtitleStudent ##
    5. Select an icon of your choice, using the value help for the Icon field.

    6. In the Navigation pane, enter the following values:

      FieldValue
      Semantic ObjectInvoice
      Actionmanage##
    7. Choose Save.

  9. Now add start authorization for the OData service that was generated for your custom business object to your business role. To do this, edit your role Z_S4D425_BR_STUDENT_## in the Role Maintenance transaction in the S4D system. Add OData start authorization for the activated OData service ZZ1_CUSTOMERINVOICE##_CDS to this role. Then regenerate the authorization profile for the role.

    1. If you are not yet logged into the system S4D, select the S4D SAP GUI SNC [PAS] system entry in SAP Logon.

    2. Choose Log On.

    3. Enter /nPFCG in the command field of the SAP GUI window and press Enter.

    4. In the Role field, enter Z_S4D425_BR_STUDENT_## and choose Change.

    5. Switch to the Menu tab and choose the option Authorization Default for the Insert Node button, by means of the small triangle on the right edge.

    6. In the Service window that displays, choose SAP Gateway: Service Groups Metadata from the menu for the Authorization Default.

    7. In the displayed table, enter the name of the activated OData service ZZ1_CUSTOMERINVOICE##_CDS_0001 in the TADIR Service column and choose Copy.

    8. Choose Save to save your changes.

    9. Now switch to the Authorization tab and choose Change Authorization Data.

    10. On the displayed screen, choose Generate.

  10. Add the invoices app to the group S4D425 (created in a previous exercise) of the Home Page of your SAP Fiori launchpad.

    1. To open the SAP Fiori launchpad of the S4D system, choose the following path in the bookmarks bar of the browser: 10 Developments4dhost10 S4D Fiori Launchpad

    2. Open the User Actions Menu in the upper right corner of the SAP Fiori launchpad and choose App Finder.

    3. In the App Finder, select the catalog S4D425 Catalog Student ## on the left hand side and choose the Add tile icon below the app Manage Customer Invoices (Student ##) on the right hand side.

    4. On the pop-up that appears, add the app to the group S4D425, by ticking the corresponding check-box and choose Close.

    5. Navigate back to the Home Page of the SAP Fiori launchpad.

  11. Test the Manage Customer Invoices app in the SAP Fiori launchpad of the S4D system.

    1. Choose the Manage Customer Invoices tile in the S4D425 group of the SAP Fiori launchpad.

    2. Choose Create in the toolbar of the displayed table to create a new customer invoice.

    3. On the page that appears, fill the form fields with meaningful values.

    4. To save your entries choose the Create button.

Custom Logic and Actions

temp

To add custom logic to a custom business object, you have to enable logic implementation as follows:

  1. In the Custom Business Objects application, switch to the General Information section.
  2. Check the box for Determination and Validation.
  3. Publish the business object.

You can choose between the patterns After Modification and Before Save for your custom logic.

temp

The After Modification pattern is used to recalculate data immediately after node instances have been created or edited, and then saved. Logic of this pattern overwrites field values such as text strings or dates. If node instances are deleted, this pattern is used to inform dependent nodes of the deletion and trigger their recalculation.

In the example shown, the key field ID of the custom business object’s node CUSTOMERINVOICE10 is set if it is still initial. To do this, the largest ID already assigned is selected from the database and increased by 1. The changing parameter CUSTOMERINVOICE10 is used to read and change current node data. Existing customer invoice data is read via the CDS view that is named as the business object's identifier ZZ1_CUSTOMERINVOICE10.

temp

The Before Save pattern is used to validate the consistency of node instances in the save phase. Logic of this pattern checks field values for correctness and displays error messages if field values are incorrect. After this check, the fields can be edited manually. If field values do not comply with the check requirements, the custom business object cannot be saved. The field values need to be corrected before the custom business object can be saved.

In the example shown, we check if the invoice amount is set. The invoice is prevented from being saved if the invoice amount field is not filled with a value. In this case, an error message is returned. The importing parameter CUSTOMERINVOICE10 is used to read current node data. The exporting parameter VALID is set to true for save and to false for save rejection. The exporting parameter MESSAGE is used to provide the validation message.

temp

Actions are used to execute specific logic on data triggered by the UI, created for the custom business object.

Action logic is implemented for one node. This means, the action has read access to all parts of the business object (input parameter), but write access only to the node on which the action logic is implemented (changing parameter). You can also define an error, warning, and success message that is displayed after the action was executed (exporting parameter).

The action in the example shown is used to calculate the discount amount based on the discount granted to the customer. To it, the discount granted to the customer is selected from the CDS view D425_I_CUST10 and applied to the invoice amount to calculate the discount amount. The selected customer discount and the calculated discount amount are assigned to the corresponding fields of the business object. Also, the currency field for the discount amount is assigned the same currency that is used for the invoice amount. An error message is returned if no customer discount could be found on the database. The changing parameter CUSTOMERINVOICE10 is used to read and change current node data. The exporting parameter MESSAGE is used to provide the error message.

How to Implement Custom Logic for a Custom Business Object

For the steps and data of this demonstration, refer to the exercise Implement Custom Logic for your Custom Business Object.

Implement Custom Logic for your Custom Business Object

Business Scenario

In this exercise, you add custom logic to your business object. You will implement After Modification logic to set field values and Before Save logic to check if mandatory fields are filled with values. In addition, you will add an Action to the business object that is used to calculate the discount amount based on the discount granted to the customer.

Note

In this exercise, when the values include ##, replace ## with your group number.

Steps

  1. Enable logic implementation for your custom business object ZZ1_CUSTOMERINVOICE## in the Custom Business Objects application. Save a draft of your changes and publish it.

    1. To start the SAP Fiori launchpad of the S4D system, choose the path 10 Developments4dhost10 S4D Fiori Launchpad in the bookmarks bar of the browser.

    2. In the SAP Fiori launchpad, choose the Custom Business Objects tile.

    3. Open your custom business object ZZ1_CUSTOMERINVOICE## by selecting it in the displayed Custom Business Objects table.

    4. Start the edit mode by choosing Edit Draft.

    5. Make sure that the General Information tab is displayed and tick the check-box Determination and Validation.

    6. Choose Save Draft.

    7. Choose Publishto publish the business object.

  2. After publishing, create a draft version for the After Modification logic and implement it as follows:

    • Set the key field ID of the invoice, if it is still initial. To do this, select the largest IDalready assigned from the database and increase it by 1.
    • Determine and set the customer name from the customer ID. Use the CDS view D425_I_CUST## to select the customer name from the database.

    Note

    • The changing parameter CUSTOMERINVOICE## enables you to read current node data and change it.
    • You can read existing customer invoice data via the CDS view that is named as the business object's identifier ZZ1_CUSTOMERINVOICE##.
    • With the key combination CTRL + Space, you can access the very helpful code completion.
    • With the key combination Shift + F1, you can format your code.
    1. Switch to the Logic tab.

    2. Select the link After Modification.

    3. Choose Edit.

    4. Enter the following coding in the Draft Code editor:

      Code Snippet
      123456789101112
      * set Invoice ID IF customerinvoice##-id IS INITIAL. SELECT MAX( id ) FROM zz1_customerinvoice## INTO @DATA(current_max_id). customerinvoice##-id = current_max_id + 1. ENDIF. * set Customer Name IF customerinvoice##-customerid IS NOT INITIAL. SELECT SINGLE FROM d425_i_cust## FIELDS name WHERE id = @customerinvoice##-customerid INTO @customerinvoice##-customername. ENDIF.
    5. Choose Save to save the draft.

  3. Test the draft of your After Modification logic within the Custom Business Objects application.

    Note

    In tab Test editor, you can maintain runtime data for the current node structure, which represents the data before running the After Modification logic. This data can also be saved as variant for later usages.
    1. Choose the input help for the CUSTOMERINVOICE## field in tab Test (View Importing parameters) and enter appropriate data.

      Note

      You should at least test the following cases:

      • Test the logic if the ID field does not contain a Value.
      • Test the logic if the ID field contains a Value.
      • Test the logic if the CUSTOMERID field contains a valid customer number.
    2. Choose the Test button to execute the test run.

    3. Check the node data after your logic was executed. It is displayed in the Changing Parameters view.

  4. Publish the After Modification logic after successful testing.

    1. Choose Publish.

  5. After publishing test the After Modification logic via your Manage Customer Invoices app.

    1. In the SAP Fiori launchpad, choose your Manage Customer Invoices tile in the S4D425 group.

    2. Select Create in the toolbar of the displayed table to create a new customer invoice.

    3. On the page that appears, enter a valid customer number in the Customer ID field and choose Create.

    4. Check if the ID field and the Customer Name field were set for this invoice by the After Modification logic.

  6. Now create a draft version for the Before Save logic of your business object and check the correctness of the invoice data in your implementation. Ensure the following:

    • The invoice amount must be set.
    • The currency of the invoice amount must be set.
    • The customer ID must be set.

    Prevent the invoice from being saved if the invoice data is incorrect. In this case, return a suitable error message.

    Note

    • The importing parameter CUSTOMERINVOICE## enables you to read current node data.
    • The exporting parameter VALID must be set to true for save and to false for save rejection.
    • Use the exporting parameter MESSAGE to provide corresponding validation messages.
    • With the key combination CTRL + Space, you can access the very helpful code completion.
    • With the key combination Shift + F1, you can format your code.
    1. Open your custom business object ZZ1_CUSTOMERINVOICE## in the Custom Business Objects application.

    2. Switch to the Logictab.

    3. Select the link Before Save.

    4. Choose Edit.

    5. Enter the following coding in the Draft Code editor:

      Code Snippet
      12345678910111213141516171819202122
      valid = abap_true. * decide about save rejection * Validation Messages IF customerinvoice##-amount_v IS INITIAL. valid = abap_false. message = 'Invoice amount must be greater than 0'. RETURN. ENDIF. IF customerinvoice##-amount_c IS INITIAL. valid = abap_false. message = 'Invoice amount currency must be set'. RETURN. ENDIF. IF customerinvoice##-customerid IS INITIAL. valid = abap_false. message = 'Customer ID must be set'. RETURN. ENDIF.
    6. Choose Save to save the draft.

  7. Test the draft of your Before Save logic within the Custom Business Objects application.

    1. Choose the input help for the CUSTOMERINVOICE## field.

      Note

      Make sure that you create test cases for the following situations:

      • Test the logic if the AMOUNT_V field contains a Value.
      • Test the logic if the AMOUNT_V field does not contain a Value.
      • Test the logic if the AMOUNT_C field contains a Value.
      • Test the logic if the AMOUNT_C field does not contain a Value.
      • Test the logic if the CUSTOMERID field contains a Value.
      • Test the logic if the CUSTOMERID field does not contain a Value.
    2. Choose Test to execute the test run.

    3. Check the export parameters after your logic was executed. They are displayed in the Test Results: Draft Logic panel below the ABAP editor.

  8. Publish the Before Save logic after successful testing.

    1. Choose Publish from the page footer.

  9. After publishing test the Before Save logic via your Manage Customer Invoices app.

    1. In the SAP Fiori launchpad, select your Manage Customer Invoices tile in the S4D425 group.

    2. Choose Create in the toolbar of the displayed table to create a new customer invoice.

    3. On the page that is displayed, it should not be possible to save successfully as long as no values have been entered for the fields Customer ID, Amount, and Amount Currency. As long as these values are missing, the corresponding error messages from your Before Save implementation should be displayed.

  10. Now create an Action with LabelApply Customer Discount and IdentifierApplyCustomerDiscount for your business object. Publish your changes.

    1. Open your custom business object ZZ1_CUSTOMERINVOICE## in the Custom Business Objects application.

    2. Switch to the Logic tab.

    3. Choose Edit Draft.

    4. Choose New from the toolbar of the Actions of CUSTOMERINVOICE## table.

    5. Enter the LabelApply Customer Discount and the IdentifierApplyCustomerDiscount for the new Action and choose Publish.

  11. After publishing implement the following draft for the newly created Action:

    • Select the discount granted to a customer from the database.
    • Apply the selected discount to the invoice amount to calculate the discount amount.
    • Make sure that the selected customer discount and the calculated discount amount are assigned to the corresponding fields of the business object. Also, assign the currency field for the discount amount the same currency that is used for the invoice amount.
    • Return an appropriate error message if no customer discount could be found on the database.

    Note

    • Use the CDS view D425_I_CUST## to select the customer discount from the database. It is provided via the custom field ZZ1_DISCOUNT_##_G##, which you created in a previous exercise.
    • The changing parameter CUSTOMERINVOICE## enables you to read current node data and change it.
    • Use the exporting parameter MESSAGE to provide an appropriate error message.
    • With the key combination CTRL + Space, you can access the very helpful code completion.
    • With the key combination Shift + F1, you can format your code.
    1. In the Custom Business Objects application, make sure that the Logic tab is displayed.

    2. Click the link Apply Customer Discount.

    3. Choose Edit.

    4. Enter the following coding in the Draft Code editor:

      Code Snippet
      12345678910111213
      SELECT SINGLE FROM d425_i_cust## FIELDS zz1_discount_##_g## WHERE id = @customerinvoice##-customerid INTO @customerinvoice##-discount. IF sy-subrc = 0. customerinvoice##-discountamount_v = ( customerinvoice##-amount_v * customerinvoice##-discount ) / 100. customerinvoice##-discountamount_c = customerinvoice##-amount_c. ELSE. message = VALUE #( severity = co_severity-error text = 'Discount could not be determined' ). ENDIF.
    5. Choose Save to save the draft.

  12. Test the draft of your Action logic within the Custom Business Objects application.

    1. Choose the input help for the CUSTOMERINVOICE## field in the Test tab (view Importing parameters) and enter appropriate data.

      Note

      You should at least test the following case:

      • Enter in the CUSTOMERID field the number of a customer for whom you have maintained a discount in the Manage Customers app via your corresponding custom field.
      • In addition, enter an invoice amount and a corresponding currency in the fields AMOUNT_V and AMOUNT_C.
    2. Choose the Test button to execute the test run.

    3. Check the returned values after your logic was executed. They are displayed in the Changing parameters view.. In the test case described above, the fields DISCOUNT, DISCOUNTAMOUNT_V, and DISCOUNTAMOUNT_C must have been filled with appropriate values.

  13. Publish the Action logic after successful testing.

    1. Choose Publish from the page footer.

      Note

      If a pop-up is displayed reporting that a consistency check can only be performed on published business objects, confirm it with Close. Publishing your Action logic will still work.
  14. After publishing test the Action logic via your Manage Customer Invoices app.

    1. In the SAP Fiori launchpad, choose your Manage Customer Invoices tile in the S4D425 group.

    2. In the displayed table, select an invoice for a customer for whom you have maintained a discount in the Manage Customers app.

      Note

      If the table does not contain such an invoice, first create one by selecting Create in the toolbar of the table.
    3. On the object page that appears, choose Apply Customer Discount.

    4. Check whether the fields Discount (%) and Discount Amount are filled accordingly.

Log in to track your progress & complete quizzes