Application Development and Automation Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Mohanreddy
Explorer
5,327
  • SAP Managed Tags:

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. 

Mohanreddy_0-1720435949801.png

 

Step 2: Go to SEGW, create a project. 

Mohanreddy_1-1720435949803.png

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

Mohanreddy_2-1720435949804.png

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

Open ZCL_ZMR_ODATA_DEFER_MO_DPC_EXT. 

Mohanreddy_3-1720435949806.png

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. 

Mohanreddy_4-1720435949808.png

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

Mohanreddy_5-1720435949809.png

Open /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END. 

Mohanreddy_6-1720435949810.png

Open /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_PROCESS. 

Mohanreddy_7-1720435949811.png

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. 

Mohanreddy_8-1720435949812.png

 

Mohanreddy_9-1720435949814.png

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. 

Mohanreddy_10-1720435949815.png

Click on Direct type entry (Arrow mark). 

Mohanreddy_11-1720435949816.png

Double click on /iwbep/mgw_operation_type. 

Mohanreddy_12-1720435949816.png

Double click on domain name. 

Mohanreddy_13-1720435949817.png

In value range, we can see the opearation_type. 

Mohanreddy_14-1720435949818.png

Mohanreddy_15-1720435949819.png

Mohanreddy_16-1720435949819.png

Mohanreddy_17-1720435949819.png

 

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. 

Mohanreddy_18-1720435949820.png

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. 

Mohanreddy_19-1720435949821.png

Mohanreddy_20-1720435949822.png

Mohanreddy_21-1720435949823.png

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

Mohanreddy_22-1720435949825.png

Mohanreddy_23-1720435949827.png

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: 

Mohanreddy_24-1720435949828.png

 

  

 

 

 

 

 

 

  

 

 

 

 

3 Comments
jyothi7
Explorer

Nice Blog well explanation..

Sandra_Rossi
Active Contributor
0 Kudos

For the developers who want to code with constant names instead of literals:

 

CASE ls_changeset_request-operation_type.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-create_deep_entity.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-create_entity.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-create_stream.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-delete_entity.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-delete_stream.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-execute_action.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-expand_entity.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-expand_entityset.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-get_entity.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-get_entityset.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-get_entityset_delta.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-get_stream.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-patch_entity.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-update_entity.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-update_stream.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-changeset_begin.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-changeset_end.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-changeset_process.
  WHEN /iwbep/if_mgw_appl_types=>gcs_operation_type-get_is_cond_impl.
ENDCASE.

 

 

AmveshKumar
Explorer
0 Kudos

Really nice vlog Mohan it help allots, 

Thankyou.


Labels in this area