Implement OData actions for business operations

Completed

OData actions expose business operations that aren't represented well by ordinary create, update, or delete requests. The relationship between the operation and an entity determines whether the action is bound or unbound.

Implement an entity-bound action

A bound action operates on the entity identified in the request URL. Add the procedure to the page that exposes the entity and decorate it with [ServiceEnabled]. A WebServiceActionContext identifies the affected object and entity and communicates the result category.

[ServiceEnabled]
procedure SetIntegrationEmail(
    var ActionContext: WebServiceActionContext; email: Text[80])
begin
    if email = '' then
        Error('Email must contain a value.');

    Rec.Validate("E-Mail", email);
    Rec.Modify(true);

    ActionContext.SetObjectType(ObjectType::Page);
    ActionContext.SetObjectId(Page::"Integration Customer API");
    ActionContext.AddEntityKey(Rec.FieldNo(SystemId), Rec.SystemId);
    ActionContext.SetResultCode(WebServiceActionResultCode::Updated);
end;

The action uses the same record identity as the API page. Validation and table triggers still apply. The caller also needs execute permission on the page and sufficient table-data permission for the operation.

For a custom API page, the request route includes the API entity and the Microsoft.NAV action namespace.

POST {apiBaseUrl}/api/training/integration/v1.0/companies({companyId})/integrationCustomers({id})/Microsoft.NAV.SetIntegrationEmail
Authorization: Bearer {token}
Content-Type: application/json

{
  "email": "integration@example.com"
}

Use a bound action when the target entity is part of the operation's meaning. Keep the parameters focused on operation input rather than repeating the entity key already present in the URL.

Bound actions can't be added by extending an existing published page. Define the procedure on the page that owns the service contract.

Implement an unbound action

An unbound action doesn't belong to one entity. Define a public procedure in a codeunit and publish the codeunit as an OData V4 web service. The procedure doesn't use [ServiceEnabled] or WebServiceActionContext.

codeunit 71302 "Integration Operations"
{
    procedure GetCustomerDisplayName(customerNumber: Code[20]): Text[100]
    var
        Customer: Record Customer;
    begin
        if customerNumber = '' then
            Error('Customer number must contain a value.');

        if not Customer.Get(customerNumber) then
            Error('Customer %1 does not exist.', customerNumber);

        exit(Customer.Name);
    end;
}

After an administrator publishes the codeunit with the service name IntegrationOperations, invoke the procedure with POST. The route joins the service and procedure names with an underscore.

POST {odataBaseUrl}/ODataV4/IntegrationOperations_GetCustomerDisplayName?company={companyId}
Authorization: Bearer {token}
Content-Type: application/json

{
  "customerNumber": "10000"
}

An unbound codeunit action is an OData V4 endpoint, not a custom REST API endpoint. Use it for a reusable operation that doesn't need entity context. Its return value appears in the OData response's value property.

Design the operation contract

Treat each action as a versioned integration contract even though the route doesn't contain a separate action version.

  • Use stable procedure and parameter names.
  • Validate required values before changing data.
  • Return a predictable scalar result or action result code.
  • Use errors that identify the invalid input or failed business rule without exposing secrets.
  • Assign the minimum table-data and object permissions required by the operation.
  • Keep user-interface interactions out of service code because web service sessions are noninteractive.

Use ordinary CRUD requests when they express the operation clearly. Reserve actions for business behavior such as posting, approval, copying, or an operation that must validate and update an entity as one command.

For implementation and request details, see Create and interact with an OData V4 bound action and Create and interact with an OData V4 unbound action.