Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ codeunit 30284 "Shpfy Company Export"
Shop: Record "Shpfy Shop";
CompanyAPI: Codeunit "Shpfy Company API";
CatalogAPI: Codeunit "Shpfy Catalog API";
MetafieldAPI: Codeunit "Shpfy Metafield API";
SkippedRecord: Codeunit "Shpfy Skipped Record";
CreateCustomers: Boolean;
CountyCodeTooLongLbl: Label 'Can not export customer %1 %2. The length of the string is %3, but it must be less than or equal to %4 characters. Value: %5, field: %6', Comment = '%1 - Customer No., %2 - Customer Name, %3 - Length, %4 - Max Length, %5 - Value, %6 - Field Name';
Expand Down Expand Up @@ -213,7 +212,6 @@ codeunit 30284 "Shpfy Company Export"
Shop := ShopifyShop;
CompanyAPI.SetShop(Shop);
CatalogAPI.SetShop(Shop);
MetafieldAPI.SetShop(Shop);
end;

local procedure UpdateShopifyCompany(Customer: Record Customer; CompanyId: BigInteger)
Expand Down Expand Up @@ -252,8 +250,10 @@ codeunit 30284 "Shpfy Company Export"
end;

local procedure UpdateMetafields(ComppanyId: BigInteger)
var
Metafields: Codeunit "Shpfy Metafields";
begin
MetafieldAPI.CreateOrUpdateMetafieldsInShopify(Database::"Shpfy Company", ComppanyId);
Metafields.SyncMetafieldsToShopify(Database::"Shpfy Company", ComppanyId, Shop.Code);
end;

internal procedure SetCreateCompanies(NewCustomers: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ codeunit 30116 "Shpfy Customer Export"
var
Shop: Record "Shpfy Shop";
CustomerApi: Codeunit "Shpfy Customer API";
MetafieldAPI: Codeunit "Shpfy Metafield API";
SkippedRecord: Codeunit "Shpfy Skipped Record";
CreateCustomers: Boolean;
CountyCodeTooLongLbl: Label 'Can not export customer %1 %2. The length of the string is %3, but it must be less than or equal to %4 characters. Value: %5, field: %6', Comment = '%1 - Customer No., %2 - Customer Name, %3 - Length, %4 - Max Length, %5 - Value, %6 - Field Name';
Expand Down Expand Up @@ -228,7 +227,6 @@ codeunit 30116 "Shpfy Customer Export"
begin
Shop := ShopifyShop;
CustomerApi.SetShop(Shop);
MetafieldAPI.SetShop(Shop)
end;

/// <summary>
Expand Down Expand Up @@ -286,7 +284,9 @@ codeunit 30116 "Shpfy Customer Export"
end;

local procedure UpdateMetafields(CustomerId: BigInteger)
var
Metafields: Codeunit "Shpfy Metafields";
begin
MetafieldAPI.CreateOrUpdateMetafieldsInShopify(Database::"Shpfy Customer", CustomerId);
Metafields.SyncMetafieldsToShopify(Database::"Shpfy Customer", CustomerId, Shop.Code);
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,42 @@ codeunit 30316 "Shpfy Metafield API"
CommunicationMgt.SetShop(Shop);
end;

local procedure SetShop(ShopCode: Code[20])
begin
Shop.Get(ShopCode);
SetShop(Shop);
end;

#region To Shopify
internal procedure SyncMetafieldToShopify(var Metafield: Record "Shpfy Metafield"; ShopCode: Code[20]): BigInteger
var
UserErrorOnShopifyErr: Label 'Something went wrong while sending the metafield to Shopify. Check Shopify Log Entries for more details.';
GraphQuery: TextBuilder;
JResponse: JsonToken;
JMetafields: JsonArray;
JUserErrors: JsonArray;
JItem: JsonToken;
begin
SetShop(ShopCode);
CreateMetafieldQuery(Metafield, GraphQuery);
JResponse := UpdateMetafields(GraphQuery.ToText());

JsonHelper.GetJsonArray(JResponse, JUserErrors, 'data.metafieldsSet.userErrors');

if JUserErrors.Count() = 0 then begin
JsonHelper.GetJsonArray(JResponse, JMetafields, 'data.metafieldsSet.metafields');
JMetafields.Get(0, JItem);
exit(JsonHelper.GetValueAsBigInteger(JItem, 'legacyResourceId'));
end else
Error(UserErrorOnShopifyErr);
end;

internal procedure SyncMetafieldsToShopify(ParentTableNo: Integer; OwnerId: BigInteger; ShopCode: Code[20])
begin
SetShop(ShopCode);
CreateOrUpdateMetafieldsInShopify(ParentTableNo, OwnerId);
end;

/// <summary>
/// Creates or updates the metafields in Shopify.
/// </summary>
Expand Down Expand Up @@ -177,6 +212,12 @@ codeunit 30316 "Shpfy Metafield API"
DeleteUnusedMetafields(MetafieldIds);
end;

internal procedure GetMetafieldDefinitions(ParentTableNo: Integer; OwnerId: BigInteger; ShopCode: Code[20])
begin
SetShop(ShopCode);
GetMetafieldDefinitions(ParentTableNo, OwnerId);
end;

/// <summary>
/// Retrieves the metafield definitions from Shopify.
/// </summary>
Expand All @@ -186,7 +227,7 @@ codeunit 30316 "Shpfy Metafield API"
///</remarks>
/// <param name="ParentTableNo">Table id of the parent resource.</param>
/// <param name="OwnerId">Id of the parent resource.</param>
internal procedure GetMetafieldDefinitions(ParentTableNo: Integer; OwnerId: BigInteger)
local procedure GetMetafieldDefinitions(ParentTableNo: Integer; OwnerId: BigInteger)
var
Metafield: Record "Shpfy Metafield";
OwnerType: Enum "Shpfy Metafield Owner Type";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------

namespace Microsoft.Integration.Shopify;

/// <summary>
/// Provides functionality for managing Shopify metafields.
/// </summary>
codeunit 30418 "Shpfy Metafields"
{
Access = Public;

var
MetafieldAPI: Codeunit "Shpfy Metafield API";

/// <summary>
/// Retrieves the metafield definitions from Shopify for the specified resource.
/// </summary>
/// <param name="ParentTableNo">Table id of the parent resource (e.g., Database::"Shpfy Product").</param>
/// <param name="OwnerId">Id of the parent resource in Shopify.</param>
/// <param name="ShopCode">The code of the Shopify shop.</param>
procedure GetMetafieldDefinitions(ParentTableNo: Integer; OwnerId: BigInteger; ShopCode: Code[20])
begin
MetafieldAPI.GetMetafieldDefinitions(ParentTableNo, OwnerId, ShopCode);
end;

/// <summary>
/// Synchronizes a single metafield to Shopify, creating or updating it as needed.
/// </summary>
/// <param name="Metafield">The metafield record to synchronize to Shopify.</param>
/// <param name="ShopCode">The code of the Shopify shop.</param>
/// <returns>The Shopify Id of the metafield.</returns>
procedure SyncMetafieldToShopify(var Metafield: Record "Shpfy Metafield"; ShopCode: Code[20]): BigInteger
begin
exit(MetafieldAPI.SyncMetafieldToShopify(Metafield, ShopCode));
end;

/// <summary>
/// Synchronizes all metafields for the specified resource to Shopify.
/// Only metafields that have been updated in BC since the last sync will be sent.
/// </summary>
/// <param name="ParentTableNo">Table id of the parent resource (e.g., Database::"Shpfy Product").</param>
/// <param name="OwnerId">Id of the parent resource in Shopify.</param>
/// <param name="ShopCode">The code of the Shopify shop.</param>
procedure SyncMetafieldsToShopify(ParentTableNo: Integer; OwnerId: BigInteger; ShopCode: Code[20])
begin
MetafieldAPI.SyncMetafieldsToShopify(ParentTableNo, OwnerId, ShopCode);
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,36 @@ page 30163 "Shpfy Metafields"

trigger OnAction()
var
MetafieldAPI: Codeunit "Shpfy Metafield API";
Metafields: Codeunit "Shpfy Metafields";
ParentTableNo: Integer;
OwnerId: BigInteger;
begin
Evaluate(ParentTableNo, Rec.GetFilter("Parent Table No."));
Evaluate(OwnerId, Rec.GetFilter("Owner Id"));
MetafieldAPI.SetShop(Shop);
MetafieldAPI.GetMetafieldDefinitions(ParentTableNo, OwnerId);
Metafields.GetMetafieldDefinitions(ParentTableNo, OwnerId, Shop.Code);
end;
}
action(SyncToShopify)
{
ApplicationArea = All;
Caption = 'Sync to Shopify';
Image = Export;
ToolTip = 'Send the selected metafields to Shopify.';
Visible = IsPageEditable;
Promoted = true;
PromotedOnly = true;
PromotedCategory = Process;

trigger OnAction()
var
Metafield: Record "Shpfy Metafield";
Metafields: Codeunit "Shpfy Metafields";
begin
CurrPage.SetSelectionFilter(Metafield);
if Metafield.FindSet() then
repeat
Metafields.SyncMetafieldToShopify(Metafield, Shop.Code);
until Metafield.Next() = 0;
end;
}
}
Expand Down Expand Up @@ -113,18 +135,19 @@ page 30163 "Shpfy Metafields"
Rec.TestField(Name);
Rec.Validate(Value);

Rec.Id := SendMetafieldToShopify();
Rec.Id := Metafields.SyncMetafieldToShopify(Rec, Shop.Code);
end;

trigger OnModifyRecord(): Boolean
begin
if Rec.Id < 0 then
if xRec.Value <> Rec.Value then
Rec.Rename(SendMetafieldToShopify());
Rec.Rename(Metafields.SyncMetafieldToShopify(Rec, Shop.Code));
end;

var
Shop: Record "Shpfy Shop";
Metafields: Codeunit "Shpfy Metafields";
IsPageEditable: Boolean;
IsValueEditable: Boolean;

Expand All @@ -149,29 +172,4 @@ page 30163 "Shpfy Metafields"
CurrPage.SetTableView(Metafield);
CurrPage.RunModal();
end;

local procedure SendMetafieldToShopify(): BigInteger
var
JsonHelper: Codeunit "Shpfy Json Helper";
MetafieldAPI: Codeunit "Shpfy Metafield API";
UserErrorOnShopifyErr: Label 'Something went wrong while sending the metafield to Shopify. Check Shopify Log Entries for more details.';
GraphQuery: TextBuilder;
JResponse: JsonToken;
JMetafields: JsonArray;
JUserErrors: JsonArray;
JItem: JsonToken;
begin
MetafieldAPI.SetShop(Shop);
MetafieldAPI.CreateMetafieldQuery(Rec, GraphQuery);
JResponse := MetafieldAPI.UpdateMetafields(GraphQuery.ToText());

JsonHelper.GetJsonArray(JResponse, JUserErrors, 'data.metafieldsSet.userErrors');

if JUserErrors.Count() = 0 then begin
JsonHelper.GetJsonArray(JResponse, JMetafields, 'data.metafieldsSet.metafields');
JMetafields.Get(0, JItem);
exit(JsonHelper.GetValueAsBigInteger(JItem, 'legacyResourceId'));
end else
Error(UserErrorOnShopifyErr);
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ permissionset 30104 "Shpfy - Objects"
codeunit "Shpfy Log Entries Delete" = X,
codeunit "Shpfy Math" = X,
codeunit "Shpfy Metafield API" = X,
codeunit "Shpfy Metafields" = X,
codeunit "Shpfy Metafield Owner Company" = X,
codeunit "Shpfy Metafield Owner Customer" = X,
codeunit "Shpfy Metafield Owner Product" = X,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ codeunit 30177 "Shpfy Product Events"
begin
end;

/// <summary>
/// Raised before updating product metafields in Shopify.
/// </summary>
/// <param name="ProductId">The Shopify product Id.</param>
[IntegrationEvent(false, false)]
internal procedure OnBeforeUpdateProductMetafields(ProductId: BigInteger)
begin
end;

#pragma warning disable AS0027
/// <summary>
/// Raised After Find Item Template.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ codeunit 30178 "Shpfy Product Export"
ProductEvents: Codeunit "Shpfy Product Events";
ProductPriceCalc: Codeunit "Shpfy Product Price Calc.";
VariantApi: Codeunit "Shpfy Variant API";
MetafieldAPI: Codeunit "Shpfy Metafield API";
SkippedRecord: Codeunit "Shpfy Skipped Record";
OnlyUpdatePrice: Boolean;
RecordCount: Integer;
Expand Down Expand Up @@ -578,7 +577,6 @@ codeunit 30178 "Shpfy Product Export"
ProductApi.SetShop(Shop);
VariantApi.SetShop(Shop);
ProductPriceCalc.SetShop(Shop);
MetafieldAPI.SetShop(Shop);
end;

/// <summary>
Expand Down Expand Up @@ -765,17 +763,20 @@ codeunit 30178 "Shpfy Product Export"
local procedure UpdateMetafields(ProductId: BigInteger)
var
ShpfyVariant: Record "Shpfy Variant";
Metafields: Codeunit "Shpfy Metafields";
begin
if OnlyUpdatePrice then
exit;

MetafieldAPI.CreateOrUpdateMetafieldsInShopify(Database::"Shpfy Product", ProductId);
ProductEvents.OnBeforeUpdateProductMetafields(ProductId);

Metafields.SyncMetafieldsToShopify(Database::"Shpfy Product", ProductId, Shop.Code);

ShpfyVariant.SetRange("Product Id", ProductId);
ShpfyVariant.ReadIsolation := IsolationLevel::ReadCommitted;
if ShpfyVariant.FindSet() then
repeat
MetafieldAPI.CreateOrUpdateMetafieldsInShopify(Database::"Shpfy Variant", ShpfyVariant.Id);
Metafields.SyncMetafieldsToShopify(Database::"Shpfy Variant", ShpfyVariant.Id, Shop.Code);
until ShpfyVariant.Next() = 0;
end;

Expand Down
Loading