From 6f8b7b345124e46a9a5f0a094f55ae0b9a1797af Mon Sep 17 00:00:00 2001 From: Luuk Date: Tue, 6 Jun 2023 10:52:52 +0200 Subject: [PATCH 1/5] SetLoadFields --- .../docs/BestPractices/SetLoadFields/Index.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 content/docs/BestPractices/SetLoadFields/Index.md diff --git a/content/docs/BestPractices/SetLoadFields/Index.md b/content/docs/BestPractices/SetLoadFields/Index.md new file mode 100644 index 00000000..259ba178 --- /dev/null +++ b/content/docs/BestPractices/SetLoadFields/Index.md @@ -0,0 +1,44 @@ +--- +title: "SetLoadFields" +tags: ["AL","Readability"] +categories: ["Best Practice"] +--- + +See the documentation on learn.microsoft.com for more information about [SetLoadFields](https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-setloadfields-method). + +For the performance of your code it is important that you use SetLoadFields as much as possible. + +If you want to retrieve a record from the database to check if the record is available the advice is to set an SetLoadFields on the primary key fields of the table so only those fields will be retrieved from the database. + +## Bad code + +```AL +if not Item.Get(ItemNo) then + exit(); +``` + +## Good code + +```AL +Item.SetLoadFields("No."); +if not Item.Get(ItemNo) then + exit(); +``` + +Place the SetLoadFields in the code before filtering the record (there is no need to record filterfields in the SetLoadFields). +## Bad code + +```AL +Item.Setrange("Third Party Item Exists", false); +Item.SetLoadFields("Item Category Code"); +Item.Get(ItemNo); +``` + +## Good code + +```AL +Item.SetLoadFields("Item Category Code"); +Item.Setrange("Third Party Item Exists", false); +Item.Get(ItemNo); +``` + From b317951d6386aaf6dbaa4f6122423cf25735d0af Mon Sep 17 00:00:00 2001 From: Luuk Date: Tue, 6 Jun 2023 10:58:57 +0200 Subject: [PATCH 2/5] SetLoadFields --- content/docs/BestPractices/SetLoadFields/Index.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/content/docs/BestPractices/SetLoadFields/Index.md b/content/docs/BestPractices/SetLoadFields/Index.md index 259ba178..11453790 100644 --- a/content/docs/BestPractices/SetLoadFields/Index.md +++ b/content/docs/BestPractices/SetLoadFields/Index.md @@ -8,7 +8,7 @@ See the documentation on learn.microsoft.com for more information about [SetLoad For the performance of your code it is important that you use SetLoadFields as much as possible. -If you want to retrieve a record from the database to check if the record is available the advice is to set an SetLoadFields on the primary key fields of the table so only those fields will be retrieved from the database. +If you want to retrieve a record from the database to check if the record is available the advice is to use SetLoadFields on the primary key fields of the table so only those fields will be retrieved from the database. ## Bad code @@ -25,11 +25,12 @@ if not Item.Get(ItemNo) then exit(); ``` -Place the SetLoadFields in the code before filtering the record (there is no need to record filterfields in the SetLoadFields). + +Place the SetLoadFields in the code before filtering the record (there is no need to record filter fields in the SetLoadFields because these will be retrieved automatically). ## Bad code ```AL -Item.Setrange("Third Party Item Exists", false); +Item.SetRange("Third Party Item Exists", false); Item.SetLoadFields("Item Category Code"); Item.Get(ItemNo); ``` @@ -38,7 +39,7 @@ Item.Get(ItemNo); ```AL Item.SetLoadFields("Item Category Code"); -Item.Setrange("Third Party Item Exists", false); +Item.SetRange("Third Party Item Exists", false); Item.Get(ItemNo); ``` From 6b3d740bbbced8d9ac79f50abe76b73ceb13a31a Mon Sep 17 00:00:00 2001 From: Luuk Date: Thu, 10 Aug 2023 10:38:28 +0200 Subject: [PATCH 3/5] #SetLoadFields --- content/docs/BestPractices/SetLoadFields/Index.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/docs/BestPractices/SetLoadFields/Index.md b/content/docs/BestPractices/SetLoadFields/Index.md index 11453790..465aea6f 100644 --- a/content/docs/BestPractices/SetLoadFields/Index.md +++ b/content/docs/BestPractices/SetLoadFields/Index.md @@ -8,7 +8,7 @@ See the documentation on learn.microsoft.com for more information about [SetLoad For the performance of your code it is important that you use SetLoadFields as much as possible. -If you want to retrieve a record from the database to check if the record is available the advice is to use SetLoadFields on the primary key fields of the table so only those fields will be retrieved from the database. +If you want to retrieve a record from the database to check if the record is available always use SetLoadFields on the primary key fields of the table so only those fields will be retrieved from the database. ## Bad code @@ -26,20 +26,20 @@ if not Item.Get(ItemNo) then ``` -Place the SetLoadFields in the code before filtering the record (there is no need to record filter fields in the SetLoadFields because these will be retrieved automatically). +Place the SetLoadFields in the code before the line of the Get (or find). (there is no need to record filter fields in the SetLoadFields because these will be retrieved automatically). ## Bad code ```AL -Item.SetRange("Third Party Item Exists", false); Item.SetLoadFields("Item Category Code"); +Item.SetRange("Third Party Item Exists", false); Item.Get(ItemNo); ``` ## Good code ```AL -Item.SetLoadFields("Item Category Code"); Item.SetRange("Third Party Item Exists", false); +Item.SetLoadFields("Item Category Code"); Item.Get(ItemNo); ``` From 892862e068b874a4577ba02b9034db4e96463971 Mon Sep 17 00:00:00 2001 From: Luuk Date: Fri, 22 Sep 2023 12:13:18 +0200 Subject: [PATCH 4/5] #SetLoadFIelds --- .../docs/BestPractices/SetLoadFields/Index.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/content/docs/BestPractices/SetLoadFields/Index.md b/content/docs/BestPractices/SetLoadFields/Index.md index 465aea6f..684b751d 100644 --- a/content/docs/BestPractices/SetLoadFields/Index.md +++ b/content/docs/BestPractices/SetLoadFields/Index.md @@ -43,3 +43,27 @@ Item.SetLoadFields("Item Category Code"); Item.Get(ItemNo); ``` +Place the SetLoadFields in the code before the case statement +## Bad code + +```AL +Item.SetLoadFields("Item Category Code"); +ItemCategoryCode := FindItemCategoryCode; + +case true of + Item.Get(ItemNo): + SetItemCategoryCode(Item, ItemCategoryCode); +end; +``` + +## Good code + +```AL +ItemCategoryCode := FindItemCategoryCode; +Item.SetLoadFields("Item Category Code"); + +case true of + Item.Get(ItemNo): + SetItemCategoryCode(Item, ItemCategoryCode); +end; +``` From 8befe1b704b0a0ad3748d9469281ad4331fbf934 Mon Sep 17 00:00:00 2001 From: Luuk Date: Fri, 22 Sep 2023 13:01:23 +0200 Subject: [PATCH 5/5] #SetLoadFields --- content/docs/BestPractices/SetLoadFields/Index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/docs/BestPractices/SetLoadFields/Index.md b/content/docs/BestPractices/SetLoadFields/Index.md index 684b751d..e3aae546 100644 --- a/content/docs/BestPractices/SetLoadFields/Index.md +++ b/content/docs/BestPractices/SetLoadFields/Index.md @@ -32,7 +32,7 @@ Place the SetLoadFields in the code before the line of the Get (or find). (there ```AL Item.SetLoadFields("Item Category Code"); Item.SetRange("Third Party Item Exists", false); -Item.Get(ItemNo); +Item.FindFirst(); ``` ## Good code @@ -40,7 +40,7 @@ Item.Get(ItemNo); ```AL Item.SetRange("Third Party Item Exists", false); Item.SetLoadFields("Item Category Code"); -Item.Get(ItemNo); +Item.FindFirst(); ``` Place the SetLoadFields in the code before the case statement