CLAIMATE

Technical blog

Restricting Returned Fields in an OData Service for Expanded Entities

created by DALL-E

Vlado Balko

7. 8. 2024

Development

When working with OData services, it’s often necessary to limit the amount of data returned by the service to improve performance and reduce payload size. This is particularly useful when dealing with expanded entities, where the potential for large amounts of data is significant. Here’s a concise guide on how to restrict returned fields in an OData service, especially for expanded entities.

Example OData Request

Below is an anonymized and simplified OData request that demonstrates how to filter and select specific fields, including those in an expanded entity, and ensures the response format is JSON:

/sap/opu/odata4/sap/zui_xf_example_o4/srvd/sap/zsrv_xf_example/0001/ZFI_C_XF_EXAMPLE_H?$filter=example_id eq '${context.data.example_id}' and fiscal_year eq '${context.data.fiscal_year}'&$select=example_id,example_type,created_by,fiscal_year&$expand=_XFExampleDetails($select=company_code,amount_local,due_date,document_number)&$format=json&sap-client=100

Breakdown of the Request

1. Base Entity Filtering and Selection:

  • Filtering: The request filters the base entity (ZFI_C_XF_EXAMPLE_H) to only include entries where example_id equals ${context.data.example_id} and fiscal_year equals ${context.data.fiscal_year}.

  • Selecting Specific Fields: The $select parameter specifies that only certain fields (example_idexample_typecreated_byfiscal_year) should be included in the response.

2. Expanding and Selecting Fields from Related Entities:

  • Expanding: The $expand parameter is used to include related entities (_XFExampleDetails) in the response.

  • Selecting Specific Fields from Expanded Entities: Within the $expandparameter, the $select clause specifies which fields from the expanded entity should be included (company_codeamount_localdue_datedocument_number)

3. Specifying Response Format:

  • Response Format: The $format=json parameter ensures the response is in JSON format, making it easier to handle in modern web applications.

Example Output JSON

The following is an example of what the output JSON might look like for the above request:

{
  "d": {
    "results": [
      {
        "example_id": "12345",
        "example_type": "Type1",
        "created_by": "User1",
        "fiscal_year": "2024",
        "_XFExampleDetails": {
          "results": [
            {
              "company_code": "1000",
              "amount_local": "5000",
              "due_date": "2024-12-31",
              "document_number": "1234567890"
            }
          ]
        }
      }
    ]
  }
}

Focus on $expand and $select

Using the $expand parameter allows you to include related entities within the main entity's response. By pairing $expand with $select, you can precisely control which fields from the related entities are included. This combination is powerful for optimizing the data returned by the service, ensuring you only retrieve necessary information, which can significantly enhance performance and readability of the payload.

Here’s the key part of the request focusing on the $expand and $selectparameters:

$expand=_XFExampleDetails($select=company_code,amount_local,due_date,document_number)

  • $expand: This part ensures that the related entity _XFExampleDetails is included in the response.

  • $select within $expand: Specifies which fields from _XFExampleDetails should be included, thus filtering out unnecessary data and optimizing the response payload.

It was also published here:

https://community.sap.com/t5/technology-blogs-by-members/restricting-returned-fields-in-an-odata-service-for-expanded-entities/ba-p/13785878
https://medium.com/@vbalko/restricting-returned-fields-in-an-odata-service-for-expanded-entities-b66e68094fb9

Navigace

© 2023 CLAIMATE

Navigace

© 2023 CLAIMATE