Search
Close this search box.

PATCH Request for updating

OData API
PATCH Request for updating

With the PATCH request it is possible that you update data in the SAP Sales Cloud

This 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 updating, since without this token you have no authorization to change the data. In the body you specify which fields are to be updated with which value.

5.1. Single PATCH Request

To be able to update an object you always need the ObjectID. A PATCH request consists of three parts: The Request URL to API, the Collection and the ObjectID. Die ObjectID has to be noted in the request in brackets and apostrophes.

With that said, set the x-csrf-token and content-type for your PATCH query in the header. The content-type defines in which form the body will be written. In this case application/json will be used.

In the next step, you define the body for this query. To do this, click on the Body tab and select the raw type. The content-type JSON will now be displayed again. Make sure to define your JSON input.

There are three possible payloads. The first payload would be the successful one – which has no content. As status you get here: 204 – No Content. The second payload would be if a property – which you have defined in your body – does not exist. And the third possible payload would be if you try to change a property, which is not possible according to the metadata.

Successfully Updated                  Status: 204 No Content

     

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>

5.2. Change multiple entries with $batch

The PATCH request is similar to the POST request. The structure of the header must be exactly the same. Only the body looks slightly different. Again make sure that the lines starting with // are comments and shall be deleted, if you want to use this as a try:

--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

PATCH VisitCollection('00163EA729061EEBBDCC209B2C03C6AC') HTTP/1.1
// Define the type of request first -> Here: PATCH
// Define the collection secondly -> Here: VisitCollection
// Provide the ObjectID of the Object you want to update
// End with HTTP/1.1
sap-context-accept: header
Content-Type: application/json
Accept: application/json

// JSON input for updating a visit:
{
    "Subject" : "Update Visit",
    "MainAccountPartyID" : "1229342",
    "Status" : "2"
}

// End of changeset_C01
--changeset_C01--

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

--changeset_C02
Content-Type: application/http
Content-Transfer-Encoding: binary

PATCH VisitCollection('00163EA729061EEBBDCC99E1FFDDC890') HTTP/1.1
// Define the type of request first -> Here: PATCH
// Define the collection secondly -> Here: VisitCollection
// Provide the ObjectID of the Object you want to update
// End with HTTP/1.1
sap-context-accept: header
Content-Type: application/json
Accept: application/json

// JSON input for updating a visit:
{
    "Subject" : "Update Visit",
    "MainAccountPartyID" : "1229342",
    "Status" : "2"
}

// End of 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 #PATCHRequest #Patch

Receive the latest news

Subscribe To our SAP Sales & Service Cloud Newsletter

Get notified about new articles