среда, 29 ноября 2023 г.

Open D365F&O form with specific query parameters in URL

Recently I was struggled with one task assigned to me. I had to store string as URL that points to the particular form with particular query filtered. For instance, open particular sales order or some voucher. A colleguae of mine has proposed coming up with such a logic to generate deep link with all required parameters:
       
using Microsoft.Dynamics.AX.Framework.Utilities;
using Microsoft.Dynamics.ApplicationPlatform.Environment;

class M_DeepLinkCreator
{
     /*#######################################################################################################################################*/
    public static str createDeepLink(str _menuItemName, DataSourceName _dataSourceName, Map _fieldValuesMap, DataAreaId _dataAreaId = curExt())
    {
        IApplicationEnvironment env         = EnvironmentFactory::GetApplicationEnvironment();
        System.Uri              host        = new System.Uri(env.Infrastructure.HostUrl);
        MapEnumerator           mapEnumerator;
        UrlHelper.UrlGenerator  generator   = new UrlHelper.UrlGenerator();

        generator.HostUrl       = host.GetLeftPart(System.UriPartial::Path);
        generator.Company       = _dataAreaId;
        generator.MenuItemName  = _menuItemName;

        if (_dataSourceName && _fieldValuesMap)
        {
            mapEnumerator = _fieldValuesMap.getEnumerator();

            var requestQueryParameterCollection = generator.RequestQueryParameterCollection;

            while (mapEnumerator.moveNext())
            {
                requestQueryParameterCollection.UpdateOrAddEntry(_dataSourceName, mapEnumerator.currentKey(), mapEnumerator.currentValue());
            }
        }

        return generator.GenerateFullUrl().AbsoluteUri;
    }
    /*#######################################################################################################################################*/

}    
 
Hope it's going to be useful for you in your development process.