The ODataUpdateCL class leverages the sap.ui.model.odata.v2.ODataModel to facilitate the handling of PATCH/MERGE/PUT (UPDATE) requests in a promisified manner.
Constructor
In order to utilise the functionality of ODataUpdateCL, it is necessary to initialise the object.
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 ODataUpdateCL<EntityT, EntityKeysT> is a generic class and can be initialized with 2 types.
The EntityT type contains all properties of the EntitySet that is specified on the class constructor.
The EntityKeysT type contains the key properties of the EntitySet that is specified on the class constructor.
The EntityT type is used as the type for the data parameter of the setData(data: EntityT) method and as the return type for the getData(): EntityT and update(): Promise<EntityT> methods.
The EntityKeysT type is used as the type for the keys parameter of the update(keys: EntityKeysT) method.
importControllerfrom"sap/ui/core/mvc/Controller";importODataUpdateCLfrom"ui5/antares/odata/v2/ODataUpdateCL";// Import the class/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconUpdateProduct(){// Initialize without a typeconstodata=newODataUpdateCL(this,"Products");}publicasynconUpdateCategory(){// Initialize with a typeconstodata=newODataUpdateCL<ICategory,ICategoryKeys>(this,"Categories");}publicasynconUpdateCustomer(){// Initialize with a model nameconstodata=newODataUpdateCL(this,"Customers","myODataModelName");}}// The properties that will be updated should not be optionalinterfaceICategory{ID:string;name:string;}interfaceICategoryKeys{ID:string;}
sap.ui.define(["sap/ui/core/mvc/Controller","ui5/antares/odata/v2/ODataUpdateCL"// Import the class],/** * @param {typeof sap.ui.core.mvc.Controller} Controller */function(Controller,ODataUpdateCL){"use strict";returnController.extend("your.apps.namespace.YourController",{onInit:function(){},onUpdateProduct:asyncfunction(){// Initializeconstodata=newODataUpdateCL(this,"Products");},onUpdateCategory:asyncfunction(){// Initialize with a model nameconstodata=newODataUpdateCL(this,"Categories","myODataModelName");}});});
Set Data
To define the data that will be transmitted via the UPDATE HTTP request body, the setData() method can be utilized.
Parameter
Type
Mandatory
Description
data
EntityT
Yes
The data that will be sent via the UPDATE HTTP request body
Returns
Description
EntityT
Returns the value that was set using setData() method. Default value is undefined
importControllerfrom"sap/ui/core/mvc/Controller";importODataUpdateCLfrom"ui5/antares/odata/v2/ODataUpdateCL";// Import the class/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconUpdateProduct(){// Initialize with a typeconstodata=newODataUpdateCL<IProducts,IProductKeys>(this,"Products");// set the http body to updateodata.setData({name:"New Product Name",description:"New Description"});}}// The properties that will be updated should not be optionalinterfaceIProducts{ID?:string;name:string;description:string;brand?:string;price?:number;currency?:string;quantityInStock?:number;categoryID?:string;supplierID?:string;}interfaceIProductKeys{ID:string;}
sap.ui.define(["sap/ui/core/mvc/Controller","ui5/antares/odata/v2/ODataUpdateCL"// Import the class],/** * @param {typeof sap.ui.core.mvc.Controller} Controller */function(Controller,ODataUpdateCL){"use strict";returnController.extend("your.apps.namespace.YourController",{onInit:function(){},onUpdateProduct:asyncfunction(){// Initializeconstodata=newODataUpdateCL(this,"Products");// set the http body to updateodata.setData({name:"New Product Name",description:"New Description"});}});});
Update Request
To send a PATCH/MERGE/PUT (UPDATE) request through the OData V2 model, you can use the update(keys: EntityKeysT) method.
Attention
It is a prerequisite that data be set using the setData() method before calling the update() method.
Info
The update() method runs asynchronously and can be awaited.
If the request is successful, the update() method will return the data of the updated entity.
Warning
In the event of a failed UPDATE request, the OData Update class will generate an error message. To ensure the error is identified and addressed, it is recommended to call the update() method within a try-catch block.
Error Type
In the event of a failed UPDATE request, the object generated by the class can contain the properties outlined below.
importControllerfrom"sap/ui/core/mvc/Controller";importODataUpdateCLfrom"ui5/antares/odata/v2/ODataUpdateCL";// Import the classimport{IError}from"ui5/antares/types/common";// Import the error typeimportMessageBoxfrom"sap/m/MessageBox";/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconUpdateProduct(){// Initialize with a typeconstodata=newODataUpdateCL<IProducts,IProductKeys>(this,"Products");// set the http body to updateodata.setData({name:"New Product Name",description:"New Description"});try{// send the http request and get the result. Note: You have to specify the key values of the entity that will be updatedconstresult=awaitodata.update({ID:"3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"});MessageBox.information("The entity with the following ID: "+result.ID+" was updated.");}catch(error){// catch the errorMessageBox.error((errorasIError).message||"Request failed");}}}// The properties that will be updated should not be optionalinterfaceIProducts{ID?:string;name:string;description:string;brand?:string;price?:number;currency?:string;quantityInStock?:number;categoryID?:string;supplierID?:string;}interfaceIProductKeys{ID:string;}
sap.ui.define(["sap/ui/core/mvc/Controller","ui5/antares/odata/v2/ODataUpdateCL",// Import the class"sap/m/MessageBox"],/** * @param {typeof sap.ui.core.mvc.Controller} Controller */function(Controller,ODataUpdateCL,MessageBox){"use strict";returnController.extend("your.apps.namespace.YourController",{onInit:function(){},onUpdateProduct:asyncfunction(){// Initializeconstodata=newODataUpdateCL(this,"Products");// set the http body to updateodata.setData({name:"New Product Name",description:"New Description"});try{// send the http request and get the result. Note: You have to specify the key values of the entity that will be updatedconstresult=awaitodata.update({ID:"3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"});MessageBox.information("The entity with the following ID: "+result.ID+" was updated.");}catch(error){// catch the errorMessageBox.error(error.message||"Request failed");}}});});
URL Parameters
Prior to sending the UPDATE request with the update() 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 UPDATE request
Returns
Description
Record<string, string> | undefined
Returns the value that was set using setUrlParameters() method. Default value is undefined
importControllerfrom"sap/ui/core/mvc/Controller";importODataUpdateCLfrom"ui5/antares/odata/v2/ODataUpdateCL";// Import the class/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconUpdateProduct(){// Initialize with a typeconstodata=newODataUpdateCL<IProducts,IProductKeys>(this,"Products");// set the url parametersodata.setUrlParameters({"$expand":"toProductLocations"});}}// The properties that will be updated should not be optionalinterfaceIProducts{ID?:string;name:string;description:string;brand?:string;price?:number;currency?:string;quantityInStock?:number;categoryID?:string;supplierID?:string;}interfaceIProductKeys{ID:string;}
importControllerfrom"sap/ui/core/mvc/Controller";importODataUpdateCLfrom"ui5/antares/odata/v2/ODataUpdateCL";// Import the class/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconUpdateProduct(){// Initialize with a typeconstodata=newODataUpdateCL<IProducts,IProductKeys>(this,"Products");// deactivate the auto model refreshodata.setRefreshAfterChange(false);}}// The properties that will be updated should not be optionalinterfaceIProducts{ID?:string;name:string;description:string;brand?:string;price?:number;currency?:string;quantityInStock?:number;categoryID?:string;supplierID?:string;}interfaceIProductKeys{ID:string;}
1 2 3 4 5 6 7 8 910111213141516171819202122232425
sap.ui.define(["sap/ui/core/mvc/Controller","ui5/antares/odata/v2/ODataUpdateCL"// Import the class],/** * @param {typeof sap.ui.core.mvc.Controller} Controller */function(Controller,ODataUpdateCL){"use strict";returnController.extend("your.apps.namespace.YourController",{onInit:function(){},onUpdateProduct:asyncfunction(){// Initializeconstodata=newODataUpdateCL(this,"Products");// deactivate the auto model refreshodata.setRefreshAfterChange(false);}});});
Additional Response Info
The update() method returns the data of the successfully updated entity. However, you may require further information such as the status code and headers.
Once the update() function has been completed, the getResponse() method can be utilized to obtain further details.
importControllerfrom"sap/ui/core/mvc/Controller";importODataUpdateCLfrom"ui5/antares/odata/v2/ODataUpdateCL";// Import the classimport{IError}from"ui5/antares/types/common";// Import the error typeimportMessageBoxfrom"sap/m/MessageBox";/** * @namespace your.apps.namespace */exportdefaultclassYourControllerextendsController{publiconInit(){}publicasynconUpdateProduct(){// Initialize with a typeconstodata=newODataUpdateCL<IProducts,IProductKeys>(this,"Products");// set the http bodyodata.setData({name:"New Product Name",description:"New Description"});try{// send the http request and get the resultconstresult=awaitodata.update({ID:"3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"});MessageBox.information(result.ID+" was updated.");// get the additional response infoconstresponse=odata.getResponse();if(response){console.log("Status Code: "+response.statusCode);}}catch(error){// catch the errorMessageBox.error((errorasIError).message||"Request failed");}}}// The properties that will be updated should not be optionalinterfaceIProducts{ID?:string;name:string;description:string;brand?:string;price?:number;currency?:string;quantityInStock?:number;categoryID?:string;supplierID?:string;}interfaceIProductKeys{ID:string;}
sap.ui.define(["sap/ui/core/mvc/Controller","ui5/antares/odata/v2/ODataUpdateCL",// Import the class"sap/m/MessageBox"],/** * @param {typeof sap.ui.core.mvc.Controller} Controller */function(Controller,ODataUpdateCL,MessageBox){"use strict";returnController.extend("your.apps.namespace.YourController",{onInit:function(){},onUpdateProduct:asyncfunction(){// Initializeconstodata=newODataUpdateCL(this,"Products");// set the http bodyodata.setData({name:"New Product Name",description:"New Description"});try{// send the http request and get the resultconstresult=awaitodata.update({ID:"3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"});MessageBox.information(result.ID+" was updated.");// get the additional response infoconstresponse=odata.getResponse();if(response){console.log("Status Code: "+response.statusCode);}}catch(error){// catch the errorMessageBox.error(error.message||"Request failed");}}});});