cancel
Showing results for 
Search instead for 
Did you mean: 

Create Records through OData using Jquery

Former Member
0 Kudos

Hi All,

I have created a OData Service in SAP ECC system in format

http://hostname:portno/sap/opu/odata/sap/Z_PORDER_SRV/PurchaseOrderHeaderCollection

I am trying to create a record from front end using JQuery.   I am able to retrieve all the records, but creation of records using Method "POST" fails.

For creating records, using the method "GET", i fetch the token "X-CSRF-Token" and then pass the token to the "POST" method.

I am getting an Exception "No Handler for Data" error is coming.   I am accessing the OData Service using the datajs framework.

(datajs-1.1.1.min.js).

Please help me in resolving this issue.

Thanks,

Divya

               

Accepted Solutions (1)

Accepted Solutions (1)

jamie_cawley
Advisor
Advisor
0 Kudos

Did you set all of the headers correctly?  Please post your code.

Regards,

Jamie

SAP - Customer Experience Group

Former Member
0 Kudos

Thanks for the reply Jamie.

Code:

OData.request({

               "http://host:port/sap/opu/odata/sap/Z_PORDER_SRV/PurchaseOrderHeaderCollection",     

                method : "GET",

               

                user: Customer_Username, 

               

                password: Customer_Password,

                headers :
                {
                       "X-Requested-With" : "XMLHttpRequest",
                        "Content-Type" : "application/XML",
                       "DataServiceVersion" : "2.0",

                       "X-CSRF-Token" : "Fetch"
                } 
          },

          function(data, response)
          {
                        var header_xcsrf_token = response.headers['x-csrf-token'];
                        OData.request(
                          {
                                requestUri :    "http://host:portno/sap/opu/odata/sap/Z_PORDER_SRV/PurchaseOrderHeaderCollection",                     

                                method : "POST",                 
                                user: Customer_Username, 
                                password: Customer_Password, 

        
                                headers :
                                {
                                       "X-Requested-With" : "XMLHttpRequest",
               
                                      "Content-Type" : "application/XML",
                      
                                      "DataServiceVersion" : "2.0",                      
          
                                      "X-CSRF-Token" : header_xcsrf_token
                               },
                              data: 
                             {
                                 PONumber:"4500000091",
                                 CompanyCode:"TVSC",
                                 PurchaseDocCategory:"F",
                                 PurchaseDocType:"NB",
                                 PurchaseDocStatus:"9", 
                                 CreatedBy: "", 
                                 VendorAccountNumber:"V1",
                                 PurchaseOrganization:"PO01",
                                 PurchaseOrgGroup:"101",                      
                                 TotalValueAtRelase:"0.00",
                                  POItem: "00010",
                                  Material:"",
                                  MaterialNumber: "000000000000000011",
                                  Plant: "PL01",
                                  MaterialGroup: "01",
                                  TargetQuantity: "0.000",
                                  Quantity: "5.000",
                                  Unit: "ST",
                                  OrderPriceUnit:"ST",
                                  NetPrice: "10.0000",
                                  PriceUnit:1,
                                  GrossValue:"0.0000",
                                  NetValue:"0.0000",
                                  NetWeight:"0.000",
                                  WeightUnit:"",
                                 MaterialType:"",
                                 PurchaseOrderHeaderPONumber:"",
                                 PurchaseOrderHeaderPONumber1:""
                              }
                   },
                  function(data,response)
                  {
                       alert('Success. Created New Record');
                  },
                  function(err1)
                  {
                      alert('Error in Create Record...'+err1.message);
                  }
             );
             },
                 function(err)
                 {
                        alert('error....'+err.message);
                  }
   );
             

Output

I get an error message "Error in Create Record. "No Handler for Data".

I tried entering the header  details alone and also by entering the line items. Still the same error is thrown.

Please Help.

Thanks,

Divya

jamie_cawley
Advisor
Advisor
0 Kudos

It looks like you want to pass in json but you have the content type set to xml.  I would suggest testing your post in either the gateway client or a browser rest client plugin such as postman.  The dev tools in chrome/firefox can help too.

Regards,

Jamie

SAP - Customer Experience Group

Former Member
0 Kudos

Hi Jamie,

The issue still persists. Using Rest client we checked, records are getting created. But I think, the way data is passed using jquery is wrong.

Can you please let know how to pass data in JSON format using jquery?

Should i pass the XML Structure as a String.

data: {

"__metadata": {

             "id": "http://host:port/sap/opu/odata/sap/Z_PORDER_SRV/PurchaseOrderHeaderCollection('4500000001')",

             "uri": "http://host:port/sap/opu/odata/sap/Z_PORDER_SRV/PurchaseOrderHeaderCollection('4500000001')",

             "type": "Z_PORDER_SRV.PurchaseOrderHeader",

             "properties": {

                 "PurchaseOrderItem": {

                     "associationuri": "http://host:port/sap/opu/odata/sap/Z_PORDER_SRV/PurchaseOrderHeaderCollection('4500000001')/$links/P..."

                 }

             }

         },

         "PONumber": "4500000001",

         "CompanyCode": "TVSC",

         "PurchaseDocCategory": "F",

         "PurchaseDocType": "NB",

         "PurchaseDocStatus": "9",

         "CreatedBy": "",

         "VendorAccountNumber": "V1",

         "PurchaseOrganization": "PO01",

         "PurchaseOrgGroup": "101",

         "TotalValueAtRelase": "0.00",

         "PurchaseOrderItem": [{

             "__metadata": {

                 "id": "http://host:port/sap/opu/odata/sap/Z_PORDER_SRV/PurchaseOrderItemCollection(PONumber='4500000001',PO...')",

                 "uri": "http://host:port/sap/opu/odata/sap/Z_PORDER_SRV/PurchaseOrderItemCollection(PONumber='4500000001',PO...')",

                 "type": "Z_PORDER_SRV.PurchaseOrderItem",

                 "properties": {

                     "PurchaseOrderHeader": {

                         "associationuri": "http://host:port/sap/opu/odata/sap/Z_PORDER_SRV/PurchaseOrderItemCollection(PONumber='4500000001',PO..."

                     }

                 }

             },

             "PONumber": "4500000001",

             "POItem": "00010",

             "Material": "",

             "MaterialNumber": "000000000000000011",

             "Plant": "PL01",

             "MaterialGroup": "01",

             "TargetQuantity": "0.000",

             "Quantity": "5.000",

             "Unit": "ST",

             "OrderPriceUnit": "ST",

             "NetPrice": "10.0000",

             "PriceUnit": "1",

             "GrossValue": "0.0000",

             "NetValue": "0.0000",

             "NetWeight": "0.000",

             "WeightUnit": "",

             "MaterialType": "",

             "PurchaseOrderHeaderPONumber": "",

             "PurchaseOrderHeaderPONumber1": "",

             "PurchaseOrderHeader": {

                 "__deferred": {

                     "uri": "http://host:port/sap/opu/odata/sap/Z_PORDER_SRV/PurchaseOrderItemCollectionPONumber='4500000001',POI..."

                 }

             }

         }]

};

Thanks,

Divya

jamie_cawley
Advisor
Advisor
0 Kudos

Try changing the content type to...

contentType: 'application/json'


Regards,

Jamie

SAP - Customer Experience Group

kammaje_cis
Active Contributor
0 Kudos

You do not need all those URLs in the request body. It has to be as simple as below.

data: {

          "PONumber": "4500000001",

         "CompanyCode": "TVSC",

         "PurchaseDocCategory": "F",

         "PurchaseDocType": "NB",

         "PurchaseDocStatus": "9",

         "CreatedBy": "",

         "VendorAccountNumber": "V1",

         "PurchaseOrganization": "PO01",

         "PurchaseOrgGroup": "101",

         "TotalValueAtRelase": "0.00",

         "PurchaseOrderItem": [{

         "PONumber": "4500000001",

             "POItem": "00010",

             "Material": "",

             "MaterialNumber": "000000000000000011",

             "Plant": "PL01",

             "MaterialGroup": "01",

             "TargetQuantity": "0.000",

             "Quantity": "5.000",

             "Unit": "ST",

             "OrderPriceUnit": "ST",

             "NetPrice": "10.0000",

             "PriceUnit": "1",

             "GrossValue": "0.0000",

             "NetValue": "0.0000",

             "NetWeight": "0.000",

             "WeightUnit": "",

             "MaterialType": "",

          ]}

}

Former Member
0 Kudos

Hi Jamie,

I tried with application/json. But result is the same.

Thanks,

Divya

Former Member
0 Kudos

Hi Krishna,

I tried using the same format. But still getting "No handler for data" error. I tried with content type as "application/json" and application/XML.



data:

{

     "PONumber":"4500000091",

     "CompanyCode":"TVSC",

     "PurchaseDocCategory":"F",

     "PurchaseDocType":"NB",

     "PurchaseDocStatus":"9",

     "CreatedBy": ""

     "VendorAccountNumber":"V1",

     "PurchaseOrganization":"PO01",

     "PurchaseOrgGroup":"101"

     "TotalValueAtRelase":"0.00",

     "PurchaseOrderItem":[{

     "PONumber":"4500000091",

     "POItem": "00010",

     "Material":"",

     "MaterialNumber": "000000000000000011",

     "Plant": "PL01",

     "MaterialGroup": "01",

     "TargetQuantity": "0.000",

     "Quantity": "5.000",

     "Unit": "ST",

     "OrderPriceUnit":"ST",

     "NetPrice": "10.0000",

     "PriceUnit":"1",

     "GrossValue":"0.0000",

     "NetValue":"0.0000",

     "NetWeight":"0.000",

     "WeightUnit":"",

     "MaterialType":"",

     "PurchaseOrderHeaderPONumber":"",

     "PurchaseOrderHeaderPONumber1":"",        

      }]

}

Please help in resolving this issue. It is working fine in REST Client.

Thanks,

Divya

Former Member
0 Kudos

Thanks all... My issue is resolved.

Passing data, I passed the entire JSON Structure from the REST client and mapped the required fields.

Thanks,

Divya

Answers (0)