Managing SQL Queries Using Service Layer API

Objective

After completing this lesson, you will be able to manage SQL queries using Service Layer API

SQL Query

The entity SQLQuery is exposed in the Service Layer, providing you with:

  • A more dynamic way to execute a query in a secure and controllable way.
  • A simpler process for deploying a query.
  • An easier and more familiar way to develop query resources using a limited subset of SQL. This way you don’t need to learn a new query language, such as LINQ, HQL or DBQI/DBD.

By using SQLQueries, you can create, retrieve, update, and delete query resources. You can also define parameterized queries, which allow you to work with dynamic inputs.

OData 4.0 - Metadata

Code Snippet
1234567891011121314
<EntityType Name="SQLQuery"> <Key> <PropertyRef Name="SqlCode"/> </Key> <Property Name="SqlCode" Nullable="false" Type="Edm.String"/> <Property Name="SqlName" Type="Edm.String"/> <Property Name="SqlText" Type="Edm.String"/> <Property Name="ParamList" Type="Edm.String"/> <Property Name="CreateDate" Type="Edm.DateTimeOffset"/> <Property Name="UpdateDate" Type="Edm.DateTimeOffset"/> </EntityType> <EntitySet EntityType="SAPB1.SQLQuery" Name="SQLQueries"/>

The List function is a bounded function to run the query represented by a specific SQLQuery. Once an SQLQueryentity is created, the List function can be invoked in the following way with the verb GET or POST:

OData 4.0 - Metadata

Code Snippet
1234567891011121314
<Function IsBound="true" Name="List"> <Parameter Name="SQLQueryParams" Type="SAPB1.SQLQuery"/> <Parameter Name="ParamList" Type="Edm.String"/> <ReturnType Type="SAPB1.SQLQueryResult"/> </Function> <ComplexType Name="SQLQueryParams"> <Property Name="SqlCode" Type="Edm.String"/> </ComplexType> <ComplexType Name="SQLQueryResult" OpenType="true"> <Property Name="SqlText" Type="Edm.String"/> </ComplexType>

Grant Required Permissions

To grant permission to a normal user to create, update, remove, and read Service Layer SQLQueries, carry out the following steps:

  1. Log in to the SAP Business One, Desktop client as a super user.
  2. From the menu, select AdministrationSystem InitializationAuthorizationsGeneral Authorizations.
  3. Grant Full Authorization to the user on the sections Modify SQL Queries in Service Layer.
Screenshot of the SAP Business One Desktop client showing the Modify SQL Queries in Service Layer section switched to “Full Authorization”.

To grant permission to a normal user to execute (List) the query, carry out the following steps:

  1. Log in to the SAP Business One, Desktop client as a super user.
  2. From the menu, select AdministrationSystem InitializationAuthorizationsGeneral Authorizations.
  3. Find the specific SQL Query from Service Layer SQL Query section.
  4. Change No Authorization to Full Authorization.
Screenshot of the SAP Business One Desktop client showing the Service Layer SQL Query section switched to “Full Authorization”.

How to Create SQLQueries Using CRUD Operations

In this video you’ll learn how to use SQLQuery entity and execute queries.

Manage SQL Queries Using SQLQuery Entity

In this exercise, you’ll practice create, update, delete, and execute queries using SQLQuery entity in the Service Layer.

Before starting this exercise, make sure that you:

Steps

  1. To create a query definition, amend the following HTTP sample request and send:

  2. Sample request

    Code Snippet
    1234567
    POST https://localhost:50000/b1s/v2/SQLQueries { "SqlCode": "Demo_Q1", "SqlName": "Demo - list of open Salesorder", "SqlText": "SELECT T0.[DocNum],T0.[DocDate], T0.[DocDueDate], T0.[CreateDate],T2.[Name] 'Contact Parson' FROM ORDR T0 INNER JOIN OCPR T2 ON T0.[CntctCode] = T2.[CntctCode] WHERE T0.[DocStatus] = 'o'and T0.[DocDueDate] > '2020-01-01'order by T0.[Createdate] desc" }

  3. Check the response:

    1. Check the response code - it should be 201 Created if the HTTP request is successful.

    2. Check the response content - it should contain information on the query definition you created.

  4. To execute the query which you created in step1, amend the following HTTP sample request and send:

  5. Sample request

    Code Snippet
    1
    POST https://localhost:50000/b1s/v2/SQLQueries('Demo_Q1')/List

  6. Check the response:

    1. Check the response code - it should be 200 OK if the HTTP request is successful.

    2. Check the response content - it should contain the query result as defined in the query definition.

    3. At the end of the response content, there is the property odata.nextLink, which provides the link for the next collection chunk.

  7. To update the query (which we created in the step with the parameters), amend the following HTTP sample request and send:

  8. Sample request

    Code Snippet
    1234567
    PATCH https://localhost:50000/b1s/v2/SQLQueries('Demo_Q1') { "SqlCode": "Demo_Q1", "SqlName": "Demo - list of open Salesorder", "SqlText": "SELECT T0.[DocNum],T0.[DocDate], T0.[DocDueDate], T0.[CreateDate],T2.[Name] 'Contact Parson' FROM ORDR T0 INNER JOIN OCPR T2 ON T0.[CntctCode] = T2.[CntctCode] WHERE T0.[DocStatus] = :DocStatus and T0.[DocDueDate] > :DocDueDate order by T0.[Createdate] desc" }

  9. Check the response:

    1. Check the response code - it should be 204 No Content if the HTTP request is successful.

    2. Check the response content - it should contain empty response.

  10. To execute the query with parameters, amend the following HTTP sample request and send:

  11. Sample request

    Code Snippet
    12345
    POST https://localhost:50000/b1s/v2/SQLQueries('Demo_Q1')/List { "ParamList":"DocStatus='O'&DocDueDate='2020-01-01'" }

  12. Check the response:

    1. Check the response code - it should be 200 OK if the HTTP request is successful.

    2. Check the response content - it should contain the results for the query definition.

  13. To delete the query definition created in step 1, amend the following HTTP sample request and send:

  14. Sample request

    Code Snippet
    1
    DELETE https://localhost:50000/b1s/v2/SQLQueries('Demo_Q1')

  15. Check the response:

    1. Check the response code - it should be 204 - No Content, if the HTTP request is successful.

    2. Check the response content - it should contain an empty response.

Result

You have successfully performed various operations using SQLQuery entity checked the request output.

Log in to track your progress & complete quizzes