Search code examples
javascripttypescripts4sdksap-cloud-sdk

How to get the eTag(MetaData) from a DocumentInfoRecord get() call in SAP Cloud SDK for javascript?


I use the SAP Cloud SDK for javascript to handle DocumentInfoRecords. An update of a DIR caused the error 428. So I need the etag of the request like in the SAP Cloud API.

How can I get the etag from the GET request or generally header response information of each sdk request?

GET:

DocumentInfoRecord.requestBuilder()
.getByKey(dir.documentInfoRecordDocType, dir.documentInfoRecordDocVersion, dir.documentInfoRecordDocNumber, dir.documentInfoRecordDocPart)
.execute({});

UPDATE with etag

DocumentInfoRecord.requestBuilder().update(dir).withCustomHeaders({ key: "If-Match", value: "etag" }).execute({});

Solution

  • Since version 1.1.0, the SAP Cloud SDK for JavaScript (fka SAP S/4HANA Cloud SDK) transparently handles ETag in the background. For more information, take a look at the section on optimistic concurrency control in the release blog.

    If you update to the latest version (1.2.1 at the time of writing), you can simply retrieve the document info record, change the fields you want to change, and then update it by sending the same object to the service.

    var dir = await DocumentInfoRecord.requestBuilder()
        .getByKey(dir.documentInfoRecordDocType, dir.documentInfoRecordDocVersion, dir.documentInfoRecordDocNumber, dir.documentInfoRecordDocPart)
        .execute({destinationName: "MyServer"});
    dir.responsiblePersonName = "John Doe";
    DocumentInfoRecord.requestBuilder()
        .update(dir)
        .execute({destinationName: "MyServer"})
        .then(...);