web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

JSON Updates in BC

(1) ShareShare
ReportReport
Posted on by 729
I have the following code that calls a URL to return JSON data, and I am populating a date field from the results in BC.  Code is below.  Works great when the URL finds a match, but when it does not, I get this error:
 
I am trying to code a scenario when the URL does not return the correct data, it defaults to "Please call for ETA".  I have it working for when the date field is null, but cannot figure out how to identify situations where there is no date result. Where should I trap this issue?
 
                trigger OnAction()
                var
                    APIKey: Text;
                    SeaRatesURL: Text;
                    URL1: Text;
                    URL2: Text;
                    URL3: Text;
                    Client: HttpClient;
                    Response: HttpResponseMessage;
                    SeaRatesJson: JsonObject;
                    ResponseText: Text;
                    SalesHeader: Record "Sales Header";
                    DocumentType: Enum "Sales Document Type";
                    ContainerNbr: Text[20];
                    SteamshipCode: Text[20];
                    ETADate: Text[20];
                begin
                    APIKey := '123456';
                    URL1 := 'https://tracking.searates.com/tracking?number=';
                    URL2 := 'api_key=';
                    url3 := 'sealine=';
                    SalesHeader.SetRange("Document Type", DocumentType::Order);
                    SalesHeader.SetFilter("Container Number", '<>%1', '');
                    SalesHeader.SetFilter("ETA to Port", '<>%1', '');
                    if SalesHeader.Findset() then begin
                        repeat
                            ContainerNbr := SalesHeader."Container Number";
                            SteamshipCode := SalesHeader."Steamship Code";
                            SeaRatesURL := URL1 + ContainerNbr + '&' + URL2 + APIKey + '&' + URL3 + SteamshipCode;
                            Client.Get(SeaRatesURL, Response);
                            if Response.IsSuccessStatusCode() then begin
                                Response.Content.ReadAs(ResponseText);
                                SeaRatesJson.ReadFrom(ResponseText);
                                ETADate := GetETADate(SeaRatesJson.AsToken());
                                SalesHeader.Validate("ETA to Port", ETADate);
                                SalesHeader.Modify(true)
                            end
                            else begin
                                //     error('API returned %1', Response.HttpStatusCode);
                                SalesHeader.Validate("ETA to Port", 'Please call for ETA');
                                SalesHeader.Modify(true)
                            end;
                        until SalesHeader.Next() = 0;
                    end
                end;
            }
        }
    }
    procedure GetETADate(ContainerData: JsonToken) Date: Text
    var
        query: Text;
        dateToken: JsonToken;
        DateValue: JsonValue;
        DateText: Text;
        IsNull: Boolean;
    begin
        query := '$.data.route.pod.date';
        ContainerData.SelectToken(query, dateToken);
        if dateToken.IsValue() then begin
            DateValue := dateToken.AsValue();
            IsNull := DateValue.IsNull;
            if IsNull then
                Date := 'Please call for ETA'
            else
                Date := dateToken.AsValue().AsText();
        end
    end;
}

 
I have the same question (0)
  • Suggested answer
    Dhiren Nagar Profile Picture
    2,286 Super User 2026 Season 1 on at
    Hi,
     
    I thinl the error you are getting is in the Peocedure GetETADate in the line of ContainerData.SelectToken(query, dateToken);.
     
    You basically need to handle with if condition. Just use an "If" condition and in the else write your custom error message (Please call for ETA) to user. You can also assign it to date variable just like you did for date null value.
     
    Another way is to make the entire procedure a try function and in all condition in the procedure exit false.
    This way you can handle the entire procedure as well.
     
    Tag me if you need any more help.

    ✅ Tick the checkbox below to mark the answer as verified, if it helped resolve your question.

    Regards,
    Dhiren.
     
     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,296 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,174 Super User 2026 Season 1

#3
Dhiren Nagar Profile Picture

Dhiren Nagar 1,034 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans