Skip to content

OData Delete

The ODataDeleteCL class leverages the sap.ui.model.odata.v2.ODataModel to facilitate the handling of DELETE requests in a promisified manner.

Constructor

In order to utilise the functionality of ODataDeleteCL, it is necessary to initialise the object.

Parameter Type Mandatory Default Value Description
controller sap.ui.core.mvc.Controller Yes The controller object (usually this object)
entityPath string Yes The name of the EntitySet. It can start with a "/" (slash)
modelName? string No undefined The name of the OData V2 model which can be found on the manifest.json file. Leave this parameter undefined if the name of the OData model = "" (empty string)

Tip for TypeScript

The ODataDeleteCL<EntityKeysT> is a generic class that can be initialized with a type containing the key properties of the EntitySet specified as a parameter in the class constructor.

  • The EntityKeysT type is used as the return type and type for the keys parameter of the delete(keys: EntityKeysT): Promise<EntityKeysT> method.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Controller from "sap/ui/core/mvc/Controller";
import ODataDeleteCL from "ui5/antares/odata/v2/ODataDeleteCL"; // Import the class

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onDeleteProduct() {
    // Initialize without a type
    const odata = new ODataDeleteCL(this, "Products"); 
  }

  public async onDeleteCategory() {
    // Initialize with a type
    const odata = new ODataDeleteCL<ICategoryKeys>(this, "Categories"); 
  }

  public async onDeleteCustomer() {
    // Initialize with a model name
    const odata = new ODataDeleteCL(this, "Customers", "myODataModelName"); 
  }
}

interface ICategoryKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataDeleteCL" // Import the class
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataDeleteCL) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onDeleteProduct: async function () {
          // Initialize
          const odata = new ODataDeleteCL(this, "Products"); 
        },

        onDeleteCategory: async function () {
          // Initialize with a model name
          const odata = new ODataDeleteCL(this, "Categories", "myODataModelName");
        }
      });

    });

Delete Request

To send a DELETE request through the OData V2 model, you can use the delete(keys: EntityKeysT): Promise<EntityKeysT> method.

Info

  • The delete() method runs asynchronously and can be awaited.
  • If the request is successful, the delete() method will return the key data of the deleted entity.

Warning

In the event of a failed DELETE request, the OData Delete class will generate an error message. To ensure the error is identified and addressed, it is recommended to call the delete() method within a try-catch block.

Error Type

In the event of a failed DELETE request, the object generated by the class can contain the properties outlined below.

Returns Description
object
    headers?: object | undefined The HTTP response headers.
    message?: string | undefined The HTTP response message.
    responseText?: string | undefined The HTTP response text.
    statusCode?: string | number | undefined The status code of the HTTP request.
    statusText?: string | undefined The HTTP status text.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Controller from "sap/ui/core/mvc/Controller";
import ODataDeleteCL from "ui5/antares/odata/v2/ODataDeleteCL"; // Import the class
import { IError } from "ui5/antares/types/common"; // Import the error type
import MessageBox from "sap/m/MessageBox";

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onDeleteProduct() {
    // Initialize with a type
    const odata = new ODataDeleteCL<IProductKeys>(this, "Products"); 

    try {
      // send the http request and get the result. Note: You have to specify the key values of the entity that will be deleted
      const result = await odata.delete({
        ID: "3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"
      });

      MessageBox.information("The entity with the following ID: " + result.ID + " was deleted.");
    } catch (error) {
      // catch the error
      MessageBox.error((error as IError).message || "Request failed");
    }
  }

}

interface IProductKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataDeleteCL", // Import the class
    "sap/m/MessageBox"
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataDeleteCL, MessageBox) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onDeleteProduct: async function () {
          // Initialize
          const odata = new ODataDeleteCL(this, "Products"); 

          try {
            // send the http request and get the result. Note: You have to specify the key values of the entity that will be deleted
            const result = await odata.delete({
              ID: "3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"
            });

            MessageBox.information("The entity with the following ID: " + result.ID + " was deleted.");
          } catch (error) {
            // catch the error
            MessageBox.error(error.message || "Request failed");
          }          
        }
      });

    });

URL Parameters

Prior to sending the DELETE request with the delete() method, it is possible to set the URL parameters using the setUrlParameters() method.

Parameter Type Mandatory Description
urlParameters Record<string, string> Yes The URL parameters of the DELETE request

Returns Description
Record<string, string> | undefined Returns the value that was set using setUrlParameters() method. Default value is undefined

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import Controller from "sap/ui/core/mvc/Controller";
import ODataDeleteCL from "ui5/antares/odata/v2/ODataDeleteCL"; // Import the class

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onDeleteProduct() {
    // Initialize with a type
    const odata = new ODataDeleteCL<IProductKeys>(this, "Products"); 

    // set the url parameters
    odata.setUrlParameters({
      "$expand": "toProductLocations"
    });
  }

}

interface IProductKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataDeleteCL" // Import the class
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataDeleteCL) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onDeleteProduct: async function () {
          // Initialize
          const odata = new ODataDeleteCL(this, "Products"); 

          // set the url parameters
          odata.setUrlParameters({
            "$expand": "toProductLocations"
          });         
        }
      });

    });

Refresh After Change

The OData V2 model will be automatically refreshed after the DELETE request has been completed.

To change the default behavior, the setRefreshAfterChange() method can be utilized.

Parameter Type Mandatory Description
refreshAfterChange boolean Yes If set to false, the OData V2 model will not be refreshed after the request has been completed

Returns Description
boolean Returns the value that was set using setRefreshAfterChange() method. Default value is true

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import Controller from "sap/ui/core/mvc/Controller";
import ODataDeleteCL from "ui5/antares/odata/v2/ODataDeleteCL"; // Import the class

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onDeleteProduct() {
    // Initialize with a type
    const odata = new ODataDeleteCL<IProductKeys>(this, "Products"); 

    // deactivate the auto model refresh
    odata.setRefreshAfterChange(false);
  }

}

interface IProductKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataDeleteCL" // Import the class
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataDeleteCL) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onDeleteProduct: async function () {
          // Initialize
          const odata = new ODataDeleteCL(this, "Products"); 

          // deactivate the auto model refresh
          odata.setRefreshAfterChange(false);      
        }
      });

    });

Additional Response Info

The delete() method returns the key data of the successfully deleted entity. However, you may require further information such as the status code and headers.

Once the delete() function has been completed, the getResponse() method can be utilized to obtain further details.

Returns Description
object
    $reported?: boolean | undefined
    body?: string | undefined The HTTP body
    headers?: object | undefined The HTTP response headers.
    statusCode?: string | number | undefined The status code of the HTTP request.
    statusText?: string | undefined The HTTP status text.
    _imported?: boolean | undefined
    data?: EntityT | undefined The data that was deleted

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import Controller from "sap/ui/core/mvc/Controller";
import ODataDeleteCL from "ui5/antares/odata/v2/ODataDeleteCL"; // Import the class
import { IError } from "ui5/antares/types/common"; // Import the error type
import MessageBox from "sap/m/MessageBox";

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onDeleteProduct() {
    // Initialize with a type
    const odata = new ODataDeleteCL<IProductKeys>(this, "Products"); 

    try {
      // send the http request and get the result
      const result = await odata.delete({
        ID: "3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"
      });

      MessageBox.information(result.ID + " was deleted.");

      // get the additional response info
      const response = odata.getResponse();

      if (response) {
        console.log("Status Code: " + response.statusCode);
      }
    } catch (error) {
      // catch the error
      MessageBox.error((error as IError).message || "Request failed");
    }
  }

}

interface IProductKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataDeleteCL", // Import the class
    "sap/m/MessageBox"
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataDeleteCL, MessageBox) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onDeleteProduct: async function () {
          // Initialize
          const odata = new ODataDeleteCL(this, "Products"); 

          try {
            // send the http request and get the result
            const result = await odata.delete({
              ID: "3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"
            });

            MessageBox.information(result.ID + " was deleted.");

            // get the additional response info
            const response = odata.getResponse();

            if (response) {
              console.log("Status Code: " + response.statusCode);
            }            
          } catch (error) {
            // catch the error
            MessageBox.error(error.message || "Request failed");
          }          
        }
      });

    });