Generate error on empty Financial Dimensions in D365FO

When working with Human Resources and Finance integration in Dynamics 365 Finance and Operations (D365FO), ensuring financial dimensions are properly defined on employee records is critical for accurate reporting and budgeting. In this blog, we'll look at how to enforce required dimensions using a validatedWrite event handler on the HcmEmployment table.


We'll use an event handler on the HcmEmployment table's validatedWrite method to check for the presence of specific dimensions on the DefaultDimension field.


[DataEventHandler(tableStr(HcmEmployment), DataEventType::ValidatedWrite)]

    public static void HcmEmployment_onValidatedWrite(Common sender, DataEventArgs e)

    {

        HcmEmployment HcmEmployment = sender as HcmEmployment;

        DimensionAttributeValueSetStorage dimAttrValueSetStorage;

        DimensionAttribute dimensionAttribute;


        // List of required financial dimension names

        List dimensionNames = new List(Types::String);

        dimensionNames.addEnd("BusinessUnit");

        dimensionNames.addEnd("Department");

        dimensionNames.addEnd("Budget");

        dimensionNames.addEnd("Projects");

        dimensionNames.addEnd("Worker");


        dimAttrValueSetStorage = DimensionAttributeValueSetStorage::find(HcmEmployment.DefaultDimension);


        // Loop through each required dimension and check if it's present

        ListEnumerator enumerator = dimensionNames.getEnumerator();

        while (enumerator.moveNext())

        {

            str dimName = enumerator.current();

            dimensionAttribute = DimensionAttribute::findByName(dimName);


            if (!dimAttrValueSetStorage.containsDimensionAttribute(dimensionAttribute.RecId))

            {

                throw error(strFmt("Financial dimension '%1' is required.", dimName));

            }

        }

    }

It checks if each of the required financial dimensions is present in the DefaultDimension value set.

If any required dimension is missing, it throws an error.

Conclusion:

Validating financial dimensions programmatically ensures better data integrity and aligns HR records with your organization’s financial structure. Using event handlers is a clean and extensible way to inject validation logic without customizing core logic.

Comments

Popular posts from this blog

JSON-Based HTTP POST Request from X++ to Retrieve an Access Token

Calling an External API from D365FO Using X++ to Retrieve a Token

Restricting Date Selection in Dynamics 365 FO using X++