Skip to Content

Delete a Customer Record in an MDK App

Allow the user to delete a customer record in an MDK app.
You will learn
  • How to delete a customer record
  • How to store changes locally on Mobile app and sync these changes with backend
  • How to update a record in web application
jitendrakansalJitendra KansalJuly 8, 2022
Created by
jitendrakansal
May 23, 2019
Contributors
jitendrakansal
jitendrakansal
jitendrakansal

You may clone an existing project from GitHub repository to start with this tutorial.

MDK
  • Step 1

    The next step is to store deleted record locally for an offline application or delete directly back to the backed for online applications. You will now create an OData delete action to delete a customer record. You will also show a failure message if the delete action fails.

    1. Define a failure message action for displaying a message in case deleting of a customer fails.

      Right-click the Actions folder | MDK: New Action | choose MDK Message Actions in Category | click Message Action | Next.

      MDK

      Provide the below information:

      Property Value
      Action NameDeleteCustomerEntityFailureMessage
      Type Select Message from the dropdown
      Message Delete entity failure - {#ActionResults:delete/error}
      Title Delete Customer
      OKCaption OK
      OnOK --None--
      CancelCaption leave it blank
      OnCancel --None--
      MDK

      In this expression {#ActionResults:delete/error}, delete is a reference to the Action Result name in the Delete Entity action (you will create this action in following step). This reference is used to pass the results to subsequent actions in the chain. These actions can reference the action result as needed. In this case if there is a failure, you access the error property of the action result to display the OData failure message.

      This is the standard Binding Target Path (also called Dynamic Target Path) syntax used when you need to include a binding with other bindings or within a string as used in the message here.

      You could exclude above expression and can just display a generic message.

      Click Next and then Finish on the Confirmation step.

      Ignore the error cannot get action result for delete as delete reference is currently missing, it will be fixed after sub-step 3.

    2. Next, you will create the OData Delete action to delete a customer record.

      You can find more details about Delete Entity Action.

      Right-click the Actions folder | MDK: New Action | choose MDK Data Actions in Category | click OData Action | Next.

      MDK

      Provide the below information:

      Property Value
      Action NameCustomers_DeleteEntity
      Type Select DeleteEntity from the dropdown
      ServiceSelect SampleServiceV2.service from the dropdown
      EntitySet Select Customers from the dropdown
      ReadLinkclick link icon and double click readLink
      MDK

      The readLink is a direct reference to an individual entity set entry.

      Click Next and Finish on the confirmation screen. The action editor will open with the Customers_DeleteEntity.action loaded.

    3. Next, define Success and Failure actions for Customers_DeleteEntity.action.

      In the action editor for the new action, expand the Common Action Properties and provide the below information:

      Property Value
      Action Resultdelete
      Success Action Click the link icon and bind it to CloseModalPage_Complete.action
      Failure Action Click the link icon and bind it to DeleteCustomerEntityFailureMessage.action
      MDK

      When Customers_DeleteEntity.action gets executed successfully then CloseModalPage_Complete.action will be triggered or if Customers_DeleteEntity.action fails then DeleteCustomerEntityFailureMessage.action will be triggered.

      delete value for Action Result is reference to DeleteCustomerEntityFailureMessage.action created in sub-step 1.

      You could also show a success message for Success Action or chain a message to success of CloseModalPage_Complete.action.

  • Step 2

    In the MDK editor, you will write a rule in JavaScript called Customers_DeleteConfirmation.js to display a message to confirm if user wants to delete current record. On it’s confirmation, customer delete entity action is executed.

    1. Right-click the Actions folder | MDK: New Action | choose MDK Message Actions in Category | click Message Action | Next.

      MDK

      Provide the below information:

      Property Value
      Action NameDeleteConfirmation
      Type Select Message from the dropdown
      Message Delete current entity?
      Title Delete Confirmation
      OKCaption OK
      OnOK --None--
      CancelCaption CANCEL
      OnCancel --None--
      MDK
    2. Right-click the Rules folder | MDK: New Rule File | select Empty JS Rule.

      MDK
    3. Enter the Rule name Customers_DeleteConfirmation, click Next and then Finish on the confirmation step.

      Copy and paste the following code.

      JavaScript
      Copy
      export default function DeleteConfirmation(context) {
          return context.executeAction('/DemoSampleApp/Actions/DeleteConfirmation.action').then((result) => {
              if (result.data) {
                  return context.executeAction('/DemoSampleApp/Actions/Customers_DeleteEntity.action').then(
                      (success) => Promise.resolve(success),
                      (failure) => Promise.reject('Delete entity failed ' + failure));
              } else {
                  return Promise.reject('User Deferred');
              }
          });
      }
      

      In above code there is a reference to Customers_DeleteEntity.action , you can navigate directly from here to the MDK file by right clicking on it.

      MDK
    4. Save the changes.

    Which component distributes the Mobile Development Kit (MDK) metadata to the MDK Mobile client?

  • Step 3

    You will add a button to the Customer Detail page called Trash. You will link this button to the Customers_DeleteConfirmation.js rule you just created. This event will display a dialog when the Trash button is pressed by the end-user.

    1. In Customers_Detail.page, drag and drop an Action Bar Item to the upper right of the action bar.

      MDK

      Action Bar Item is a button that users can use to fire actions when pressed. You can add an Action Bar Item only to the Action Bar (at the top of the page).

      Click the link icon to open the object browser for the System Item property.

      Double click the Trash type and click OK.

      MDK
    2. Next, click the Events tab, click the 3 dots icon for the OnPress property to open the Object Browser.

      Double click the Customers_DeleteConfirmation.js rule and click OK to set it as the OnPress Action.

      You could link OnPress property directly to OData delete action directly instead to this JavaScript file. Idea of linking to JavaScript file is to let you understand another way to achieve similar functionality.

      MDK
  • Step 4

    Deploy the updated application to your MDK client.

    1. Right-click Application.app and select MDK: Deploy.

      MDK
    2. Select deploy target as Mobile & Cloud.

      MDK

      You should see success message for both deployments.

      MDK

      Alternatively, you can select MDK: Redeploy in the command palette (View menu>Find Command OR press Command+Shift+p on Mac OR press Ctrl+Shift+P on Windows machine), it will perform the last deployment.

      MDK
  • Step 5

    Make sure you are choosing the right device platform tab above.

    You can cross verify if this record has been deleted in the backend.

    Backend URL can be found in Mobile Services Cockpit.

    Mobile Applications | Native/Hybrid | click the MDK App com.sap.mdk.demo | Mobile Connectivity | click Launch in Browser icon

    MDK

    It will open the URL in a new tab, remove ?auth=uaa and add /Customers at the end of the URL.

    What is the ActionResult name set in the Customers_DeleteEntity.action?

Back to top