0% found this document useful (0 votes)
8 views

Easy way to write data via a deep entity in OData - SAP Community

This blog post provides a simplified guide for beginners on how to write data via a deep entity in OData, specifically focusing on the SAP Gateway Service Builder. It outlines the necessary steps including creating a project, importing data structures, and defining methods for data insertion. The author emphasizes that while this method demonstrates how to insert data, it is not the recommended approach for updating database tables, and users should adapt the code to their specific needs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Easy way to write data via a deep entity in OData - SAP Community

This blog post provides a simplified guide for beginners on how to write data via a deep entity in OData, specifically focusing on the SAP Gateway Service Builder. It outlines the necessary steps including creating a project, importing data structures, and defining methods for data insertion. The author emphasizes that while this method demonstrates how to insert data, it is not the recommended approach for updating database tables, and users should adapt the code to their specific needs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

m
m
Products and Technology Groups Partners Topics Events What's New Get Start
u
ni
t
y
SAP Community  Products and Technology  Technology  Technology Blogs by Members
 Easy way to write data via a deep entity in OData

Technology Blogs by Members


Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP
products, technology, and events. Get in the mix!

Blog  What are you looking for today?

Easy way to write data via a deep entity in OData

kallolathome
Active Participant

‎2022 Feb 23 8:43 AM

 19 Kudos  86,876

SAP Managed Tags: ABAP Connectivity, ABAP Extensibility, OData,


ABAP RESTful Application Programming Model, NW ABAP Gateway (OData)

Introduction
I have seen many tutorials based on OData but I really found them complex for
beginners.

So, I am writing this blog post for quick easy reference. This is the 2nd blog post in the
series.

For the first post please check the Easy way to read data via a deep entity in OData.

Solution
Please follow the steps:

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 1/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

1. Tables involved: VBAK(Sales Document: Header Data), VBAP(Sales Document:


Item Data).

2. Create a Project in SEGW(SAP Gateway Service Builder)

3. Import the DDIC Structure: VBAK(Sales Document: Header Data)

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 2/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

4. Import the DDIC Structure: VBAP(Sales Document: Item Data)

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 3/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

5. Create Association

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 4/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

6. Go to Runtime Artifacts node, open the ZCL_ZGW_PRACTICE006_MPC_EXT class


in ABAP Workbench(Right-Click: Go to ABAP Workbench) & click on the Types
tab.

7. Click on the Change(Ctrl+F1) button for editing.

8. Click on the Direct Type Entry button. Then, create the deep structure &
activate.

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 5/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

1 *--------------------------------------------------------
2 ------------*
3 * Deep Structure
4 *--------------------------------------------------------
5 ------------*
6 TYPES: BEGIN OF ty_deep_entity,
vbeln TYPE vbeln_va, "Sales
7 Document
8 erdat TYPE erdat, "Date on
which the record was created
9 erzet TYPE erzet, "Entry time
10 ernam TYPE ernam, "Name of
Person who Created the Object
11 vkorg TYPE vkorg, "Sales
Organization
12 * Navigation property name should be used otherwise empty
13 records will be shown
headertoitem TYPE TABLE OF
ts_sales_item_data WITH DEFAULT KEY,
END OF ty_deep_entity.
*--------------------------------------------------------
------------*​

N.B:

The navigation property name should be used in case of a deep entity like shown in
the image above otherwise, empty records will be returned.

Do not regenerate the service before taking the backup as it will delete all the
custom structures.

Just redefine the basic methods: *GET_ENTITY & *GET_ENTITYSET of the entities
for easy troubleshooting. No need to write any code within the methods. The
$expand keyword will call only the GET_EXPANDED_ENTITYSET method.

9. Redefine the DEFINE method of the ZCL_ZGW_PRACTICE006_MPC_EXT and


enter the below code.

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 6/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

1 DATA lo_entity_type TYPE REF TO


2 /iwbep/if_mgw_odata_entity_typ.
3 super->define( ).
4 * Header Entity Name
lo_entity_type = model->get_entity_type(
5 iv_entity_name = 'Sales_Header_Data' ).
6 * MPC_EXT Deep Structure Name
lo_entity_type->bind_structure( iv_structure_name =
'ZCL_ZGW_PRACTICE006_MPC_EXT=>TY_DEEP_ENTITY​

10. Go to the ZCL_ZGW_PRACTICE006_DPC_EXT class & redefine the method:


CREATE_DEEP_ENTITY.

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 7/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

METHOD
/iwbep/if_mgw_appl_srv_runtime~create_deep_entity.

DATA : ls_deep_entity TYPE


zcl_zgw_practice006_mpc_ext=>ty_deep_entity,
lt_item TYPE TABLE OF
zcl_zgw_practice006_mpc_ext=>ts_sales_item_data,
ls_header TYPE
zcl_zgw_practice006_mpc_ext=>ts_sales_header_data.

* Methods/FMs should be used in case of direct database


table update
* Reading the entity data through the parameter:
io_data_provider
TRY.
CALL METHOD io_data_provider->read_entry_data
IMPORTING
es_data = ls_deep_entity.
IF ls_deep_entity IS NOT INITIAL.
ls_header = CORRESPONDING #( ls_deep_entity ).
ls_header-mandt = sy-mandt.
MODIFY vbak FROM ls_header.
IF sy-subrc = 0.
lt_item[] = CORRESPONDING #( ls_deep_entity-
headertoitem[] ).
DO lines( lt_item[] ) TIMES.
lt_item[ sy-index ]-mandt = sy-mandt.
ENDDO.
TRY.
INSERT vbap FROM TABLE lt_item ACCEPTING
DUPLICATE KEYS.
IF sy-subrc = 4. "To
overcome the dump
CALL METHOD me->copy_data_to_ref
"Populating the ER_DEEP_ENTITY
EXPORTING
is_data = ls_deep_entity
CHANGING
cr_data = er_deep_entity.
ENDIF.
https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 8/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

37 CATCH cx_root.
38 "Error during insert
39 ENDTRY.
40 ENDIF.
ENDIF.
CATCH /iwbep/cx_mgw_tech_exception.
"Do Nothing[
ENDTRY.

ENDMETHOD.​

N.B: Here, the OPEN SQL statement is used to insert/update entries in the
database tables. It's better to use FMs/Methods for the same.

11. Input: Go to the HTTP Request section then enter your JSON/XML file & select the
radio button POST & execute.

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 9/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

1 {
2 "Vbeln" : "0000032183",
3 "Erdat" : "\/Date(1481760000000)\/",
4 "Erzet" : "TEST0000001",
5 "Ernam" : "Kallol",
6 "Vkorg" : "1710",
7 "HeaderToItem" : [
8 {
9 "Vbeln" : "0000032183",
10 "Posnr" : "000010",
11 "Matnr" : "MZ-FG-M550",
12 "Arktx" : "M550 BIKE",
13 "Posar" : ""
14 },
15 {
16 "Vbeln" : "0000032183",
17 "Posnr" : "000020",
18 "Matnr" : "LMO-PRD-M121",
19 "Arktx" : "1ortable DVD Player PDP-121",
20 "Posar" : ""
21 },
22 {
23 "Vbeln" : "0000032183",
24 "Posnr" : "000030",
25 "Matnr" : "MZ-FG-C950",
26 "Arktx" : "C950 BIKE",
27 "Posar" : ""
28 }
29 ]
30 }

N.B: Remove the ?$expand=HeaderToItem&$format=json from the URI otherwise


it will throw error while inserting the data.

12. Output

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 10/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

That's it.

N.B: This is not a proper way to update the database tables. Please use FMs/Methods for
the same otherwise there will be discrepancies. This blog post is only for showing how
the insert happens via a deep entity. You can manipulate the code according to your
requirements.
If I have missed something, please feel free to add it in the comment section so that, this
post can be useful to others.

Tags:

deepentity deepinsert gateway OData restapi SAPCommnity

8 Comments

swamy1
Explorer

‎2022 Jun 03 4:44 AM

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 11/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

 0 Kudos

HI kallolathome,

we are creatingl create deep entity with ref to your post we are getting: 'csrf token
validation failed' message how to overcome this.

we did not Implement get_expanded_Deep_entity as it is not needed, please let me


know any solution to call creat Deep entity for testing.

bhaskar_chakraborty
Discoverer

‎2022 Nov 09 9:10 AM

 0 Kudos

Just came accross your question today, probably bit too late for you.

But to use Create Deep Entity you first need to call the Get Method of the root entity and
pass the below parameter in Request Header.

x-csrf-token = fetch

You will get the value of xcrsf token in Response header. Then call the create deep entity
passing the xcsrf token in the request header which you earlier received. This should
solve your problem.

x-csrf-token = fnZX7zSr3JBG4oAB1uYrUg== => the value is just for egxample. This will
mostprobably be valid for 30 min depending on your system config.

Let me know if its useful for you.

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 12/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

eduardo_luiz_nunes_ribeir
Explorer

‎2022 Dec 28 12:23 AM

 0 Kudos

A few years ago, if I updated the sales order tables like that, I would get fired.

The world is changing. Indeed.

Hey! I think I'll try to earn money playing video games and chatting!

Thanks for teaching, friend!

RohitSingh
Explorer

‎2023 Jan 14 7:21 AM

 0 Kudos

Is there any way to bind multiple Deep Entity structures on different Entity within same
SEGW project MPC_EXT-DEFINE method?

Thanks in advance.

malirahul
Member

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 13/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community


‎2023 Apr 18 2:01 PM

 0 Kudos

I got an error when I processed the same steps as mentioned by you. Please help me to
solve the below attach screenshot error.

kyo_choi2
Participant

‎2023 May 15 1:43 PM

 0 Kudos

Excellent blog!!!

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 14/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

kapilmahire14
Explorer

‎2023 May 24 12:58 PM

 0 Kudos

I followed all the above steps but thing is that when i Redefine the DEFINE method of
the ZCL_ZGW_PRACTICE006_MPC_EXT then my entityset data is not visible
@Gateway client.

Please suggest

DaddyBear
Discoverer

‎2024 May 29 3:50 PM

 0 Kudos

Hi kapilmahire14.

I met the same error. Impossible to generate the service in /IWFND/MAINT_SERVICE


eventhough class generation was possible.

So I built a DDIC structure with identifical properties, than removed DEFINE methods but
also custom types in MPC_EXT.

Goods news, service generation worked well !

Bad news, impossible to submit the nested payload. It causes errors. Basically, only data
compatible with the URI entityset are accepted. The rest causes parsing issues.

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 15/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

I am seriously wondering if complex REST webservice from OData and ABAP is possible.
If anyone reads this and his a clue to propose, it might make my day for years !!!

 You must be a registered user to add a comment. If you've already registered,


sign in. Otherwise, register and sign in.

Comment

Labels In This Area


"automatische backups" 1 "regelmäßige sicherung" 1 "SAP BW" 1

"SAP VARIANT CONFIGURAITION 2 "SAPDatasphere" 1

"TypeScript" "Development" "FeedBack" 1 3-TIER Extensibility 1

505 Technology Updates​53 1 @RetroDate_HireDateCorrection 1

@sapilm @archiving @sapiq 1

A Comprehensive Guide to Using OLE Objects in SAP ABAP 1 aATP 1 ABAP 34

ABAP 7.4 2 ABAP API 1 ABAP CDS VIEW 2 ABAP CDS Views 10

ABAP CDS Views - BW Extraction 3 ABAP CDS Views - CDC (Change Data Capture) 2

ABAP class 2 ABAP Cloud 4 ABAP Cloud Developer Trial 1

ABAP DDIC CDS view 1 ABAP Development 8 ABAP Environment & RAP 2

ABAP Extensibility 2 ABAP in Eclipse 3 ABAP New Syntax 1 ABAP OOABAP 1

ABAP Platform Trial 2 ABAP Programming 5 ABAP Push Channels 1 ABAP RAP 2

ABAP RAP custom action 1 ABAP RAP(RESTful Application Programming) 4

ABAP RESTFul API 1 ABAP RESTful Application Programming Model 2

ABAP String functions 1 abap technical 1 ABAP test cokpit 1 abap to xml 1

abapGit 1 absl 2 Access data from datasphere to ADF Azure Data Factory 2

access data from SAP Datasphere directly from Snowflake 1

Access data from SAP datasphere to Qliksense 1 Accessibility 1

Accessibility in SAPUI5 1 Accrual 1 Acquire SAC Knowledge 2 action 1

actions 1 adapter 2 adapter modules 1 ADDING LEAN SERVICES 2 Addon 2

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 16/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

Adobe Document Services 1 ADS 1 ADS Config 1 ADS with ABAP 1

ADS with Java 1 ADT 3 Advance Shipping and Receiving 1

Advanced Event Mesh 4 Advanced formula 1 Advanced SAP Techniques 1

Advanced Scripting in SAC 1 Advanced Workflow 1 AEM 1 AEM Event Portal 1

agile 2 agile development 1 agile teams 1 ai 15 AI Agents 1 AI Essentials 1

ai generated content 1 ai in transportation 1 AI Integration 1 AI Launchpad 3

AI Optimizer 1 AI Projects 2 AI TOOLS 1 aichallenges 1 aicompliance 1

aicreators 1 AIF Logs 1 AIML 11 aimodels 1 aiupdate 1 AL11 1

Alert in Sap analytical cloud 1 alm 1 ALM Nuggets 2 ALV 1 Amazon S3 1

AMDP 3 Analytic Models 1 Analytical Dataset 1 Analytical Model 1

Related Content
Use SAP CAP SAPUI5 Smart Filter to Show Value Help that are not Associated
Entitites 
in Technology Blogs by Members yesterday

Create CDS Entity in system 2 based on table in system 1 


in Technology Q&A Sunday

Problem with request body mapper and how to write the code 
in Technology Q&A Friday

SAP CAP - How to create a custom endpoint? 


in Technology Q&A Thursday

SAP CAP: Is it ok to use req._query[$filter] in a custom handler? 


in Technology Q&A Thursday

Popular Blog Posts

https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 17/18
10/8/24, 3:07 PM Easy way to write data via a deep entity in OData - SAP Community

SAP PI for Beginners

former_member200339
Participant

 714508  153  386

Follow

Privacy Terms of Use

Copyright Legal Disclosure

ABAP 7.40 Quick Reference


Trademark Support
jeffrey_towell2
Explorer
Cookie Preferences
 1163985  75  335
https://community.sap.com/t5/technology-blogs-by-members/easy-way-to-write-data-via-a-deep-entity-in-odata/ba-p/13539510 18/18

You might also like