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:
rscn4hw6cme0.png



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;
Sign In or Register to comment.