Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Shopify] Customer/Company Metafields #27542

Merged
merged 25 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b4b8323
add import customer metafields import
petemchlk Oct 3, 2024
641b9b6
handle metafields removalwhen removed in Shopify
petemchlk Oct 4, 2024
2e391ee
remove old metafield related not working code, add code to update Cus…
petemchlk Oct 4, 2024
ddf0b67
add company metafield import
petemchlk Oct 4, 2024
753ab0c
Add metafield export support
petemchlk Oct 4, 2024
321fd5e
small refactors
petemchlk Oct 4, 2024
56a224d
add metafields action to customer/company card
petemchlk Oct 7, 2024
878d2ed
create test cu
petemchlk Oct 11, 2024
ce08405
create test codeunit
petemchlk Oct 14, 2024
38d4e03
add test
petemchlk Oct 14, 2024
9e97378
add comany tests, create helper codeunit
petemchlk Oct 14, 2024
bf65b47
Merge branch 'main' into dev/pmi/CustomerMetafields
petemchlk Oct 15, 2024
cc72822
remove empty spaces
petemchlk Oct 21, 2024
abf978e
pr fix, add page editable function to metafield owner interface
petemchlk Oct 21, 2024
33dee95
change procedure name and summary
petemchlk Oct 21, 2024
cfb0302
renumber objects
petemchlk Oct 22, 2024
b86173d
sending metafields test in progress
petemchlk Oct 22, 2024
7ae9575
refactor export metafields test
petemchlk Oct 22, 2024
6175567
add company export metafields test
petemchlk Oct 22, 2024
bdbdac2
renumber ids
petemchlk Oct 25, 2024
f316da3
updated return var documentation
petemchlk Oct 25, 2024
92c49c3
fix object id
petemchlk Oct 28, 2024
8484fd2
fix tooltips
petemchlk Oct 30, 2024
0594f9a
make metafields only edaitable when not imported before export
petemchlk Oct 31, 2024
36ca70d
object id fixes
petemchlk Oct 31, 2024
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
2 changes: 1 addition & 1 deletion Apps/W1/Shopify/app/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"idRanges": [
{
"from": 30100,
"to": 30370
"to": 30380
}
],
"internalsVisibleTo": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,10 @@ codeunit 30286 "Shpfy Company API"
internal procedure UpdateShopifyCompanyFields(var ShopifyCompany: Record "Shpfy Company"; JCompany: JsonObject) Result: Boolean
var
CompanyLocation: Record "Shpfy Company Location";
MetafieldAPI: Codeunit "Shpfy Metafield API";
UpdatedAt: DateTime;
JLocations: JsonArray;
JMetafields: JsonArray;
JItem: JsonToken;
OutStream: OutStream;
PhoneNo: Text;
Expand Down Expand Up @@ -343,5 +345,7 @@ codeunit 30286 "Shpfy Company API"
CompanyLocation."Tax Registration Id" := CopyStr(JsonHelper.GetValueAsText(JItem, 'node.taxRegistrationId', MaxStrLen(CompanyLocation."Tax Registration Id")), 1, MaxStrLen(CompanyLocation."Tax Registration Id"));
CompanyLocation.Modify();
end;
if JsonHelper.GetJsonArray(JCompany, JMetafields, 'metafields.edges') then
MetafieldAPI.UpdateMetafieldsFromShopify(JMetafields, Database::"Shpfy Company", ShopifyCompany.Id);
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ codeunit 30284 "Shpfy Company Export"
ShopifyCompany.Modify();
CompanyLocation.Modify();
end;

UpdateMetafields(ShopifyCompany.Id);
end;

local procedure UpdateMetafields(ComppanyId: BigInteger)
var
MetafieldAPI: Codeunit "Shpfy Metafield API";
begin
MetafieldAPI.CreateOrUpdateMetafieldsInShopify(Database::"Shpfy Company", ComppanyId);
end;

internal procedure SetCreateCompanies(NewCustomers: Boolean)
Expand Down
18 changes: 18 additions & 0 deletions Apps/W1/Shopify/app/src/Companies/Pages/ShpfyCompanies.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@ page 30156 "Shpfy Companies"
end;

}
action(Metafields)
{
ApplicationArea = All;
Caption = 'Metafields';
Image = PriceAdjustment;
Promoted = true;
PromotedCategory = Process;
PromotedIsBig = true;
PromotedOnly = true;
ToolTip = 'Add metafields to a company. This can be used for adding custom data fields to companies in Shopify.';

trigger OnAction()
var
Metafields: Page "Shpfy Metafields";
begin
Metafields.RunForResource(Database::"Shpfy Company", Rec.Id, Rec."Shop Code");
end;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ page 30157 "Shpfy Company Card"
RunPageLink = "Company SystemId" = field(SystemId);
ToolTip = 'View a list of Shopify catalogs for the company.';
}
action(Metafields)
{
ApplicationArea = All;
Caption = 'Metafields';
Image = PriceAdjustment;
Promoted = true;
PromotedCategory = Category4;
PromotedIsBig = true;
PromotedOnly = true;
ToolTip = 'Add metafields to a company. This can be used for adding custom data fields to compoanies in Shopify.';

trigger OnAction()
var
Metafields: Page "Shpfy Metafields";
begin
Metafields.RunForResource(Database::"Shpfy Company", Rec.Id, Rec."Shop Code");
end;
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,14 @@ codeunit 30114 "Shpfy Customer API"
internal procedure UpdateShopifyCustomerFields(var ShopifyCustomer: Record "Shpfy Customer"; JCustomer: JsonObject) Result: Boolean
var
CustomerAddress: Record "Shpfy Customer Address";
MetafieldAPI: Codeunit "Shpfy Metafield API";
NodeId: BigInteger;
UpdatedAt: DateTime;
JAddresses: JsonArray;
JTags: JsonArray;
JAddress: JsonObject;
JItem: JsonToken;
JMetafields: JsonArray;
Ids: List of [BigInteger];
OutStream: OutStream;
StateString: Text;
Expand Down Expand Up @@ -462,6 +464,9 @@ codeunit 30114 "Shpfy Customer API"
CustomerAddress.Modify(false);
end;
end;

if JsonHelper.GetJsonArray(JCustomer, JMetafields, 'metafields.edges') then
MetafieldAPI.UpdateMetafieldsFromShopify(JMetafields, Database::"Shpfy Customer", ShopifyCustomer.Id);
end;
end;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,6 @@ codeunit 30116 "Shpfy Customer Export"
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';

/// <summary>
/// Add Or Update Metadata.
/// </summary>
/// <param name="ShopifyCustomer">Parameter of type Record "Shopify Customer".</param>
/// <param name="MetadataFieldRef">Parameter of type FieldRef.</param>
internal procedure AddOrUpdateMetadata(ShopifyCustomer: Record "Shpfy Customer"; MetadataFieldRef: FieldRef)
var
Metafield: Record "Shpfy Metafield";
Name: Text;
begin
Metafield.SetRange("Parent Table No.", Database::"Shpfy Customer");
Metafield.SetRange("Owner Id", ShopifyCustomer.Id);
Metafield.SetRange(Namespace, 'Microsoft.Dynamics365.BusinessCentral');
Name := CleanName(MetadataFieldRef);
Metafield.SetRange(Name, Name);
if Metafield.FindFirst() then begin
if Metafield.Value <> Format(MetadataFieldRef.Value) then;
end else begin
Clear(Metafield);
Metafield.Namespace := 'Microsoft.Dynamics365.BusinessCentral';
Metafield.Validate("Parent Table No.", Database::"Shpfy Customer");
Metafield."Owner Id" := ShopifyCustomer.Id;
Metafield.Type := Metafield.Type::single_line_text_field;
Metafield.Value := Format(MetadataFieldRef.Value);
end;
end;

/// <summary>
/// Clean Name.
/// </summary>
/// <param name="FieldRef">Parameter of type FieldRef.</param>
/// <returns>Return value of type Text.</returns>
local procedure CleanName(FieldRef: FieldRef): Text
begin
exit(DelChr(FieldRef.Record().Name, '=', ' %.-+') + '.' + DelChr(FieldRef.Name, '=', ' %-+'));
end;

/// <summary>
/// Create Shopify Customer.
Expand All @@ -101,7 +65,8 @@ codeunit 30116 "Shpfy Customer Export"
ShopifyCustomer.Insert();
CustomerAddress.Insert();
end;
MetadataFields(Customer, ShopifyCustomer);

UpdateMetafields(ShopifyCustomer.Id);
end;

/// <summary>
Expand Down Expand Up @@ -224,25 +189,6 @@ codeunit 30116 "Shpfy Customer Export"
exit(true);
end;

/// <summary>
/// Metadata Fields.
/// </summary>
/// <param name="Customer">Parameter of type Record Customer.</param>
/// <param name="ShopifyCustomer">Parameter of type Record "Shopify Customer".</param>
local procedure MetadataFields(Customer: Record Customer; ShopifyCustomer: Record "Shpfy Customer")
var
RecordRef: RecordRef;
begin
RecordRef.GetTable(Customer);
AddOrUpdateMetadata(ShopifyCustomer, RecordRef.Field(Customer.FieldNo("No.")));
AddOrUpdateMetadata(ShopifyCustomer, RecordRef.Field(Customer.FieldNo("VAT Bus. Posting Group")));
AddOrUpdateMetadata(ShopifyCustomer, RecordRef.Field(Customer.FieldNo("VAT Registration No.")));
AddOrUpdateMetadata(ShopifyCustomer, RecordRef.Field(Customer.FieldNo(SystemId)));
AddOrUpdateMetadata(ShopifyCustomer, RecordRef.Field(Customer.FieldNo("Customer Disc. Group")));
AddOrUpdateMetadata(ShopifyCustomer, RecordRef.Field(Customer.FieldNo("Customer Price Group")));
AddOrUpdateMetadata(ShopifyCustomer, RecordRef.Field(Customer.FieldNo("Customer Posting Group")));
end;

/// <summary>
/// Set Shop.
/// </summary>
Expand Down Expand Up @@ -313,10 +259,19 @@ codeunit 30116 "Shpfy Customer Export"
ShopifyCustomer.Modify();
CustomerAddress.Modify();
end;

UpdateMetafields(ShopifyCustomer.Id);
end;

internal procedure SetCreateCustomers(NewCustomers: Boolean)
begin
CreateCustomers := NewCustomers;
end;

local procedure UpdateMetafields(CustomerId: BigInteger)
var
MetafieldAPI: Codeunit "Shpfy Metafield API";
begin
MetafieldAPI.CreateOrUpdateMetafieldsInShopify(Database::"Shpfy Customer", CustomerId);
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,27 @@ page 30106 "Shpfy Customer Card"
RunPageLink = "Customer Id" = Field(Id);
ToolTip = 'View a list of Shopify orders for the customer.';
}
action(Metafields)
{
ApplicationArea = All;
Caption = 'Metafields';
Image = PriceAdjustment;
Promoted = true;
PromotedCategory = Category4;
PromotedIsBig = true;
PromotedOnly = true;
ToolTip = 'Add metafields to a customer. This can be used for adding custom data fields to customers in Shopify.';

trigger OnAction()
var
Shop: Record "Shpfy Shop";
Metafields: Page "Shpfy Metafields";
begin
Shop.SetRange("Shop Id", Rec."Shop Id");
Shop.FindFirst();
Metafields.RunForResource(Database::"Shpfy Customer", Rec.Id, Shop.Code);
end;
}
}

}
Expand Down
20 changes: 20 additions & 0 deletions Apps/W1/Shopify/app/src/Customers/Pages/ShpfyCustomers.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,27 @@ page 30107 "Shpfy Customers"
BackgroundSyncs.CustomerSync(Shop.Code);
end;
end;
}
action(Metafields)
{
ApplicationArea = All;
Caption = 'Metafields';
Image = PriceAdjustment;
Promoted = true;
PromotedCategory = Process;
PromotedIsBig = true;
PromotedOnly = true;
ToolTip = 'Add metafields to a customer. This can be used for adding custom data fields to customers in Shopify.';

trigger OnAction()
var
Shop: Record "Shpfy Shop";
Metafields: Page "Shpfy Metafields";
begin
Shop.SetRange("Shop Id", Rec."Shop Id");
Shop.FindFirst();
Metafields.RunForResource(Database::"Shpfy Customer", Rec.Id, Shop.Code);
end;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ codeunit 30302 "Shpfy GQL Company" implements "Shpfy IGraphQL"
/// <returns>Return value of type Text.</returns>
internal procedure GetGraphQL(): Text
begin
exit('{"query":"{company(id: \"gid://shopify/Company/{{CompanyId}}\") {name id note createdAt updatedAt mainContact { id customer { id firstName lastName email phone}} locations(first:1, sortKey: CREATED_AT ) {edges { node { id name billingAddress {address1 address2 city countryCode phone province zip zoneCode} taxRegistrationId}}}}}"}');
exit('{"query":"{company(id: \"gid://shopify/Company/{{CompanyId}}\") {name id note createdAt updatedAt mainContact { id customer { id firstName lastName email phone}} locations(first:1, sortKey: CREATED_AT ) {edges { node { id name billingAddress {address1 address2 city countryCode phone province zip zoneCode} taxRegistrationId }}} metafields(first: 50) {edges {node {id namespace ownerType legacyResourceId key value type}}}}}"}');
end;

/// <summary>
Expand All @@ -22,6 +22,6 @@ codeunit 30302 "Shpfy GQL Company" implements "Shpfy IGraphQL"
/// <returns>Return value of type Integer.</returns>
internal procedure GetExpectedCost(): Integer
begin
exit(7);
exit(10);
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Microsoft.Integration.Shopify;

/// <summary>
/// Codeunit Shpfy GQL CompanyMetafieldIds (ID 30373) implements Interface Shpfy IGraphQL.
/// </summary>
codeunit 30373 "Shpfy GQL CompanyMetafieldIds" implements "Shpfy IGraphQL"
{
Access = Internal;

/// <summary>
/// GetGraphQL.
/// </summary>
/// <returns>Return value of type Text.</returns>
procedure GetGraphQL(): Text
begin
exit('{"query":"{company(id: \"gid://shopify/Company/{{CompanyId}}\") {metafields(first: 50) {edges {node {id namespace ownerType legacyResourceId }}}}}"}');
end;

/// <summary>
/// GetExpectedCost.
/// </summary>
/// <returns>Return value of type Integer.</returns>
procedure GetExpectedCost(): Integer
begin
exit(50);
end;

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ codeunit 30127 "Shpfy GQL Customer" implements "Shpfy IGraphQL"
/// <returns>Return value of type Text.</returns>
internal procedure GetGraphQL(): Text
begin
exit('{"query":"{customer(id: \"gid://shopify/Customer/{{CustomerId}}\") {legacyResourceId firstName lastName email phone taxExempt taxExemptions verifiedEmail state note createdAt updatedAt tags emailMarketingConsent {consentUpdatedAt marketingState} addresses {id company firstName lastName address1 address2 zip city countryCodeV2 country provinceCode province phone} defaultAddress {id} metafields(namespace: \"Microsoft.Dynamics365.BusinessCentral\" first: 10) {edges {node {id namespace ownerType legacyResourceId key value}}}}}"}');
exit('{"query":"{customer(id: \"gid://shopify/Customer/{{CustomerId}}\") {legacyResourceId firstName lastName email phone taxExempt taxExemptions verifiedEmail state note createdAt updatedAt tags emailMarketingConsent {consentUpdatedAt marketingState} addresses {id company firstName lastName address1 address2 zip city countryCodeV2 country provinceCode province phone} defaultAddress {id} metafields(first: 50) {edges {node {id namespace ownerType legacyResourceId key value type}}}}}"}');
end;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Microsoft.Integration.Shopify;
/// <summary>
/// Codeunit Shpfy GQL Customer Metafield Ids (ID 30374) implements Interface Shpfy IGraphQL.
/// </summary>
codeunit 30374 "Shpfy GQL CustomerMetafieldIds" implements "Shpfy IGraphQL"
{
Access = Internal;

/// <summary>
/// GetGraphQL.
/// </summary>
/// <returns>Return value of type Text.</returns>
procedure GetGraphQL(): Text
begin
exit('{"query":"{customer(id: \"gid://shopify/Customer/{{CustomerId}}\") { metafields(first: 50) {edges {node {legacyResourceId updatedAt}}}}}"}');
end;

/// <summary>
/// GetExpectedCost.
/// </summary>
/// <returns>Return value of type Integer.</returns>
procedure GetExpectedCost(): Integer
begin
exit(50);
end;

}
10 changes: 10 additions & 0 deletions Apps/W1/Shopify/app/src/GraphQL/Enums/ShpfyGraphQLType.Enum.al
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,14 @@ enum 30111 "Shpfy GraphQL Type" implements "Shpfy IGraphQL"
Caption = 'Get Product Image';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL GetProductImage";
}
value(103; CustomerMetafieldIds)
{
Caption = 'Customer Metafield Ids';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL CustomerMetafieldIds";
}
value(104; CompanyMetafieldIds)
{
Caption = 'Company Metafield Ids';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL CompanyMetafieldIds";
}
}
Loading
Loading