How to Implement batch operations by using defer mode

Estimated read time 17 min read

What is Batch operation: 

Batch operations allow you to group multiple requests together and send them as a single HTTP request to the OData service.   

While performing Batch operations, we should redefine Changeset_begin, Changeset_end methods. 

What is the purpose of Changeset method: 

A Changeset is an unordered group of one or more insert, update, or delete operations. It allows bundling multiple modifications (such as creating, updating, or deleting entities) together within a single batch request. 

All changes within a ChangeSet must either be processed successfully or none of them. If there are two ChangeSet with operations, each ChangeSet is treated as a separate logic unit of work (LUW). When using $batch and ChangeSets, the commit and rollback are handled by the OData service framework. The framework ensures that either all operations within a ChangeSet succeed, or none of them take effect. 

What is the main purpose of this Blog (Scenario): 

Normally, if we want to do the Insert, update, delete operations, we can do it inside Single ChangeSet. If we are trying to do multiple insert or insert with update or delete operations inside one ChangeSet method, it will give an error. 

Here My requirement is I want to do multiple operations like insert or update or delete the records by using Single ChangetSet. So, for this requirement we should use Defer mode. 

Steps to Create Batch call by using defer mode. 

Step1: 

Go to SE11, create one database table. 

 

Step 2: Go to SEGW, create a project. 

Step 3: Create an entity type and add some fields. 

Step 4: Whenever we are doing Batch operation we must redefine 2 methods. 

Open ZCL_ZMR_ODATA_DEFER_MO_DPC_EXT. 

Redefine /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_BEGIN, 

/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END. 

Based upon our requirement we should redefine another method also i.e.,  

/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_PROCESS. 

Open /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_BEGIN. Here we are enabling the defer mode. 

Open /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END. 

Open /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_PROCESS. 

Here IT_CHANGESET_REQUEST is an importing parameter. 

 It holds the OPERATION_TYPE ‘CE’ CE means create entity. Here Iam doing the Post operation, so I used here ‘CE’. 

OPEARATION_NO means how many requests we gave in payload. 

 

Here  by using this step we are reading the data from payload, and we are storing the data in LS_DATA. 

wa_changeset_requestentry_provider->read_entry_data( 
         IMPORTING 
           es_data = ls_data 

How to find Opration_type? 

Go to /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_PROCESS. 

Double click on TYPE /IWBEP/IF_MGW_APPL_TYPES=>TY_T_CHANGESET_REQUEST. 

Click on Direct type entry (Arrow mark). 

Double click on /iwbep/mgw_operation_type. 

Double click on domain name. 

In value range, we can see the opearation_type. 

 

Step 5: Payload data.  

–batch 
Content-Type: multipart/mixed; boundary=changeset_01 
  

 
–changeset_01 
Content-Type: application/http 
Content-Transfer-Encoding: binary 
 
 
POST BatchSet HTTP/1.1 
Content-Type: application/xml 
Content-Length: 1000 
 
 
<?xml version=“1.0” encoding=“utf-8”?> 
<entry xml:base=http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/xmlns=http://www.w3.org/2005/Atomxmlns:m=http://schemas.microsoft.com/ado/2007/08/dataservices/metadataxmlns:d=http://schemas.microsoft.com/ado/2007/08/dataservices> 
<id>http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/BatchSet(‘1’)</id> 
<title type=“text”>BatchSet(‘7’)</title> 
<updated>2024-07-03T16:22:21Z</updated> 
<category term=“ZMR_ODATA_DEFER_MODE_SRV.Batch” scheme=http://schemas.microsoft.com/ado/2007/08/dataservices/scheme/> 
<link href=“BatchSet(‘7’)” rel=“self” title=“Batch”/> 
<content type=“application/xml”> 
<m:properties> 
<d:EmpId>7</d:EmpId> 
<d:Empname>John</d:Empname> 
<d:Empsal>20000</d:Empsal> 
<d:Empcountry>INDIA</d:Empcountry> 
<d:Empcity>BANGLORE</d:Empcity> 
<d:Empphno>8768765876</d:Empphno> 
<d:Currency>EUR</d:Currency> 
</m:properties> 
</content> 
</entry> 
 
 
–changeset_01 
Content-Type: application/http 
Content-Transfer-Encoding: binary 
 
 
POST BatchSet HTTP/1.1 
Content-Type: application/xml 
Content-Length: 1000 
 
 
<?xml version=“1.0” encoding=“utf-8”?> 
<entry xml:base=http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/xmlns=http://www.w3.org/2005/Atomxmlns:m=http://schemas.microsoft.com/ado/2007/08/dataservices/metadataxmlns:d=http://schemas.microsoft.com/ado/2007/08/dataservices> 
<id>http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/BatchSet(‘1’)</id> 
<title type=“text”>BatchSet(‘7’)</title> 
<updated>2024-07-03T16:22:21Z</updated> 
<category term=“ZMR_ODATA_DEFER_MODE_SRV.Batch” scheme=http://schemas.microsoft.com/ado/2007/08/dataservices/scheme/> 
<link href=“BatchSet(‘8’)” rel=“self” title=“Batch”/> 
<content type=“application/xml”> 
<m:properties> 
<d:EmpId>8</d:EmpId> 
<d:Empname>MOHAN</d:Empname> 
<d:Empsal>16950</d:Empsal> 
<d:Empcountry>INDIA</d:Empcountry> 
<d:Empcity>CHITTOOR</d:Empcity> 
<d:Empphno>9865432143</d:Empphno> 
<d:Currency>EUR</d:Currency> 
</m:properties> 
</content> 
</entry> 
 
–changeset_01– 
 
–batch– 

Output:  

Here in the database table, I added the data by using a single ChangeSet. 

We did multiple post operations in single changeset. Now my requirement is I want to do multiple post operations and Put operation in Single changeset. 

I have 2 records in my database table. Now Iam trying to insert 2 new records and Iam updating 7th Empid record, name John to Ram. 

Here we are doing 2 post and 1 put. So, here we can see Operation_No is 1,2,3. 

Payload: 

–batch 
Content-Type: multipart/mixed; boundary=changeset_01 
 
–changeset_01 
Content-Type: application/http 
Content-Transfer-Encoding: binary 
 
 
POST BatchSet HTTP/1.1 
Content-Type: application/xml 
Content-Length: 1000 
 
 
<?xml version=“1.0” encoding=“utf-8”?> 
<entry xml:base=http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/xmlns=http://www.w3.org/2005/Atomxmlns:m=http://schemas.microsoft.com/ado/2007/08/dataservices/metadataxmlns:d=http://schemas.microsoft.com/ado/2007/08/dataservices> 
<id>http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/BatchSet(‘1’)</id> 
<title type=“text”>BatchSet(‘1’)</title> 
<updated>2024-07-03T16:22:21Z</updated> 
<category term=“ZMR_ODATA_DEFER_MODE_SRV.Batch” scheme=http://schemas.microsoft.com/ado/2007/08/dataservices/scheme/> 
<link href=“BatchSet(‘1’)” rel=“self” title=“Batch”/> 
<content type=“application/xml”> 
<m:properties> 
<d:EmpId>1</d:EmpId> 
<d:Empname>Sri</d:Empname> 
<d:Empsal>10000</d:Empsal> 
<d:Empcountry>INDIA</d:Empcountry> 
<d:Empcity>pune</d:Empcity> 
<d:Empphno>6768765876</d:Empphno> 
<d:Currency>EUR</d:Currency> 
</m:properties> 
</content> 
</entry> 
 
 
–changeset_01 
Content-Type: application/http 
Content-Transfer-Encoding: binary 
 
 
POST BatchSet HTTP/1.1 
Content-Type: application/xml 
Content-Length: 1000 
 
 
<?xml version=“1.0” encoding=“utf-8”?> 
<entry xml:base=http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/xmlns=http://www.w3.org/2005/Atomxmlns:m=http://schemas.microsoft.com/ado/2007/08/dataservices/metadataxmlns:d=http://schemas.microsoft.com/ado/2007/08/dataservices> 
<id>http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/BatchSet(‘1’)</id> 
<title type=“text”>BatchSet(‘2’)</title> 
<updated>2024-07-03T16:22:21Z</updated> 
<category term=“ZMR_ODATA_DEFER_MODE_SRV.Batch” scheme=http://schemas.microsoft.com/ado/2007/08/dataservices/scheme/> 
<link href=“BatchSet(‘2’)” rel=“self” title=“Batch”/> 
<content type=“application/xml”> 
<m:properties> 
<d:EmpId>2</d:EmpId> 
<d:Empname>Bharath</d:Empname> 
<d:Empsal>30000</d:Empsal> 
<d:Empcountry>INDIA</d:Empcountry> 
<d:Empcity>BANGLORE</d:Empcity> 
<d:Empphno>7768765876</d:Empphno> 
<d:Currency>EUR</d:Currency> 
</m:properties> 
</content> 
</entry> 
 
 
–changeset_01 
Content-Type: application/http 
Content-Transfer-Encoding: binary 
 
 
PUT BatchSet(‘7’) HTTP/1.1 
Content-Type: application/xml 
Content-Length: 130 
 
 
<?xml version=“1.0” encoding=“utf-8”?> 
<entry xml:base=http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/xmlns=http://www.w3.org/2005/Atomxmlns:m=http://schemas.microsoft.com/ado/2007/08/dataservices/metadataxmlns:d=http://schemas.microsoft.com/ado/2007/08/dataservices> 
<id>http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/BatchSet(‘1’)</id> 
<title type=“text”>BatchSet(‘7’)</title> 
<updated>2024-07-03T16:22:21Z</updated> 
<category term=“ZMR_ODATA_DEFER_MODE_SRV.Batch” scheme=http://schemas.microsoft.com/ado/2007/08/dataservices/scheme/> 
<link href=“BatchSet(‘7’)” rel=“self” title=“Batch”/> 
<content type=“application/xml”> 
<m:properties> 
<d:EmpId>7</d:EmpId> 
<d:Empname>Ram</d:Empname> 
<d:Empsal>20000</d:Empsal> 
<d:Empcountry>INDIA</d:Empcountry> 
<d:Empcity>BANGLORE</d:Empcity> 
<d:Empphno>8768765876</d:Empphno> 
<d:Currency>EUR</d:Currency> 
</m:properties> 
</content> 
</entry> 
 
 
–changeset_01– 
–batch–  

 

Output: 

 

  

 

 

 

 

 

 

  

 

 

 

 

 

​ What is Batch operation: Batch operations allow you to group multiple requests together and send them as a single HTTP request to the OData service.   While performing Batch operations, we should redefine Changeset_begin, Changeset_end methods. What is the purpose of Changeset method: A Changeset is an unordered group of one or more insert, update, or delete operations. It allows bundling multiple modifications (such as creating, updating, or deleting entities) together within a single batch request. All changes within a ChangeSet must either be processed successfully or none of them. If there are two ChangeSet with operations, each ChangeSet is treated as a separate logic unit of work (LUW). When using $batch and ChangeSets, the commit and rollback are handled by the OData service framework. The framework ensures that either all operations within a ChangeSet succeed, or none of them take effect. What is the main purpose of this Blog (Scenario): Normally, if we want to do the Insert, update, delete operations, we can do it inside Single ChangeSet. If we are trying to do multiple insert or insert with update or delete operations inside one ChangeSet method, it will give an error. Here My requirement is I want to do multiple operations like insert or update or delete the records by using Single ChangetSet. So, for this requirement we should use Defer mode. Steps to Create Batch call by using defer mode. Step1: Go to SE11, create one database table.  Step 2: Go to SEGW, create a project. Step 3: Create an entity type and add some fields. Step 4: Whenever we are doing Batch operation we must redefine 2 methods. Open ZCL_ZMR_ODATA_DEFER_MO_DPC_EXT. Redefine /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_BEGIN, /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END. Based upon our requirement we should redefine another method also i.e.,  /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_PROCESS. Open /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_BEGIN. Here we are enabling the defer mode. Open /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END. Open /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_PROCESS. Here IT_CHANGESET_REQUEST is an importing parameter.  It holds the OPERATION_TYPE ‘CE’ CE means create entity. Here Iam doing the Post operation, so I used here ‘CE’. OPEARATION_NO means how many requests we gave in payload.  Here  by using this step we are reading the data from payload, and we are storing the data in LS_DATA. wa_changeset_request-entry_provider->read_entry_data(          IMPORTING            es_data = ls_data How to find Opration_type? Go to /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_PROCESS. Double click on TYPE /IWBEP/IF_MGW_APPL_TYPES=>TY_T_CHANGESET_REQUEST. Click on Direct type entry (Arrow mark). Double click on /iwbep/mgw_operation_type. Double click on domain name. In value range, we can see the opearation_type.  Step 5: Payload data.  –batch Content-Type: multipart/mixed; boundary=changeset_01    –changeset_01 Content-Type: application/http Content-Transfer-Encoding: binary   POST BatchSet HTTP/1.1 Content-Type: application/xml Content-Length: 1000   <?xml version=”1.0″ encoding=”utf-8″?> <entry xml:base=”http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/” xmlns=”http://www.w3.org/2005/Atom” xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices”> <id>http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/BatchSet(‘1’)</id> <title type=”text”>BatchSet(‘7’)</title> <updated>2024-07-03T16:22:21Z</updated> <category term=”ZMR_ODATA_DEFER_MODE_SRV.Batch” scheme=”http://schemas.microsoft.com/ado/2007/08/dataservices/scheme”/> <link href=”BatchSet(‘7’)” rel=”self” title=”Batch”/> <content type=”application/xml”> <m:properties> <d:EmpId>7</d:EmpId> <d:Empname>John</d:Empname> <d:Empsal>20000</d:Empsal> <d:Empcountry>INDIA</d:Empcountry> <d:Empcity>BANGLORE</d:Empcity> <d:Empphno>8768765876</d:Empphno> <d:Currency>EUR</d:Currency> </m:properties> </content> </entry>   –changeset_01 Content-Type: application/http Content-Transfer-Encoding: binary   POST BatchSet HTTP/1.1 Content-Type: application/xml Content-Length: 1000   <?xml version=”1.0″ encoding=”utf-8″?> <entry xml:base=”http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/” xmlns=”http://www.w3.org/2005/Atom” xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices”> <id>http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/BatchSet(‘1’)</id> <title type=”text”>BatchSet(‘7’)</title> <updated>2024-07-03T16:22:21Z</updated> <category term=”ZMR_ODATA_DEFER_MODE_SRV.Batch” scheme=”http://schemas.microsoft.com/ado/2007/08/dataservices/scheme”/> <link href=”BatchSet(‘8’)” rel=”self” title=”Batch”/> <content type=”application/xml”> <m:properties> <d:EmpId>8</d:EmpId> <d:Empname>MOHAN</d:Empname> <d:Empsal>16950</d:Empsal> <d:Empcountry>INDIA</d:Empcountry> <d:Empcity>CHITTOOR</d:Empcity> <d:Empphno>9865432143</d:Empphno> <d:Currency>EUR</d:Currency> </m:properties> </content> </entry>  –changeset_01–  –batch– Output:  Here in the database table, I added the data by using a single ChangeSet. We did multiple post operations in single changeset. Now my requirement is I want to do multiple post operations and Put operation in Single changeset. I have 2 records in my database table. Now Iam trying to insert 2 new records and Iam updating 7th Empid record, name John to Ram. Here we are doing 2 post and 1 put. So, here we can see Operation_No is 1,2,3. Payload: –batch Content-Type: multipart/mixed; boundary=changeset_01  –changeset_01 Content-Type: application/http Content-Transfer-Encoding: binary   POST BatchSet HTTP/1.1 Content-Type: application/xml Content-Length: 1000   <?xml version=”1.0″ encoding=”utf-8″?> <entry xml:base=”http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/” xmlns=”http://www.w3.org/2005/Atom” xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices”> <id>http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/BatchSet(‘1’)</id> <title type=”text”>BatchSet(‘1’)</title> <updated>2024-07-03T16:22:21Z</updated> <category term=”ZMR_ODATA_DEFER_MODE_SRV.Batch” scheme=”http://schemas.microsoft.com/ado/2007/08/dataservices/scheme”/> <link href=”BatchSet(‘1’)” rel=”self” title=”Batch”/> <content type=”application/xml”> <m:properties> <d:EmpId>1</d:EmpId> <d:Empname>Sri</d:Empname> <d:Empsal>10000</d:Empsal> <d:Empcountry>INDIA</d:Empcountry> <d:Empcity>pune</d:Empcity> <d:Empphno>6768765876</d:Empphno> <d:Currency>EUR</d:Currency> </m:properties> </content> </entry>   –changeset_01 Content-Type: application/http Content-Transfer-Encoding: binary   POST BatchSet HTTP/1.1 Content-Type: application/xml Content-Length: 1000   <?xml version=”1.0″ encoding=”utf-8″?> <entry xml:base=”http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/” xmlns=”http://www.w3.org/2005/Atom” xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices”> <id>http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/BatchSet(‘1’)</id> <title type=”text”>BatchSet(‘2’)</title> <updated>2024-07-03T16:22:21Z</updated> <category term=”ZMR_ODATA_DEFER_MODE_SRV.Batch” scheme=”http://schemas.microsoft.com/ado/2007/08/dataservices/scheme”/> <link href=”BatchSet(‘2’)” rel=”self” title=”Batch”/> <content type=”application/xml”> <m:properties> <d:EmpId>2</d:EmpId> <d:Empname>Bharath</d:Empname> <d:Empsal>30000</d:Empsal> <d:Empcountry>INDIA</d:Empcountry> <d:Empcity>BANGLORE</d:Empcity> <d:Empphno>7768765876</d:Empphno> <d:Currency>EUR</d:Currency> </m:properties> </content> </entry>   –changeset_01 Content-Type: application/http Content-Transfer-Encoding: binary   PUT BatchSet(‘7’) HTTP/1.1 Content-Type: application/xml Content-Length: 130   <?xml version=”1.0″ encoding=”utf-8″?> <entry xml:base=”http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/” xmlns=”http://www.w3.org/2005/Atom” xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices”> <id>http://S4HANA.IDES.COM:9222/sap/opu/odata/SAP/ZMR_ODATA_DEFER_MODE_SRV/BatchSet(‘1’)</id> <title type=”text”>BatchSet(‘7’)</title> <updated>2024-07-03T16:22:21Z</updated> <category term=”ZMR_ODATA_DEFER_MODE_SRV.Batch” scheme=”http://schemas.microsoft.com/ado/2007/08/dataservices/scheme”/> <link href=”BatchSet(‘7’)” rel=”self” title=”Batch”/> <content type=”application/xml”> <m:properties> <d:EmpId>7</d:EmpId> <d:Empname>Ram</d:Empname> <d:Empsal>20000</d:Empsal> <d:Empcountry>INDIA</d:Empcountry> <d:Empcity>BANGLORE</d:Empcity> <d:Empphno>8768765876</d:Empphno> <d:Currency>EUR</d:Currency> </m:properties> </content> </entry>   –changeset_01– –batch–   Output:                   Read More Application Development Blog Posts articles 

#SAP

You May Also Like

More From Author

+ There are no comments

Add yours