简体   繁体   中英

SAP Cloud SDK for Javascript: Filter on Expanded Entities

I know I am reopening an old one ( Perform filter on expanded entity with SAP Cloud SDK ), but it was a while ago and was referencing the Java version of the API used to consume an S/4 HANA service.

I'm testing the Javascript version of the API against the SuccessFactors OData API, which is indeed able to perform filters on expanded entities, like so:

<API_HOST>/odata/v2/PerPerson?$filter=personalInfoNav/firstName eq 'MARCO'&$expand=personalInfoNav&$select=personalInfoNav/firstName, personalInfoNav/lastName&$top=20

Translated into the SDK, it would be (TypeScript):

const personList: Array<PerPerson> = 
    await PerPerson.requestBuilder().getAll().top(20)
        .select(
            PerPerson.DATE_OF_BIRTH,
            PerPerson.PERSONAL_INFO_NAV.select(
                PerPersonal.PERSON_ID_EXTERNAL,
                PerPersonal.FIRST_NAME,
                PerPersonal.LAST_NAME,
                PerPersonal.GENDER
            )
        ).filter(PerPersonal.FIRST_NAME.equals('MARCO'))
         .execute({ destinationName: this.configService.get<string>('ACTIVE_DESTINATION') });

But this code does not compile because of the incompatibility of types for the filter function, which here expects a "PerPerson" type and not "PerPersonal". I was not able to find anything about this.

Considering that the plain OData query works perfectly, anyone has been able to make this work?

Update:

I didn't initially understand that Successfactors offered this functionality on top of the protocol. There are two possible workarounds I can think of:

  1. use new Filter , eg:
PerPerson.requestBuilder().getAll().top(20)
  .select(
   ...
  ).filter(
    new Filter('personalInfoNav/firstName', 'eq', 'MARCO')
  )
...
  1. If this doesn't work, you can call build on the request builder instead of execute , which gives you the ODataRequest object from which you can get the URL, which you'd then have to manipulate manually. Then you should be able to use the executeHttpRequest function to execute this custom request object against a destination.

Let me know if one of these work for you!


Original answer:

filtering on expanded entities on OData v2 is only possible if the relationship between the two entities is 1:1. In the case the code would look like this:

PerPerson.requestBuilder().getAll().top(20)
  .select(
    PerPerson.DATE_OF_BIRTH,
    PerPerson.PERSONAL_INFO_NAV.select(
      PerPersonal.PERSON_ID_EXTERNAL,
      PerPersonal.FIRST_NAME,
      PerPersonal.LAST_NAME,
      PerPersonal.GENDER
    )
  ).filter(
    PerPerson.PERSONAL_INFO_NAV.filter(
      PerPersonal.FIRST_NAME.equals('MARCO')
  ))
...

If, however, the relationship is 1:n, filtering is not possible.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM