How do I get all my variants from the API? The query only gives me 250 and not the 1300

How do I get all my API variants? The query only gives me 250 and not all 1300.

I have a problem with my code in AL for Business Central. What I am trying to do is to get from the shopify API all my variants that are a total of 1300 and insert them into a table created in Business Central.
When I run the code if you do the first query to the API and brings me only 50 variants which is what by default limits the API itself.

I tried doing the pagination but I was unsuccessful, I get the following status 0 error:
Status error 0

My code is as follows:

procedure InsertVariants()
var
    Client: HttpClient;
    Request: HttpRequestMessage;
    Response: HttpResponseMessage;
    Content: HttpContent;
    JsonResponse: Text;
    JsonObject: JsonObject;
    JsonArray: JsonArray;
    JsonToken: JsonToken;
    VariantObject: JsonObject;
    TempToken: JsonToken;
    AccessToken: Text;
    Url: Text;
    ShopifyVariantRecord: Record "Shopify Variants Mercalab";
    VariantIdText: Text;
    VariantIdInteger: BigInteger;
    ProductIdText: Text;
    ProductIdInteger: BigInteger;
    PriceText: Text;
    PriceDecimal: Decimal;
    CompareAtPriceText: Text;
    CompareAtPriceDecimal: Decimal;
    Sku: Text;
    Price: Decimal;
    CompareAtPrice: Decimal;
    CompareAtPriceBoolean: Boolean;
    ShopifyVariantesOriginal: Record "Shpfy Variant";
    NextPageUrl: Text;
    BaseUrl: Text;
    Shopify : Record "Shpfy Company";
begin
    ShopifyVariantRecord.DeleteAll();
    BaseUrl := 'https://shoptest.myshopify.com/admin/api/2024-04/variants.json';
    NextPageUrl := BaseUrl;

    repeat
        Request.SetRequestUri(NextPageUrl);
        Request.Method := 'GET';
        Client.DefaultRequestHeaders.Clear();
        Client.DefaultRequestHeaders.Add('X-Shopify-Access-Token', Shopify.Token);

        if Client.Send(Request, Response) then begin
            Content := Response.Content();
            Content.ReadAs(JsonResponse);
            JsonObject.ReadFrom(JsonResponse);
            if JsonObject.Get('variants', JsonToken) then begin
                JsonArray := JsonToken.AsArray();
                foreach JsonToken in JsonArray do begin
                    if JsonToken.IsObject() then begin
                        VariantObject := JsonToken.AsObject();

                        VariantObject.Get('id', TempToken);
                        VariantIdText := TempToken.AsValue().AsText();
                        EVALUATE(VariantIdInteger, VariantIdText);
                        if VariantIdInteger < 0 then
                            VariantIdInteger := 0;

                        VariantObject.Get('product_id', TempToken);
                        ProductIdText := TempToken.AsValue().AsText();
                        EVALUATE(ProductIdInteger, ProductIdText);
                        if ProductIdInteger < 0 then
                            ProductIdInteger := 0;

                        VariantObject.Get('price', TempToken);
                        PriceText := TempToken.AsValue().AsText();
                        EVALUATE(PriceDecimal, PriceText);
                        if PriceDecimal < 0 then
                            PriceDecimal := 0;

                        VariantObject.Get('compare_at_price', TempToken);
                        CompareAtPriceBoolean := TempToken.AsValue().IsNull();
                        if CompareAtPriceBoolean then
                            CompareAtPriceDecimal := 0
                        else begin
                            CompareAtPriceText := TempToken.AsValue().AsText();
                            EVALUATE(CompareAtPriceDecimal, CompareAtPriceText);
                            if CompareAtPriceDecimal < 0 then
                                CompareAtPriceDecimal := 0;
                        end;

                        VariantObject.Get('sku', TempToken);
                        Sku := TempToken.AsValue().AsText();
                        ShopifyVariantesOriginal.SetRange(SKU, Sku);
                        if ShopifyVariantesOriginal.FindSet() then begin
                            ShopifyVariantRecord.Init();
                            ShopifyVariantRecord.id := VariantIdInteger;
                            ShopifyVariantRecord.product_id := ProductIdInteger;
                            ShopifyVariantRecord.price := PriceDecimal;
                            ShopifyVariantRecord.compare_at_price := CompareAtPriceDecimal;
                            ShopifyVariantRecord.sku := Sku;
                            ShopifyVariantRecord.Coincide := (VariantIdInteger = ShopifyVariantesOriginal.Id);
                            ShopifyVariantRecord."Id variante BC" := ShopifyVariantesOriginal.Id;
                            ShopifyVariantRecord.Insert();
                        end else begin
                            ShopifyVariantRecord.Init();
                            ShopifyVariantRecord.id := VariantIdInteger;
                            ShopifyVariantRecord.product_id := ProductIdInteger;
                            ShopifyVariantRecord.price := PriceDecimal;
                            ShopifyVariantRecord.compare_at_price := CompareAtPriceDecimal;
                            ShopifyVariantRecord.sku := Sku;
                            ShopifyVariantRecord.Coincide := (VariantIdInteger = ShopifyVariantesOriginal.Id);
                            ShopifyVariantRecord."Id variante BC" := ShopifyVariantesOriginal.Id;
                            ShopifyVariantRecord.Insert();
                        end;
                    end;
                end;

                NextPageUrl := GetNextPageUrl(Response);
            end else
                Message('No variants were found in the response.');
        end else begin
            Message('Error when querying Shopify API: %1', Format(Response.HttpStatusCode));
            NextPageUrl := '';
        end;
    until NextPageUrl = '';
    Message('%1 Shopify variants were saved.', ShopifyVariantRecord.Count());
end;

procedure GetNextPageUrl(Response: HttpResponseMessage): Text
var
    LinkHeader: List of [Text];
    LinkParts: List of [Text];
    LinkPart: Text;
begin
    if Response.Headers.Contains('Link') then begin
        Response.Headers.GetValues('Link', LinkHeader);
        foreach LinkPart in LinkHeader do begin
            LinkParts := LinkPart.Split(',');
            foreach LinkPart in LinkParts do begin
                if LinkPart.Contains('rel="next"') then begin
                    exit(LinkPart.Substring(LinkPart.IndexOf('<') + 1, LinkPart.IndexOf('>') - LinkPart.IndexOf('<') - 1));
                end;
            end;
        end;
    end;
    exit('');
end;

New contributor

JulioSistemas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật