使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者

i042416發表於2020-09-29

In the last part of my blog  Step by step to enable Text Collection for your custom BO I mentioned it is possible to create a web service in Cloud Application studio which contains a read operation, to return the Text Collection data to web service consumer. However the read operation can only support single BO instance ID as operation input parameter – on the other hand query operation does support multiple BO instance ID to fulfill the mass-handling requirement, however unfortunately it is not possible for query created in Cloud studio to directly return the transaction data of Text Collection. As a result we have to seek alternative.

The requirement to return text collection belonging to multiple BO instances could be achieved by C4C OData consisting of two steps.

Suppose I have two Custom BO instances JERRYORDER1 and JERRYORDER2, each has maintained their own Text Collection data:


使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者 使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者


The definition of my custom BO:


使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者


Here below are two necessary steps to return the Text Collection of these two BO instances via custom OData service.

(1) Create a new Custom OData Service in Work center Administrator, view OData Service Explorer, make sure you have selected Dependent object TextCollection:


使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者


Get the Object ID of the two BO instances by performing fitlering against their alternative key, in my custom BO the alternative key is MainBOID.

Create a new HTTP Get request in Chrome extension Postman with url:

https:///sap/c4c/odata/cust/v1/jerrylongtextodata/MainBORootCollection?$filter=MainBOID eq 'JERRYORDER1' or MainBOID eq 'JERRYORDER2'


使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者


write down the Object ID of these two BO instances from response:


使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者


(2) Create a HTTP post request in Postman:


使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者


Paste the following text as post body. In the post body, the Object ID of two BO instances fetched from previous step are used as input parameter of two embedded HTTP GET operation contained in the batch request.

--changeset_1Content-Type: application/httpContent-Transfer-Encoding: binaryGET MainBORootCollection('00163E20C9511ED7A9B1B0D65CCCDA81')?$expand=MainBOTextCollection HTTP/1.1Accept-Language: enAccept: application/jsonMaxDataServiceVersion: 2.0DataServiceVersion: 2.0--changeset_1Content-Type: application/httpContent-Transfer-Encoding: binaryGET MainBORootCollection('00163E20C9511ED7A9B19A78790759DE')?$expand=MainBOTextCollection HTTP/1.1Accept-Language: enAccept: application/jsonMaxDataServiceVersion: 2.0DataServiceVersion: 2.0--changeset_1--

Execute this post request in Postman, and the TextCollection for the two BO instances will be returned in a SINGLE roundtrip. Response for BO JERRYORDER2:


使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者


Response for BO JERRYORDER1:


使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者


Note

(1) If you have too many BO instances for which you would like to query their ObjectID by alternative key for subsequent query operation, it means you have to append a lengthy string like $filter=MainBOID eq ‘JERRYORDER1’ or MainBOID eq ‘JERRYORDER2’ or …. MainBOID eq ‘JERRYORDERN’ as HTTP Get request url. It is possible that the url will exceed  the maximum length of a URL for HTTP Get.

In this case you can avoid it by using HTTP Post instead:


使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者


HTTP post body for doing filter operation on multiple BO instances:

--changeset_1Content-Type: application/httpContent-Transfer-Encoding: binaryGET MainBORootCollection?$filter=MainBOID%20eq%20%27JERRYORDER1%27 HTTP/1.1Accept-Language: enAccept: application/jsonMaxDataServiceVersion: 2.0DataServiceVersion: 2.0--changeset_1Content-Type: application/httpContent-Transfer-Encoding: binaryGET MainBORootCollection?$filter=MainBOID%20eq%20%27JERRYORDER2%27 HTTP/1.1Accept-Language: enAccept: application/jsonMaxDataServiceVersion: 2.0DataServiceVersion: 2.0--changeset_1--

You will get the same result as using HTTP get with url:

https://<tenant>/sap/c4c/odata/cust/v1/jerrylongtextodata/MainBORootCollection?$filter=MainBOID eq 'JERRYORDER1' or MainBOID eq 'JERRYORDER2'


使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者


(2) In case you need to develop a Java Program to achieve the above mentioned two steps, you can find sample code in my blog  OData service parallel performance measurement – how to deal with XSRF token in Java Program and JMeter.

Further reading

You can find a list of all other blogs related to OData written by Jerry.

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2724931/,如需轉載,請註明出處,否則將追究法律責任。

相關文章