Search
Close this search box.

POST Request for creating

OData API
POST Request for creating

With the POST request it is possible that you create new data entry in the SAP Sales Cloud

The POST request consists of three parts: The URL, the header and a body. The x‑csrf token is the most important component in the header for creating, since without this token you have no authorization to create new entries. In the body you specify which fields properties you want to consist doing the creation phase. Always note that some fields will be filled automatically by the system such as ObjectID, ID, etc.

4.1. Single POST Request

When creating new entries, the URL consists of only two parts. The request URL for the API and the collection for the SAP Sales Cloud object you want to create.

When making the POST request, make sure that you include the x-csrf-token in the header of the request, otherwise you will get the 403 Forbidden status. Navigate to Headers tab and make sure to put not only the x-csrf-token but also the Content-Type for the body.

Next, click the Body tab and select raw as the type here. Again make sure that the JSON format has been detected. You can now define the fields in JSON format that you want to consider when creating.

The following part shows the response you will receive if the request was successful. In this response you will automatically receive important fields like ObjectID, ID, etc. as a response.

Successfully Updated                  Status: 201 Created

<?xml version="1.0" encoding="utf-8"?>
<entry m:etag="W/&quot;datetimeoffset'2021-08-06T08%3A28%3A22.2480570Z'&quot;" xml:base="https://my######.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/" 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>https://my######.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/VisitCollection('00163EA7293F1EDBBDD2083E55548147')</id>
    <title type="text">VisitCollection('00163EA7293F1EDBBDD2083E55548147')</title>
    <updated>2021-08-06T08:28:23Z</updated>
    <category term="c4codata.Visit" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
    <link href="VisitCollection('00163EA7293F1EDBBDD2083E55548147')" rel="edit" title="Visit"/>
    <link href="VisitCollection('00163EA7293F1EDBBDD2083E55548147')/VisitAttachment" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VisitAttachment" type="application/atom+xml;type=feed" title="VisitAttachment"/>
    <link href="VisitCollection('00163EA7293F1EDBBDD2083E55548147')/VisitBTDReference" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VisitBTDReference" type="application/atom+xml;type=feed" title="VisitBTDReference"/>
    <link href="VisitCollection('00163EA7293F1EDBBDD2083E55548147')/VisitParty" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VisitParty" type="application/atom+xml;type=feed" title="VisitParty"/>
    <link href="VisitCollection('00163EA7293F1EDBBDD2083E55548147')/VisitTextCollection" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VisitTextCollection" type="application/atom+xml;type=feed" title="VisitTextCollection"/>
    <link href="VisitCollection('00163EA7293F1EDBBDD2083E55548147')/VisitWorklistItem" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VisitWorklistItem" type="application/atom+xml;type=feed" title="VisitWorklistItem"/>
    <content type="application/xml">
        <m:properties>
       <d:ObjectID>00163EA7293F1EDBBDD2083E55548147</d:ObjectID>
       <d:ID>12402</d:ID>
       <d:Subject>Creating a New Visit via OData API</d:Subject>
       <d:Status>2</d:Status>
       <d:StatusText>In Process</d:StatusText>
       <d:PriorityCode>3</d:PriorityCode>
       <d:PriorityCodeText>Normal</d:PriorityCodeText>
       <d:Account>00163E36-C2CF-1ED9-8CF9-9771FAFB115F</d:Account>
       <d:MainAccountPartyID>1191277</d:MainAccountPartyID>
       ...

Below you can see two more responses, when you either try to create a property – which does not exist – or you try to affect a property, which is not possible – such as the ID.

Property not found:                       Status: 400 Bad Request

<?xml version="1.0" encoding="utf-8"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <code/>
    <message xml:lang="en">Property 'ABC' not found</message>
</error>

Property not updated:                  Status: 400 Bad Request

<?xml version="1.0" encoding="utf-8"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <code>CX_ODATA_ANNOTATION_ERROR</code>
    <message xml:lang="en">Update not allowed for properties: ID. Check metadata for VisitCollection</message>
</error>

4.2. Create multiple entries with $batch

If you want to create multiple entries using the $batch parameter, the request URL always follows the syntax below:

https://my######.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/$batch

As you can see, this request URL does not specify which collection is being requested, nor what should be passed with the request. This information is provided in the body of the request. The only important thing here is that the parameter $batch is noted at the end.

Header

In the header you specify the necessary parameters so that the request can be executed without errors. In the header, make sure that the x-csrf-token is stored and, most importantly, set the Content-Type. The value of the Content-Type corresponds to: multipart/mixed; boundary=batch#BATCHNAME#. The parameter #BATCHNAME# must correspond to the name in the body.

Body

A batch request is very sensitive when it comes to spaces and line spacing. So please make sure that you follow the required syntax. Below you can see an example of a body. The grey lines which are starting with // are comments and should be deleted, if you want to copy this for trying it yourself.

--batch_B01
// the name of the batch request is B01 and was separated
// with an underline. As said make sure that this batch is equal
// as in the header parameter of content-type
Content-Type: multipart/mixed; boundary=changeset_C01

// Start of the changeset C01:
--changeset_C01
Content-Type: application/http
Content-Transfer-Encoding: binary

POST VisitCollection HTTP/1.1
// Define the type of request first -> Here: POST
// Define the collection secondly -> Here: VisitCollection
// End with HTTP/1.1
sap-context-accept: header
Content-Type: application/json
Accept: application/json

// JSON input for creating a visit:
{
"Subject":"Creating Visit 1 with batch",
"Status":"1",
"MainAccountPartyID":"1191277",
"MainContactPartyID":"1191278",
"OwnerPartyID":"8000000167",
"StartDateTime":"2021-08-06T12:00:00.000Z",
"EndDateTime":"2021-08-06T16:00:00.000Z"
}

// End of the changeset C01:
--changeset_C01--

--batch_B01
Content-Type: multipart/mixed; boundary=changeset_C02

// Start of the changeset C02:
--changeset_C02
Content-Type: application/http
Content-Transfer-Encoding: binary

POST VisitCollection HTTP/1.1
// Define the type of request first -> Here: POST
// Define the collection secondly -> Here: VisitCollection
// End with HTTP/1.1
sap-context-accept: header
Content-Type: application/json
Accept: application/json

// JSON input for creating a visit:
{
"Subject":"Creating Visit 2 with batch",
"Status":"1",
"MainAccountPartyID":"1191277",
"MainContactPartyID":"1191278",
"OwnerPartyID":"8000000167",
"StartDateTime":"2021-08-06T12:00:00.000Z",
"EndDateTime":"2021-08-06T16:00:00.000Z"
}
// End of the changeset C02
--changeset_C02--

// End of batch:
--batch_B01--

Do you need consulting from our experts?

The Customer Experience team at Camelot ITLab deals with exciting and challenging CRM related topics every day and serves a large portfolio of different customers from a wide range of industries. Trust in this collaboration and feel free to contact us at tlil@camelot-itlab.com.

Was this article helpful?

If you like our content we would highly appreciate your review on Trustpilot

 

#SAP C4C #SAP Cloud 4 Customer #Cloud 4 Customer #Cloud for Customer #SAP Sales Cloud #Sales Cloud #OData #API #Request #Postman #POSTRequest #Post

Receive the latest news

Subscribe To our SAP Sales & Service Cloud Newsletter

Get notified about new articles