Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

Error message appears in the attached image when I post in the Sales Order (Send XML)

(1) ShareShare
ReportReport
Posted on by 70
Hi
Please help me solve the problem of sending (XML) An error message appears in the attached image when I post in the Sales Order screen
 
 
 
Code AL :
========================================================================
 
pageextension 50114 "SalesOrderExt" extends "Sales Order"
{
    actions
    {
        modify(Post)
        {
            trigger OnAfterAction()
            var
                HttpClient: HttpClient;
                HttpResponse: HttpResponseMessage;
                RequestContent: HttpContent;
                ResponseText: Text;
                JsonBody: Text;
                EncodedXML: Text;
                HttpHeaders: HttpHeaders;
                Parts: List of [Text];
                Part: Text;
 
                SalesInvoiceHeader: Record "Sales Invoice Header";
                SalesInvoiceLine: Record "Sales Invoice Line";
                CompanyInfo: Record "Company Information";
                CustomerInfo: Record "Customer";
                ConnectingDevicesTax_JOD: Record "ConnectingDevicesTax_JOD";
                APIResponseLog: Record "API Response Log";
 
                Posted_InvoiceID: Text;
                Posted_Document_Date: Date;
                Posted_TaxNumberCompany: Text;
                Posted_CompanyName: Text;
                Posted_TaxNumberCustomer: Text;
                Posted_CustomerName: Text;
                Posted_TotalDiscountHeader: Decimal;
                Posted_TotalInvoice: Decimal;
                Posted_TotalBalance: Decimal;
                Posted_TotalVat: Decimal;
                Posted_CurrencyCode: Text;
                Posted_InputNumberSequence: Text;
                Posted_RowNumber: Integer;
                InvoiceAreaDevelopmentCode: Text;
                BARCODE_VALUE_Invoice: Text;
            begin
               
                if Rec."Document Type" = Rec."Document Type"::Order then begin
                    SalesInvoiceHeader.SetRange("Order No.", Rec."No.");
                    if SalesInvoiceHeader.FindFirst() then begin
                        Posted_InvoiceID := SalesInvoiceHeader."No.";
                        Posted_CurrencyCode := SalesInvoiceHeader."Currency Code";
                        Posted_Document_Date := SalesInvoiceHeader."Document Date";
 
                       
                        CompanyInfo.FindFirst();
                        Posted_TaxNumberCompany := CompanyInfo."VAT Registration No.";
                        Posted_CompanyName := CompanyInfo.Name;
 
                       
                        CustomerInfo.SetRange("No.", SalesInvoiceHeader."Sell-to Customer No.");
                        if CustomerInfo.FindFirst() then begin
                            Posted_TaxNumberCustomer := CustomerInfo."VAT Registration No.";
                            Posted_CustomerName := CustomerInfo.Name;
                        end;
 
                       
                        ConnectingDevicesTax_JOD.SetRange(TypeForTax, 'GeneralSalesInvoice');
                        if ConnectingDevicesTax_JOD.FindFirst() then begin
                            Posted_InputNumberSequence := ConnectingDevicesTax_JOD.InputNumberSequence;
                            Posted_RowNumber := ConnectingDevicesTax_JOD.RowNumber + 1;
                            ConnectingDevicesTax_JOD.RowNumber := Posted_RowNumber;
                            ConnectingDevicesTax_JOD.Modify();
                        end;
 
                       
                        CalculateInvoiceTotals(SalesInvoiceHeader, Posted_TotalDiscountHeader, Posted_TotalInvoice, Posted_TotalBalance, Posted_TotalVat);
 
                       
                        JsonBody := CreateInvoiceXML(SalesInvoiceHeader, SalesInvoiceLine, Posted_InvoiceID, Posted_Document_Date, Posted_CurrencyCode, Posted_TaxNumberCompany,                                                          Posted_CompanyName, Posted_TaxNumberCustomer, Posted_CustomerName);
 
                       
                        LogXMLToFile(JsonBody, 'C:\EInvoiceTest\InvoiceXML.txt');
 
                       
                        EncodedXML := EncodeToBase64(JsonBody);
 
                       
                        Parts := SplitString(EncodedXML, 2048);
 
                        foreach Part in Parts do begin
                           
                            JsonBody := '{' +
                                '"invoice": "' + Part + '"' +
                                '}';
 
                           
                            RequestContent.WriteFrom(JsonBody);
                            RequestContent.GetHeaders(HttpHeaders);
                            HttpHeaders.Clear();
                            HttpHeaders.Add('Content-Type', 'application/json');
 
                            HttpClient.DefaultRequestHeaders.Add('Client-Id', ConnectingDevicesTax_JOD.ClinetID);
                            HttpClient.DefaultRequestHeaders.Add('Secret-Key', ConnectingDevicesTax_JOD.SecretKey);
 
                           
                            if not HttpClient.Post('https://backend.jofotara.gov.jo/core/invoices/', RequestContent, HttpResponse) then
                                Error('Failed to call the API.');
 
                            HttpResponse.Content.ReadAs(ResponseText);
                            BARCODE_VALUE_Invoice := ResponseText;
 
                           
                            LogAPIResponse(APIResponseLog, ResponseText);
 
                            Message('Part Sent - Response: %1', ResponseText);
                            SaveBarcodeValue(Posted_InvoiceID, BARCODE_VALUE_Invoice);
                        end;
                    end;
                end;
            end;
        }
    }
 
    procedure CalculateInvoiceTotals(SalesInvoiceHeader: Record "Sales Invoice Header"; var Posted_TotalDiscountHeader: Decimal; var Posted_TotalInvoice: Decimal; var Posted_TotalBalance: Decimal; var Posted_TotalVat: Decimal)
    var
        SalesInvoiceLine: Record "Sales Invoice Line";
    begin
        SalesInvoiceLine.SetRange("Document No.", SalesInvoiceHeader."No.");
        if SalesInvoiceLine.FindSet() then begin
            repeat
                Posted_TotalDiscountHeader += (SalesInvoiceLine."Line Amount") - SalesInvoiceLine."Line Discount Amount";
                Posted_TotalInvoice += SalesInvoiceLine.GetLineAmountInclVAT();
                Posted_TotalBalance += (SalesInvoiceLine."Quantity" * SalesInvoiceLine."Unit Price");
                Posted_TotalVat += SalesInvoiceLine.GetLineAmountInclVAT() - SalesInvoiceLine."Line Amount";
            until SalesInvoiceLine.Next() = 0;
        end;
    end;
 
    procedure CreateInvoiceXML(SalesInvoiceHeader: Record "Sales Invoice Header"; SalesInvoiceLine: Record "Sales Invoice Line"; Posted_InvoiceID: Text; Posted_Document_Date: Date; Posted_CurrencyCode: Text; Posted_TaxNumberCompany: Text; Posted_CompanyName: Text; Posted_TaxNumberCustomer: Text; Posted_CustomerName: Text): Text
    var
        XmlText: Text;
    begin
        XmlText := '<?xml version="1.0" encoding="UTF-8"?>' +
            '<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"' +
            ' xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"' +
            ' xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">' +
            '<cbc:ID>' + Posted_InvoiceID + '</cbc:ID>' +
            '<cbc:IssueDate>' + Format(Posted_Document_Date, 0, '<Year4>-<Month,2>-<Day,2>') + '</cbc:IssueDate>' +
            '<cbc:InvoiceTypeCode>388</cbc:InvoiceTypeCode>' +
            '<cbc:DocumentCurrencyCode>' + Posted_CurrencyCode + '</cbc:DocumentCurrencyCode>' +
            '<cbc:TaxCurrencyCode>' + Posted_CurrencyCode + '</cbc:TaxCurrencyCode>' +
            '<cac:AccountingSupplierParty>' +
                '<cac:Party>' +
                    '<cac:PartyIdentification>' +
                        '<cbc:ID>' + Posted_TaxNumberCompany + '</cbc:ID>' +
                    '</cac:PartyIdentification>' +
                    '<cac:PartyName>' +
                        '<cbc:Name>' + Posted_CompanyName + '</cbc:Name>' +
                    '</cac:PartyName>' +
                '</cac:Party>' +
            '</cac:AccountingSupplierParty>' +
            '<cac:AccountingCustomerParty>' +
                '<cac:Party>' +
                    '<cac:PartyIdentification>' +
                        '<cbc:ID>' + Posted_TaxNumberCustomer + '</cbc:ID>' +
                    '</cac:PartyIdentification>' +
                    '<cac:PartyName>' +
                        '<cbc:Name>' + Posted_CustomerName + '</cbc:Name>' +
                    '</cac:PartyName>' +
                '</cac:Party>' +
            '</cac:AccountingCustomerParty>';
 
        // إضافة بنود الفاتورة (Line Data)
        SalesInvoiceLine.SetRange("Document No.", SalesInvoiceHeader."No.");
        if SalesInvoiceLine.FindSet() then begin
            repeat
                XmlText += '<cac:InvoiceLine>' +
                    '<cbc:ID>' + Format(SalesInvoiceLine."Line No.") + '</cbc:ID>' +
                    '<cbc:InvoicedQuantity unitCode="PCE">' + Format(SalesInvoiceLine.Quantity) + '</cbc:InvoicedQuantity>' +
                    '<cbc:LineExtensionAmount currencyID="' + Posted_CurrencyCode + '">' + Format(SalesInvoiceLine."Line Amount") + '</cbc:LineExtensionAmount>' +
                    '<cac:Item>' +
                        '<cbc:Name>' + SalesInvoiceLine.Description + '</cbc:Name>' +
                    '</cac:Item>' +
                    '<cac:Price>' +
                        '<cbc:PriceAmount currencyID="' + Posted_CurrencyCode + '">' + Format(SalesInvoiceLine."Unit Price") + '</cbc:PriceAmount>' +
                    '</cac:Price>' +
                '</cac:InvoiceLine>';
            until SalesInvoiceLine.Next() = 0;
        end;
 
        XmlText += '</Invoice>';
        exit(XmlText);
    end;
 
    procedure LogAPIResponse(var APIResponseLog: Record "API Response Log"; ResponseText: Text)
    begin
        APIResponseLog.Init();
        APIResponseLog."Response Text" := ResponseText;
        APIResponseLog."Date Time" := CurrentDateTime;
        APIResponseLog.Insert();
    end;
 
    procedure EncodeToBase64(InputText: Text): Text
    var
        Base64Convert: Codeunit "Base64 Convert";
    begin
        exit(Base64Convert.ToBase64(InputText));
    end;
 
    procedure SplitString(InputText: Text; MaxLength: Integer): List of [Text]
    var
        Parts: List of [Text];
        StartIndex: Integer;
        EndIndex: Integer;
    begin
        StartIndex := 1;
        while StartIndex <= StrLen(InputText) do begin
            EndIndex := StartIndex + MaxLength - 1;
            if EndIndex > StrLen(InputText) then
                EndIndex := StrLen(InputText);
            Parts.Add(CopyStr(InputText, StartIndex, EndIndex - StartIndex + 1));
            StartIndex := EndIndex + 1;
        end;
        exit(Parts);
    end;
 
    procedure LogXMLToFile(XMLContent: Text; FileName: Text)
    var
        FileMgt: Codeunit "File Management";
        TempBlob: Codeunit "Temp Blob";
        OutStream: OutStream;
    begin
        TempBlob.CreateOutStream(OutStream);
        OutStream.WriteText(XMLContent);
        FileMgt.BLOBExport(TempBlob, FileName, true);
    end;
 
    procedure SaveBarcodeValue(RecIID: Text[20]; BARCODE: Text[1000])
    var
        SaveQRCodeReceived: Record "Save_QR_Code_Received";
    begin
        if (RecIID = '') or (BARCODE = '') then begin
            Error('Record ID or BARCODE is empty.');
        end else begin
            SaveQRCodeReceived.Init();
            SaveQRCodeReceived."ID" := SaveQRCodeReceived.Count() + 1;
            SaveQRCodeReceived.Invoice_Type := 'Invoice';
            SaveQRCodeReceived."Invoice_NO" := RecIID;
            SaveQRCodeReceived.QR_Code_JO_Insert_Date := Today;
            SaveQRCodeReceived."QR_Code_JO" := BARCODE;
            SaveQRCodeReceived.Insert();
        end;
        Commit();
    end;
}
 
========================================================================
 
I hope to get help from those with experience, as I have tried more than one method and it did not work.
 
 
 
 
 
 
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 74,280 Super User 2024 Season 2 on at
    Error message appears in the attached image when I post in the Sales Order (Send XML)
    Hi, If you want to store it in a Blob field, I hope the following information can give you some hints.
     
    Thanks
    ZHU
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 5,611 on at
    Error message appears in the attached image when I post in the Sales Order (Send XML)
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 5,611 on at
    Error message appears in the attached image when I post in the Sales Order (Send XML)
    You have specified the maximum allowable length of 2048, but the value exceeds this length.
  • Suggested answer
    Ramiz Profile Picture
    Ramiz 402 on at
    Error message appears in the attached image when I post in the Sales Order (Send XML)
    Hi,

    The maximum length of Text type field at table is 2048 characters and you are trying to store more than that.

    I believe the error is at this line
     
    APIResponseLog."Response Text" := ResponseText;
    Before storing the response to any field at table use this 
     
    APIResponseLog."Response Text" := CopyStr(ResponseText,1,MaxStrLen(APIResponseLog."Response Text" ));
     
    If you want to store all the character then you have to change the type of the field to a Blob.

    Regards
    Ramiz
  • Alexander Drogin Profile Picture
    Alexander Drogin 16 on at
    Error message appears in the attached image when I post in the Sales Order (Send XML)
    Have you tried debugging this code to at least find which line throws the error?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,559 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,579 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans