cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CAP: How to add query parameters to S/4 backend call

TobiT26
Explorer
419

Hello everyone,

I would like to consume an external S/4 backend service (odata) in my SAP CAP application. I have created the corresponding CDS schema and the calls also get through to the backend.

So far so good.

However, in addition to the normal parameters that are available in my CDS schema, there are also mandatory query parameters that I have to specify.

Hence the question: How can I add corresponding query parameters to my request?

I have already tried several possibilities and have actually found a workaround. However, I only see this option as a workaround and not as a permanent solution.

Basically, I build the URI for the call myself and execute the call via ext.send.

// connect to remote service
const ext = await cds.connect.to("ccp");

this.on("READ", "CustomerOpenItemSet", async (req) => {
    //*********************************** */
    //*********************************** */
    //*********************************** */
    //build url with query parameters
    //*********************************** */
    //*********************************** */
    //*********************************** */

    // Define the base URL path for the entity
    let urlWithParams = "/CustomerOpenItemSet?BackendUser=00010000000003&Scenario=D";

    // Manual addition of query parameters
    if (req._query) {
      // Extract the OData parameters from req.query and assemble them
      const queryParams = [];

      // Add further parameters such as $orderby, $select
      if (req._query.$select) {
        queryParams.push(`$select=${req._query.$select}`);
      }

      // for this command I get an error during the call
      // if (req._query.$count) {
      //   queryParams.push(`$count=${req._query.$count}`);
      // }

      if (req._query.$skip) {
        queryParams.push(`$skip=${req._query.$skip}`);
      }

      if (req._query.$top) {
        queryParams.push(`$top=${req._query.$top}`);
      }

      if (req._query.$filter) {
        queryParams.push(`$filter=${req._query.$filter}`);
      }

      if (req._query.$orderby) {
        queryParams.push(`$orderby=${req._query.$orderby}`);
      }

      // Combine the query parameters in the URL
      if (queryParams.length > 0) {
        urlWithParams += `&${queryParams.join("&")}`;
      }
    }

    console.log(urlWithParams);

    try {
      // Send the GET request with the dynamic parameters
      const result = await ext.send({
        method: "GET",
        path: urlWithParams,
      });

      //I need this so that the result is displayed in the frontend
      result.$count = result.length;

      return result;
    } catch (error) {
      console.error("Error when retrieving the data:", error);
      req.error({
        code: "EXTERNAL_SERVICE_ERROR",
        message: `Error when retrieving the data: ${error.message}`,
      });
    }

  });

 

Below are a few two other attempts, none of which led to the desired result.

  this.on("READ", "CustomerOpenItemSet_try1", async (req) => {
    //*********************************** */
    //*********************************** */
    //*********************************** */
    //add BackendUser as query-parameter
    //*********************************** */
    //*********************************** */
    //*********************************** */

    req._query.BackendUser = "00010000000003";
    const result1 = await ext.run(req.query);
    //OR
    req.query.BackendUser = "00010000000003";
    const result2 = await ext.run(req.query);

    return result1;
    //*********************************** */
    //--> BackendUser does not arrive in the backend as a query parameter
    //*********************************** */
  });
  this.on("READ", "CustomerOpenItemSet_try2", async (req) => {
    //*********************************** */
    //*********************************** */
    //*********************************** */
    //send-Request with query + headers
    //*********************************** */
    //*********************************** */
    //*********************************** */

    const result = await ext.send({
      query: req.query,
      headers: { BackendUser: "00010000000003" },
    });

    console.log(result);

    return result;

    //*********************************** */
    //--> BackendUser does not arrive in the backend as a query parameter
    //*********************************** */
  });


Do the CAP experts have any good ideas for my problem? 🙂 @gregorw @qmacro etc.

 

gregorw
Active Contributor
0 Kudos
Can you provide more details how the backend service is implemented? Are the parameters BackendUser and Scenario part of the OData Metadata? Maybe you could also provide more details what you want to achieve with the BackendUser and Scenario.
View Entire Topic
Dinu
Contributor
0 Kudos

I suppose these parameters are fixed for a connection. If so, you can add the following additional properties to the destination.

URL.queries.BackendUser=00010000000003
URL.queries.Scenario=D

The parameters  will get added to the URL as query parameters. 

gregorw
Active Contributor
0 Kudos
But this would only work if all users of the app should see the same data.
TobiT26
Explorer
0 Kudos
Yes, I know the option in the destination. But these are no fix values. In my example coding, I had only stored the values for test purposes.
Dinu
Contributor
0 Kudos
Is this service listed in api.sap.com?