Share This Post

NAV Updates

Creating and Interacting With OData V4 Bound Action

This walkthrough illustrates how you can publish a Dynamics NAV function as OData V4 web service action.

About This Walkthrough

This walkthrough provides an overview of how to expose a function as a web service action and how to verify that the service is working as expected. The walkthrough illustrates the following tasks:

  • Publishing the Dynamics Navision function as a web service.
  • Verifying web service availability from a browser.

Prerequisites

To complete this walkthrough, you will need:

  • Microsoft Dynamics NAV 2018 CTP 10 with a developer license.
  • CRONUS International Ltd. demonstration database.
  • The Postman application for testing a web service URI.

Publishing a Function as a Web Service

webservice

webservice

You publish the function as a Web service action by using the Microsoft Dynamics NAV Development Environment to make the function and the Dynamics Navision Windows client or the Dynamics Navision Web client for publishing the objects the function is for. The tutorial offers an example that will return the location header. The location header would be used to later issue the get request for the resulting object. Refer to Return a Value section for an example that will return the value mentioned in your function.

To create the function

Create Copy action on the Sales Invoice page.

  1. Open Microsoft Dynamics Navision Development Environment and connect to the CRONUS International Ltd. company. Object Designer opens automatically in the development environment.
  2. Open page 43, Sales Invoice.
  3. Create the new function on a page named Copy.
  4. Open the properties for the function and set the properties to the below values.
    Property  Value
    Local  No
    FunctionVisibility  External
    ServiceEnabled  Yes
  5. Open Locals for the function and set the parameters to the below values.
    Parameter  Value
    VAR  Yes
    Name  ActionContext
    DataType  DotNet
    SubType  Microsoft.Dynamics.Nav.Runtime.WebServiceActionContext.’Microsoft.Dynamics.Nav.Ncl, Culture=neutral,  PublicKeyToken=31bf3856ad364e35′
  6. Choose the Variables tab and include in the below variables.
    Name  DataType  SubType
    ToSalesHeader  Record  36
    FromSalesHeader  Record  36
    SalesSetup  Record  311
    ODataActionManagement  Codeunit  6711
    CopyDocMgt  Codeunit  6620
    DocType  Option  OptionString = Quote,Blanket Order,Order,Invoice,Return Order,Credit  Memo,Posted Shipment,Posted Invoice,Posted Return Receipt,Posted Credit Memo

    1. Add the code that copies the sales document, for example.

    SalesSetup.GET;
    CopyDocMgt.SetProperties(
    TRUE,FALSE,FALSE,FALSE,FALSE,SalesSetup."Exact Cost Reversing Mandatory",FALSE);
    FromSalesHeader.GET(Rec."Document Type",Rec."No.");
    ToSalesHeader."Document Type" := FromSalesHeader."Document Type";
    ToSalesHeader.INSERT(TRUE);
    CopyDocMgt.CopySalesDoc(DocType::Invoice,FromSalesHeader."No.",ToSalesHeader);
    // Include the necessary keys of the newly created entity using that entity’s backing table to identify the field no. and it’s value.
    ODataActionManagement.AddKey(Rec.FIELDNO(Id),ToSalesHeader.Id);
    // Depending on what was the result of action
    // - Created: SetCreatedPageResponse(ActionContext, PAGE::"Sales Invoice");
    // - Updated: SetUpdatedPageResponse(ActionContext,PAGE::"Sales Invoice");
    // - Deleted: SetDeleteResponse(ActionContext);
    ODataActionManagement.SetCreatedPageResponse(ActionContext,PAGE::"Sales Invoice");
    
  7. Save and compile the SalesInvoice page.

To register and publish a page as the Web service

  1. Open the Dynamics NAV and connect to CRONUS International Ltd. company.
  2. In Search box, enter the Web services, and then select the related link.
  3. In Web Services page, on the Home tab, select the New.
  4. In the Object Type column, select Page. In the Object ID column, enter,43 and in the Service Name column, enter.SalesInvoice
  5. Select the checkbox in the Published column.
  6. Choose the OK button.

Verifying the Web Service Availability

After publishing the web service, verify that the port that the web service apps will use to connect to your web service is open. The default port for the OData V4 web services is 7047. You can configure this value by using the Microsoft Dynamics Navision Server Administration Tool.

To verify availability of Microsoft Dynamics NAV Web service action

Dynamics NAV function as OData V4 web service action

Microsoft Dynamics NAV Web service action

  1. Start Postman or other tools that can execute the POST command against the web service URI.
  2. In the Address field, enter a URI in this format:http://<Server>:<WebServicePort>/<ServerInstance>/api/beta/companies(<companyid>)/salesInvoices(<invoiceid>)/Microsoft.NAV.Copy)
    • <Server> is the name of the computer that is running the Dynamics Navision Server.
    • <WebServicePort> is the port that the OData V4 is running on. The default port is 7047.
    • <ServiceInstance> is the name of the Dynamics Navision Server instance for your solution. The default name is DynamicsNAV90.

    Example if the default Microsoft Dynamics NAV Server is running on your local computer. http://localhost:7047/DynamicsNAV/api/beta/companies(b9248a6e-966d-478c-a25d-d91d28610397)/salesInvoices(8cc52602-3aa4-4256-b2c7-fdfef5248cbf)/Microsoft.NAV.Copy)

  3. Postman should now show the web service function that you have published and performed the action of copying an invoice.

Return a value

  1. Open the Dynamics NAV Development Environment and connect to the app database.
  2. Open page 43, Sales Invoice.
  3. Create a new function called Example.
  4. Open the Properties for the function and set the properties to the below values.
    Property  Value
    Local  No
    FunctionVisibility  External
    ServiceEnabled  Yes
  5. Open Locals for the function parameters to the following values.
    Parameter  Value  test
    inParam  Text  test
  6. Choose to the Return Value tab and then add the below values.
    Name  ReturnType
    outParam  Text
  7. Then add the following code for the Example function:outParam := inParam + ' Completed';
  8. You can now issue a post request:
    http://localhost:7047/Navision_NAV/ODataV4/Company('CRONUS International Ltd.')/SalesInvoice('Invoice', '1004')/NAV.Example
    with a JSON body of:
    { "inParam": "Hello World" }
  9. The returned value will be returned in the body of the message.
    { "@odata.context": "http://localhost:7047/Navision_NAV/ODataV4/$metadata#Edm.String", "value": "Hello World Completed" }

Share This Post

Leave a Reply

avatar
  Subscribe  
Notify of
Skip to toolbar