Performing Basic Operations Using Service Layer API

Objectives

After completing this lesson, you will be able to:
  • Perform CRUD operations (Creating, Retrieving, Updating, and Deleting entities)
  • Perform bound action and global action

CRUD Operations and Actions

OData protocol defines a standard way to Create/Retrieve/Update/Delete (CRUD) an entity. The CRUD operations are consistently implemented across all the business objects, as well as in the user-defined objects.

Diagram showing how HTTP verbs map to CRUD operations. POST connects to Create, GET to Retrieve, PATCH to Update, and DELETE to Delete.
  • POST - Create the entity.
  • GET - Retrieve the entity.
  • PATCH - Update the entity.
  • DELETE - Delete the entity.

Besides the basic entity CRUD operations, Service Layer provides you with two kinds of extended operations:

  • Bound action (bound to an entity for operations other than CRUD)
  • Global action (mainly used to expose SAP Business One services)

CRUD operations give access to most of the SAP Business One functionality. As a developer, you can use these operations to design your own functional flow.

How to Perform CRUD Operations

In this video, you’ll learn how to perform CRUD operations in the Service Layer.

For more information on CRUD operations and user-defined schemas, refer to the following resources:

How to Perform Bound Action and Global Action

Extended operations allow you to realize specific SAP Business One functionalities using service layer API. This gives more coverage to build/automate complex business requirements for customers.

In this video, you will learn more about extended operations using the same example used in the previous CRUD operations video.

For more information on actions and user-defined schemas, refer to Actions | SAP Help Portal.

Complete CRUD and Extended CRUD operations

In this exercise, you will practice Create, Retrieve, Update, Delete and Close operations.

Before starting this exercise, make sure that you:

Steps

  1. Create the sales order: Amend the following HTTP sample request and send to create a sales order:

  2. Sample request

    Code Snippet
    123456789101112131415
    POST https://localhost:50000/b1s/v2/Orders { "CardCode": "C20000", "DocDueDate": "2024-11-04", "PartialSupply": "tYES", "DocumentLines": [ { "ItemCode": "A00001", "Quantity": "100", "TaxCode": "A2", "UnitPrice": "300" } ] }

  3. Check the response:

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

    2. Check the response content - it should contain the information on newly created sales order in JSON format.

  4. Note down the DocEntry from the response content. You will use this for the subsequent request.

  5. Update the quantity of the previously created sales order: Amend the following HTTP sample request and send to update a sales order:

  6. Sample request

    Code Snippet
    123456789
    PATCH https://localhost:50000/b1s/v2/Orders(1229) { "DocumentLines": [ { "Quantity": "200" } ] }

  7. Check the response:

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

    2. Check the response content - no content is returned.

  8. Retrieve the sales order: First check that the quantity is updated. Then amend the following HTTP sample request and send to retrieve a sales order:

  9. Sample request

    Code Snippet
    1
    GET https://localhost:50000/b1s/v2/Orders(1229)

  10. Check the response:

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

    2. Check the response content - it should contain the complete information of the sales order retrieved with the updated Quantity property.

  11. Create a sales invoice based on the sales order you created in step 1: Amend the following HTTP sample request and send to create a sales invoice:

  12. Sample request

    Code Snippet
    123456789101112131415
    POST https://localhost:50000/b1s/v2/Invoices { "CardCode": "C20000", "DocDueDate": "2024-11-04", "PartialSupply": "tYES", "DocumentLines": [ { "BaseType": 17, "BaseEntry": 1229, "BaseLine": 0, "Quantity": "100" } ] }

  13. Check the response:

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

    2. Check the response content - it should contain the complete information of the invoice created.

  14. Close the sales order: Amend the following HTTP sample request and send to close the sales order.

  15. Sample request

    Code Snippet
    1
    POST https://localhost:50000/b1s/v2/Orders(1229)/Close

  16. Check the response:

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

    2. Check the response content - no content is returned.

  17. Check the status of the sales order: Repeat step 6 and check the response content property "DocumentStatus" is "bost_Close"

  18. Delete the itemcode: Amend the following HTTP sample request and delete the itemcode used in the sales order:

  19. Sample request

    Code Snippet
    1
    DELETE https://localhost:50000/b1s/v2/Items(‘A00001’)

  20. Check the response:

    1. Check the response code - it should be 400.

    2. Check the response content - it should contain an error message. For example: "You cannot remove item; linked transactions exist".

Result

You have successfully:

  • Performed create/update/retrieve/delete/close operations.
  • Checked the different status codes.
  • Checked the response received for the request.

Note

For more information on different entities and actions exposed in Service Layer, refer to the Service Layer API Reference page.

Log in to track your progress & complete quizzes