cancel
Showing results for 
Search instead for 
Did you mean: 

How to Post Multiple Rows at a time into Database table using Odata ...

former_member192971
Participant
0 Kudos

Hello Experts ,

I want to Post Multiple Records into Database table using Odata Post Method for a Remote Enabled Function Module. For that, using SE80 I created a data model and Mapped fields for table using Function Module. Then Created a consumption Model and Activated the service . Then Calling the service from REST Client and tried to post using XML Content as shown below.

In the Below XML Content I tried to Post 2 Records into DB Table, But Showing error as"Error while parsing an XML stream".

Could you please suggest How to Pass Multiple Records At a time using Odata Create/Post Method , like we post Multiple Records at the time of Function Module Execution by clicking NewLine/DoubleLine in Tables Tab.


XML Content :


<?xml version="1.0" encoding="utf-8"?>

<entry xml:base="http://Server:port/sap/opu/odata/sap/Z_RFC_POST/z_rfc_postCollection/"

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://Server:port/sap/opu/odata/sap/Z_RFC_POST/z_rfc_postCollection/</id>

<title type="text">GoodsReceipt Status</title>

    <category term="/Z_RFC_POST./z_rfc_post"

scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>

    <link href="z_rfc_postCollection" rel="self" title="/Z_RFC_POST"/>

    <content type="application/atom+xml">

    <m:properties>

    <d:empid>0000000050</d:empid>

    <d:ename>Ram</d:ename>

    <d:city>Chennai</d:city>

    <d:empid>0000000090</d:empid>

    <d:ename>Kumar</d:ename>

    <d:city>Bangalore</d:city>

    </m:properties>

    </content>

    </entry>

Appreciate your help.

Thanks,

Uday.

yoppy_santoso
Participant
0 Kudos

Are u done with this case?
Because I have the same question.

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Uday,

what i suggest you to do is:

Try to create only one record first using the same URL and request body:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"

xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"

xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">

<atom:content type="application/xml">

<m:properties>

<d:empid>0000000101</d:empid>

<d:ename>AAAA</d:ename>

<d:city>Mangalore</d:city>

</m:properties>

</atom:content>

</atom:entry>

If it works for you then execute the batch command that was specified by Arun.

please notice that when you execute batch command you must consider white spaces so the post body should not contain unnecessary new lines or spaces because it's being parsed by gateway.

Please let us know if you managed to do it.

Good luck,

ran.

arunchembra1
Participant
0 Kudos

Hi Uday,

You have redefine /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_BEGIN and

/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END methods for batch update. Please find the below links.

http://help.sap.com/saphelp_gateway20sp06/helpdata/en/90/dc8363306c47d3b2fca1398f5de94b/content.htm

http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/40546820-3ea7-2f10-dfab-be373c0da...

Your Create URL will look like this and you have to paste multiple records in your request body. Please let me know if you have any question.

http://Server:port/sap/opu/odata/sap/Z_RFC_POST/z_rfc_postCollection/$batch

<?xml version="1.0" encoding="utf-8"?>

<entry xml:base="http://Server:port/sap/opu/odata/sap/Z_RFC_POST/z_rfc_postCollection/"

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://Server:port/sap/opu/odata/sap/Z_RFC_POST/z_rfc_postCollection/</id>

<title type="text">GoodsReceipt Status</title>

    <category term="/Z_RFC_POST./z_rfc_post"

scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>

    <link href="z_rfc_postCollection" rel="self" title="/Z_RFC_POST"/>

    <content type="application/atom+xml">

    <m:properties>

    <d:empid>0000000050</d:empid>

    <d:ename>Ram</d:ename>

    <d:city>Chennai</d:city>

    <d:empid>0000000090</d:empid>

    <d:ename>Kumar</d:ename>

    <d:city>Bangalore</d:city>

    </m:properties>

    </content>

    </entry>

<?xml version="1.0" encoding="utf-8"?>

<entry xml:base="http://Server:port/sap/opu/odata/sap/Z_RFC_POST/z_rfc_postCollection/"

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://Server:port/sap/opu/odata/sap/Z_RFC_POST/z_rfc_postCollection/</id>

<title type="text">GoodsReceipt Status</title>

    <category term="/Z_RFC_POST./z_rfc_post"

scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>

    <link href="z_rfc_postCollection" rel="self" title="/Z_RFC_POST"/>

    <content type="application/atom+xml">

    <m:properties>

    <d:empid>0000000051</d:empid>

    <d:ename>Ram</d:ename>

    <d:city>Chennai</d:city>

    <d:empid>0000000090</d:empid>

    <d:ename>Kumar</d:ename>

    <d:city>Bangalore</d:city>

    </m:properties>

    </content>

    </entry>

former_member192971
Participant
0 Kudos

Hi Arun , As you said I tried with the batch method as given below.

In Header :

Content-type : multipart/mixed; boundary=changeset

and csrf-token and authorization.

URL : http://server:port/sap/opu/odata/sap/Z_RFC_POST/$batch

In Body:

--batch

Content-Type: multipart/mixed; boundary=changeset

--changeset

Content-Type: application/http

Content-Transfer-Encoding: binary

POST Z_RFC_POSTCOLLECTION  HTTP/1.1

Content-Type: application/atom+xml

<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">

<atom:content type="application/xml">

<m:properties

xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"

xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">

<d:empid>0000000101</d:empid>

<d:ename>AAAA</d:ename>

<d:city>Mangalore</d:city>

</m:properties>

</atom:content>

</atom:entry>

--changeset

Content-Type: application/http

Content-Transfer-Encoding: binary

POST Z_RFC_POSTCOLLECTION  HTTP/1.1

Content-Type: application/atom+xml

<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">

<atom:content type="application/xml">

<m:properties>

xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"

xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">

    <d:empid>0000000105</d:empid>

    <d:ename>BBBB</d:ename>

    <d:city>Australia</d:city>

</m:properties>

</atom:content>

</atom:entry>

--changeset--

--batch--

Also Tried with Another Method as Copying Whole XML Content Multiple Times in Body with Content-type Application/xml in header , But Not able to Post Multiple records at a time using both the approaches.

  Is that Approach which i followed is correct or did i misplaced anything ....Could you suggest me the correct procedure for Multiple records posting ..? And  Which Classes need to be               Redefined in gateway project-SEGW

you can write to me @ uday.tnl@gmail.com .....

  

Thanks,

Uday.

arunchembra1
Participant
0 Kudos

Hi Uday,

Try this and redefine /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_BEGIN and

/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END method and write 'EXIT' inside these methods and put external debugger inside CHANGESET_BEGIN and execute the URL in advance restclient in chrome.

In Header :

Authorization Basic ZGV2ZWxvcGVyOkFla2ExMjM=

X-CSRF-Token  2BViAS7kr97sDABrP9d9Kg==

Content-Type  multipart/mixed; boundary=batch

URL : http://server:port/sap/opu/odata/sap/Z_RFC_POST/$batch

 

In Body:

--batch

Content-Type: multipart/mixed; boundary=changeset

--changeset

Content-Type: application/http

Content-Transfer-Encoding: binary

POST Z_RFC_POSTCOLLECTIONCollection HTTP/1.1

Content-Type: application/atom+xml

Content-Length: 588

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"

xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"

xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">

<atom:content type="application/xml">

<m:properties>

<d:empid>0000000101</d:empid>

<d:ename>AAAA</d:ename>

<d:city>Mangalore</d:city>

</m:properties>

</atom:content>

</atom:entry>

--changeset

Content-Type: application/http

Content-Transfer-Encoding: binary

POST LEAVECollection HTTP/1.1

Content-Type: application/atom+xml

Content-Length: 588

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"

xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"

xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">

<atom:content type="application/xml">

<m:properties>

<d:empid>0000000102</d:empid>

<d:ename>BBB</d:ename>

<d:city>Bangalore</d:city>

</m:properties>

</atom:content>

</atom:entry>

--changeset--

--batch--

former_member192971
Participant
0 Kudos

Hi Arun ,

Tried with 'EXIT' command and with External Debugger in changeset_begin and changeset_end methods, but Getting the Error response as

<code>CX_SXML_PARSE_ERROR/001560AA0E081DEB8CA398D5EBFB9406</code><message xml:lang="en">Error while parsing an XML stream: '&lt;EOF&gt; reached'.

I redefined /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_BEGIN and/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END Methods and Just wrote Exit statement , Also I redefined Create_Entity Method with the code to Insert Multiple Records into Custom Table.

Logic in Create_Entity Method :
DATA : IT_POST TYPE STANDARD TABLE OF ZTABLE_POST,
          WA_POST TYPE ZTABLE_POST,
         T_FINAL TYPE STANDARD TABLE OF ZTABLE_POST,
          W_FINAL TYPE ZTABLE_POST.

   LOOP AT T_FINAL INTO W_FINAL.
     WA_POST-EMPID = W_FINAL-EMPID.
     WA_POST-ENAME = W_FINAL-ENAME.
     WA_POST-CITY = W_FINAL-CITY.
     APPEND WA_POST TO IT_POST.
   ENDLOOP.

   LOOP AT IT_POST INTO WA_POST.
     MODIFY ZTABLE_POST FROM WA_POST.
   ENDLOOP.

Is the whole approach which I followed till now is correct or Do i need to implement any other Methods.

BTW POST LEAVECollection HTTP/1.1 is unknown which you wrote in your previous reply .

Thanks,

uday.

arunchembra1
Participant
0 Kudos

Hi Uday,

I think this error was due to the problem in the XML data which you are passing in request body noot because of data provider logic.

Thanks,

Arun

kammaje_cis
Active Contributor
0 Kudos

Uday,

I have not tried this so far. But nothing seems to be wrong with your request. (the one given by Arun, not the original one posted by you.)

Just guessing.

Can you remove following line (mentioned multiple times) and try?

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

Also ensure that property names mentioned here are exactly same as those mentioned in metadata.

kammaje_cis
Active Contributor
0 Kudos

Hi Uday,

OData does not support creating multiple records at once.

However there is a concept called deep insert, where you can create a parent and child items at a time. For example Sales Order and multiple sales order items.

Check how to do a deep insert here.

http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/e0d92637-3d0d-2f10-ebb2-efc1f40a8...

There is an option to do batch update and search in google with OData and $batch to understand more.

One other workaround would be to create a dummy parent entity for your entity(for which you need to create multiple records), do a deep insert for your dummy entity and in the data provider class method, access all the children records and call your FM.

Thanks

Krishna

sreehari_vpillai
Active Contributor
0 Kudos

Hi Kishor,

The link you shared was too helpful. I am bit confused on something which a UI5 table control is bound with an OData model and the rows are editable. The user may change any row except the key values. I provided one update button in the footer. Is there any way, that I can update the entity set in background in a single payload ?

Regards

Sreehari