0

I created a new page type of API that uses the Sales Invoice Header Source. I would like to get newly inserted Record and update its value. After that display that values in alert. Currently I have an empty page after I create new Sales Invoice.

This is my code:

page 50105 "Custom Invoice API" {
    PageType = API;
    Caption = 'Custom Sales Invoice Header API';
    APIPublisher = 'xy';
    APIVersion = 'beta';
    APIGroup = 'customsalesinvoiceheader';
    EntityName = 'salesInvoiceHeader';
    EntitySetName = 'salesInvoiceHeaders';
    SourceTable = "Sales Invoice Header";
    DelayedInsert = true;


    layout
    {
        area(Content)
        {
            repeater(GroupName)
            {
                field(id; "No.")
                {
                    Caption = 'No.';
                }
                field(name; "Sell-to Customer Name")
                {
                    Caption = 'Customer Name';
                }
                field(address; "Sell-to Address")
                {
                    Caption = 'Sell-to Adress';
                }
                field(shipdate; "Shipment Date")
                {
                    Caption = 'Shipment Date';
                }
            }
        }
    }
    trigger OnInsertRecord(BelowxRec: Boolean): Boolean
    begin
        Insert(true);
        Message(readInvoice());
        updateInvoice();
        exit(true);
    end;

    procedure readInvoice(): Text
    var
        currentInvoice: Record "Sales Invoice Header";
    begin
        exit(currentInvoice."No." + ' ' + currentInvoice."Sell-to Customer Name" + ' ' + currentInvoice."Sell-to Address" + ' ' + Format(currentInvoice."Shipment Date"));
    end;

    procedure updateInvoice()
    var
        currentInvoice: Record "Sales Invoice Header";
    begin
        currentInvoice."Shipment Date" := DMY2Date(19, 6, 2020);
    end; 
}

After that I created a new web service inside my BC of type query that targets Sales Invoice but in postman I get 401, no matter if I use basic authentication with my admin user or Web Service App Key I generated on my admin user.

How do I connect the insert trigger action to my API Custom Page ?

underscore_d
  • 6,309
  • 3
  • 38
  • 64
Jure Fadiga
  • 195
  • 2
  • 19

1 Answers1

1

There are few things wrong about your strategy, I'll list them here:

  • You don't need to put Insert(true); inside OnInsertRecord, when the execution is inside this trigger, a trigger action is planned to happen and you Insert the record again will generate an error.
  • You cannot use user interface like, Message(readInvoice()); inside an Api or Web Service, the message will return a Call Back error. You need a mechanism to get your answer back from the server and show it client side (in the front-end).
  • Main reason behind Api pages is using standard methods exposed by BC, you don't necessarily need to create your own back-end functions. I recommend to take a look at these articles: https://www.olisterr.tech/2019/05/creating-apis-in-business-central-1.html https://www.olisterr.tech/2019/09/understanding-apis-in-business-central.html

About the 401 error, it greatly depends on the fact that if you're using a public cloud BC (Hosted by MS), or a locally hosted/OnPrem configuration.

Babak
  • 46
  • 5
  • I am using cloud BC. – Jure Fadiga Jun 23 '20 at 05:49
  • check this answer: "Go to extension management, select your extension and from Manage menu, select Configure. In the Configure page, set AllowHttpCleintRequest to True." https://stackoverflow.com/questions/60153873/dynamics-httpclient-call-never-succeeds/62520915#62520915 it's possible that you forgot to give your extension HttpClient grant. – Babak Jun 23 '20 at 19:06