Managing customizations in Dynamics 365 Finance & Operations can be challenging, especially when dealing with different legal entities. A UI-level solution that enables or disables custom code at the legal entity level is an effective approach. This post will guide you through a recommended best practice approach to achieve this.
Why Manage Custom Code at the Legal Entity Level?
Customizations are often necessary to meet specific business requirements. However, these customizations may not be relevant for all legal entities within an organization. By managing custom code at the legal entity level, you can ensure that each entity has only the customizations it needs, leading to better performance, easier maintenance, and improved compliance with local regulations.
Key Components of the Solution
Create a Custom Configuration Key
The initial step involves creating a custom configuration key in D365FO, which can be toggled on or off. This key will serve as a master switch for your custom code, allowing you to easily enable or disable specific features as needed. By setting up this configuration key, you can manage the functionality of your customizations without having to directly modify the code each time a change is required. This approach ensures flexibility and control over your system’s behavior, making it easier to adapt to various business needs and scenarios.
Implement a Custom Table
Next, design a custom table specifically to store settings for each legal entity within the system. This table should contain fields for the legal entity ID, which uniquely identifies each entity, the feature name to specify the particular feature being configured, and an enabled/disabled flag to indicate whether the feature is active or inactive for that entity. Incorporating these detailed fields will ensure precise control and customization of features across different legal entities.
Develop a Custom Form
Create a new form in D365FO to manage these settings. This form should allow users to select a legal entity from a dropdown menu, offering a comprehensive list for easy navigation. Users should be able to toggle features on or off using clearly marked checkboxes or switches. Thoughtful labels and tooltips should guide users seamlessly through the process, providing explanations and additional information to prevent confusion.
Alongside these user-friendly elements, implement robust security measures, such as role-based permissions and user authentication, to ensure that only authorized users have access. This includes configuring permissions in alignment with organizational roles and verifying user identities to safeguard against unauthorized access. Regular audits should be conducted to maintain the integrity and security of the form, identifying any discrepancies or potential vulnerabilities.
Use Extension Framework
Utilize D365FO’s extension framework to implement your custom code. This powerful tool allows you to extend the functionality of the standard application without modifying the base code. In your extensions, always ensure to check both the configuration key and the custom table settings before executing custom logic. By doing so, you can maintain system integrity and avoid potential conflicts or errors, ensuring a smoother and more efficient operation of your custom solutions.
Ensuring that your extensions check both the configuration key and the custom table settings before executing custom logic will not negatively affect system performance. This dual-check mechanism is designed to operate efficiently, adding only minimal overhead to your operations. By verifying the configuration key and custom table settings, you are leveraging streamlined processes within D365FO that are optimized for quick decision-making.
This practice actually enhances overall system stability and reliability, as it prevents the execution of unnecessary custom code, thereby maintaining optimal performance across the different legal entities. Implementing these checks can also reduce the risk of errors and conflicts, ensuring that your system runs smoothly without significant performance degradation.
Here’s a sample code structure in X++:
```
[ExtensionOf(classStr(SalesTable))]
final class SalesTableExtension_RSM
{
public void example()
{
if (this.isCustomFeatureEnabled('FeatureName'))
{
// Custom code here
}
}
private boolean isCustomFeatureEnabled(str _featureName)
{
if (!FeatureManagementFacade::isFeatureEnabled(ConfigurationKeyName))
{
return false;
}
return CustomFeatureSettings::isEnabledForLegalEntity(curext(), _featureName);
}
}
```
Implement Caching
To improve performance, implement a caching mechanism for the settings. This will allow frequently accessed data to be retrieved more quickly, reducing the load on the main database. Make sure to refresh the cache periodically to ensure it remains up-to-date, or whenever settings are changed to maintain data accuracy and integrity. Additionally, consider monitoring the cache performance regularly to identify any potential issues or areas for further optimization.
How to Implement Caching in X++ Code
Implementing caching in X++ code can significantly enhance the performance of your D365FO system by reducing the frequency of database queries and improving data retrieval times. Below is a step-by-step guide on how to implement caching for custom settings.
Define Cache Data Entity
Create a data entity that will hold the cached data. This entity should map to the table containing the settings you wish to cache:
```x++
[DataEntityKey(DataEntityFieldType.PrimaryKey)]
public static boolean isEnabledForLegalEntity(DataAreaId _legalEntity, str _featureName)
{
select firstonly1 RecId
from customTable
where customTable.DataAreaId == _legalEntity
&& customTable.FeatureName == _featureName;
return customTable.Enabled;
}
```
Create Cache Class
Develop a new class responsible for managing the cache. This class will handle the retrieval, storage, and refreshing of cache data:
```x++
public class CustomSettingsCache
{
private static Map settingsCache;
public static void initialize()
{
settingsCache = new Map(Types::String, Types::Boolean);
refresh();
}
public static void refresh()
{
settingsCache = new Map(Types::String, Types::Boolean);
CustomTable customTable;
while select customTable
{
str cacheKey = strFmt("%1|%2", customTable.DataAreaId, customTable.FeatureName);
settingsCache.insert(cacheKey, customTable.Enabled);
}
}
public static boolean isEnabledForLegalEntity(DataAreaId _legalEntity, str _featureName)
{
str cacheKey = strFmt("%1|%2", _legalEntity, _featureName);
return settingsCache.exists(cacheKey) ? settingsCache.lookup(cacheKey) : false;
}
}
```
Initialization and Refresh Mechanism
Ensure the “initialize” method is called during the application lifecycle start-up or whenever appropriate. Implement a mechanism to call the “refresh” method when settings are altered.
```x++
// Call this during system initialization
CustomSettingsCache::initialize();
// Refresh the cache after settings are updated
CustomSettingsCache::refresh();
```
Utilize the Cache in Extensions
Adapt your extensions to use the caching class to retrieve settings instead of querying the database.
```x++
[ExtensionOf(classStr(SalesTable))]
final class SalesTableExtension_RSM
{
public void example()
{
if (CustomSettingsCache::isEnabledForLegalEntity(curext(), 'FeatureName'))
{
// Custom code here
}
}
}
```
By following these steps, you can effectively implement caching in your Dynamics 365 F&O environment using X++ code. This approach will improve system performance and reduce the load on your main database by minimizing frequent data queries.
Security Considerations
Implement proper security roles and privileges for accessing the custom settings form to ensure that only authorized personnel can make changes. Additionally, log all changes to these settings for auditing purposes, which helps to maintain a detailed record of modifications and ensures accountability. Regularly review these logs to detect any unauthorized access or alterations and to uphold the integrity of the system.
Implementation Steps
1. Use Extension Framework
Utilize D365FO’s extension framework to implement your custom code. In your extensions, check both the configuration key and the custom table settings before executing custom logic.
2. Implement Caching
To improve performance, implement a caching mechanism for the settings. Refresh the cache periodically or when settings are changed.
3. Security Considerations
Implement proper security roles and privileges for accessing the custom settings form. Log changes to these settings for auditing purposes.
Final Thoughts
In conclusion, managing customizations at the legal entity level in D365FO is crucial for maintaining system performance, ensuring compliance, and meeting specific business needs. This involves carefully planning and executing customizations to avoid conflicts and ensure smooth operations. By following the steps outlined in this article, including thorough testing and documentation, you can implement a robust solution that allows for easy management of custom code across different legal entities. Proper governance and regular reviews will further enhance the system’s reliability and adaptability to evolving business requirements.
The post Strategies for Managing Custom Code at the Legal Entity Level in D365FO appeared first on Dynamics Communities.