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

juliosistemas
Member Posts: 3
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:

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;
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:

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;
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions