使用OData服务将SAP C4C自定义BO的TextCollection暴露给外部消费者

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:


The definition of my custom BO:

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:

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'

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

(2) Create a HTTP post request in Postman:

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_1
Content-Type: application/http
Content-Transfer-Encoding: binary

GET MainBORootCollection('00163E20C9511ED7A9B1B0D65CCCDA81')?$expand=MainBOTextCollection HTTP/1.1


Accept-Language: en
Accept: application/json
MaxDataServiceVersion: 2.0
DataServiceVersion: 2.0

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

GET MainBORootCollection('00163E20C9511ED7A9B19A78790759DE')?$expand=MainBOTextCollection HTTP/1.1


Accept-Language: en
Accept: application/json
MaxDataServiceVersion: 2.0
DataServiceVersion: 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:

Response for BO JERRYORDER1:

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:

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

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

GET MainBORootCollection?$filter=MainBOID%20eq%20%27JERRYORDER1%27 HTTP/1.1


Accept-Language: en
Accept: application/json
MaxDataServiceVersion: 2.0
DataServiceVersion: 2.0

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

GET MainBORootCollection?$filter=MainBOID%20eq%20%27JERRYORDER2%27 HTTP/1.1


Accept-Language: en
Accept: application/json
MaxDataServiceVersion: 2.0
DataServiceVersion: 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'

(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的原创文章,请关注公众号"汪子熙":

原文地址:https://www.cnblogs.com/sap-jerry/p/13576576.html