Skip to Content

Delete OData Entities with the SAP Cloud SDK's Virtual Data Model

test
0 %
Delete OData Entities with the SAP Cloud SDK's Virtual Data Model
Details
// Explore More Tutorials

Delete OData Entities with the SAP Cloud SDK's Virtual Data Model

Delete OData entities with the SAP Cloud SDK's virtual data model to duild an address manager application.

You will learn

  • How to use the Virtual Data Model to delete an entity
  • How to trigger a delete request from an API endpoint exposed by your application

The goal of this tutorial group is to show you how to implement a JavaScript application that allows you to manage the addresses of business partners. This application will be using Express.js and the SAP Cloud SDK for JavaScript. In this tutorial, we use the SAP Cloud SDK’s OData Virtual Data Model to delete an address and make this functionality available via an API endpoint.


Step 1: Add an API endpoint

Start by creating a new file called delete-business-partner-address-route.ts and copy the following code into it:

import { Request, Response } from 'express';
import { BusinessPartnerAddress } from '@sap/cloud-sdk-vdm-business-partner-service';

export function deleteBusinessPartnerAddressRoute(req: Request, res: Response) {
  deleteBusinessPartnerAddress(req.params.id, req.params.addressId)
    .then(() => {
      res.status(200).send('Entity successfully deleted!');
    })
    .catch(error => {
      res.status(500).send(error.message);
    })
}

function deleteBusinessPartnerAddress(businessPartnerId: string, addressId: string): Promise<void> {
  return Promise.resolve();
}

This follows the implementation in the previous tutorials. deleteBusinessPartnerAddress does not do anything useful yet, but you will implement it in the next step. Now open application.ts, import the function and add the following route definition:

import { businessPartnersRoute } from './business-partners-route';
import { singleBusinessPartnerRoute } from './single-business-partner-route';
import { createBusinessPartnerAddressRoute } from './create-business-partner-address-route';
import { updateBusinessPartnerAddressRoute } from './update-business-partner-address-route';
import { deleteBusinessPartnerAddressRoute } from './delete-business-partner-address-route';

// ...

private routes(): void {
  const router = express.Router();

  router.get('/', indexRoute);
  router.get('/hello', helloWorld);
  router.get('/business-partners', businessPartnersRoute);
  router.get('/business-partners/:id', singleBusinessPartnerRoute);
  router.post('/business-partners/:id/address', createBusinessPartnerAddressRoute);
  router.put('/business-partners/:id/address/:addressId', updateBusinessPartnerAddressRoute);
  // add the following line
  router.delete('/business-partners/:id/address/:addressId', deleteBusinessPartnerAddressRoute);
  this.app.use('/', router);
}

Start by creating a new file called delete-business-partner-address-route.js and copy the following code into it:

const { BusinessPartnerAddress } = require('@sap/cloud-sdk-vdm-business-partner-service');

function deleteBusinessPartnerAddressRoute(req, res) {
  deleteBusinessPartnerAddress(req.params.id, req.params.addressId)
    .then(() => {
      res.status(200).send('Entity successfully deleted!');
    })
    .catch(error => {
      res.status(500).send(error.message);
    })
}

module.exports.deleteBusinessPartnerAddressRoute = deleteBusinessPartnerAddressRoute;

function deleteBusinessPartnerAddress(businessPartnerId, addressId) {
  return Promise.resolve();
}

This follows the implementation in the previous tutorials. deleteBusinessPartnerAddress does not do anything useful yet, but you will implement it in the next step. Now open application.js, import the function and add the following route definition:

const { businessPartnersRoute } = require('./business-partners-route');
const { singleBusinessPartnerRoute } = require('./single-business-partner-route');
const { createBusinessPartnerAddressRoute } = require('./create-business-partner-address-route');
const { updateBusinessPartnerAddressRoute } = require('./update-business-partner-address-route');
const { deleteBusinessPartnerAddressRoute } = require('./delete-business-partner-address-route');

// ...

private routes() {
  const router = express.Router();

  router.get('/', indexRoute);
  router.get('/hello', helloWorld);
  router.get('/business-partners', businessPartnersRoute);
  router.get('/business-partners/:id', singleBusinessPartnerRoute);
  router.post('/business-partners/:id/address', createBusinessPartnerAddressRoute);
  router.put('/business-partners/:id/address/:addressId', updateBusinessPartnerAddressRoute);
  // add the following line
  router.delete('/business-partners/:id/address/:addressId', deleteBusinessPartnerAddressRoute);
  this.app.use('/', router);
}

Note, that we used router.delete for this route, so we need to send a DELETE request. Restart your server and send a DELETE request to http://localhost:8080/business-partners/1/address/2. The server should respond with "Entity successfully deleted!", since deleteBusinessPartnerAddress returns Promise.resolve() and will therefore never fail.

Log on to answer question
Step 2: Delete a business partner address

Next, we use the VDM to delete a business partner address. Open delete-business-partner-address-route.ts and overwrite deleteBusinessPartnerAddress as shown below:

function deleteBusinessPartnerAddress(businessPartnerId: string, addressId: string): Promise<void> {
  return BusinessPartnerAddress.requestBuilder()
    .delete(businessPartnerId, addressId)
    .execute({
      url: 'https://my.s4hana.ondemand.com/'
    });
}

Next, we use the VDM to delete a business partner address. Open delete-business-partner-address-route.js and overwrite deleteBusinessPartnerAddress as shown below:

function deleteBusinessPartnerAddress(businessPartnerId, addressId) {
  return BusinessPartnerAddress.requestBuilder()
    .delete(businessPartnerId, addressId)
    .execute({
      url: 'https://my.s4hana.ondemand.com/'
    });
}

As of version 1.7.0 of the SAP Cloud SDK, you can alternatively pass an entity to the delete method instead of the entity’s key fields. This has the advantage that the version identifier of the entity is set automatically. When going with the first approach, you would have to manually supply the version identifier using the setVersionIdentifier method. Therefore, if you already have an instance of the entity you want to delete, passing it directly is the easier approach.

Finally, be aware that executing a delete request will return a Promise<void>.

Restart your server and send a delete request to http://localhost:8080/business-partners/1/address/1 (or the respective IDs you used in the previous tutorials). If everything works, you should see "Entity successfully deleted!" in the response body!

Log on to answer question
Step 3: Test yourself
Write the minimum necessary code to delete a Business Partner Address with businessPartnerId '1' and addressId '2', using system 'https://my.cloud.erp.com' (assuming no authentication is necessary).
×

Next Steps

Prerequisites

Back to top