Tuesday, September 26, 2023

Generating 1D or 2D barcode in X++

Barcode barcode = BarcodeCode128::construct();
barcode.string(true, localInventDim.inventSerialId);
BarCodeString barCodeString = barcode.barcodeStr();
public str getBarcode(str _barCodeText)

  {

   BarcodeCode128 barcode;

   barcode = Barcode::construct(BarcodeType::Code128);

   barcode.string(true, _barCodeText);

   barcode.encode();

   return barcode.barcodeStr();

   }

Monday, September 25, 2023

Convert Json to XML C#

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;

namespace JSONStringConvert
{
    public class Utilities
    {
        public static string ConvertToXML(string json)
        {
            //if an empty string is passed, return empty string without conversion
            if (string.IsNullOrEmpty(json))
                return string.Empty;
            //if invalid json is passed, throw error.
            ValidateJSON(json);
            //Convert Json to XML
            XDocument xmlDoc = JsonConvert.DeserializeXNode(json);
            return xmlDoc.ToString();
        }
        public static void ValidateJSON(string json)
        {
            try
            {
                var jsonObject = JObject.Parse(json);
            }
            catch (Exception ex)
            {
                throw new InvalidCastException("Provided json is not valid.", ex);
            }
        }
    }
}

Gets the primary index of a table

// <summary>
    /// Gets the primary index of a table.
    /// </summary>
    /// <param name="_tableName">
    /// A table name.
    /// </param>
    /// <returns>
    /// Returns field id.
    /// </returns>
    public static Container getPrimaryIndex(TableName _tableName)
    {
        SysDictIndex        keyIndex;
        DictField           dictField;
        int                 fieldIndex;
        container           res, isMandatory;
        SysDictTable sysDictTable = new SysDictTable(tableName2id(_tableName));
        if(sysDictTable)
        {
            TableId tableId = sysDictTable.id();
            keyIndex = new SysDictIndex(tableId, sysDictTable.primaryIndex());
            if (keyIndex)
            {
                for (fieldIndex = 1; fieldIndex <= keyIndex.numberOfFields(); fieldIndex++)
                {

                    if (fieldId2name(tableId, keyIndex.field(fieldIndex)) != fieldStr(DMFDefinitionGroupExecution,ExecutionId) &&
                        fieldId2name(tableId, keyIndex.field(fieldIndex)) != fieldStr(DMFDefinitionGroupExecution,DefinitionGroup))
                    {
                        dictField = new DictField(tableId, keyIndex.field(fieldIndex));
                        res += dictField.id();
                        isMandatory += dictField.mandatory();
                    }
                }
            }
        }

        return [res, isMandatory];
    }

Find all the tables in Ax that have no indexes

SQLDictionary   dict;
    DictTable       dictTable;

    ;
    while select dict
    where dict.fieldId == 0 // only tablenames
    {
        dictTable = new DictTable(dict.tabId);
        if ( dictTable.indexCnt() == 0 && ! dictTable.isView() )
        {
            info(strfmt("Table %1 has no indexes", dictTable.name()));
        }
    }

Call insert or update for dataentity

Var enumerator = entityNames.getEnumerator();

while (enumerator.MoveNext())
{
	Microsoft.Dynamics.AX.Metadata.MetaModel.AxDataEntityView DataEntity = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetDataEntityView(enumerator.Current);
	//matching with publicname of the entity
	if(DataEntity.PublicEntityName == parentNodeName)
	{
		entityName  = DataEntity.Name;
		dictTable   = new SysDictTable(tableName2Id(entityName));
		dictDataEntity   = new DictDataEntity(tableName2Id(entityName));
		common           = dictDataEntity.makeRecord();
		break;
	}
}
ttsbegin;
dictDataEntity.callObject("Write", common);
ttscommit;

X++ code to get metadata of data entities

var entityNames = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetDataEntityViewNames();

Var enumerator = entityNames.getEnumerator();

while (enumerator.MoveNext())

{
	enumerator.MoveNext();

	Microsoft.Dynamics.AX.Metadata.MetaModel.AxDataEntityView DataEntity = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetDataEntityView(enumerator.Current);

   if (DataEntity)
   {         

		info(strFmt("Entity Name: %1 -- Entity Label; %2 -- Entity public collection Name: %3 -- Entity Public Name %4 -- Is Entity Public - %5--Is Entity Shared - %6",

		DataEntity.Name,
		SysLabel::labelId2String(DataEntity.Label),
		DataEntity.PublicCollectionName,
		DataEntity.PublicEntityName,
		DataEntity.IsPublic,
		DataEntity.PrimaryCompanyContext));
     }
}

Get D365Fo Host name

using Microsoft.Dynamics.ApplicationPlatform.Environment;
public str GetInfrastructureHost()
{
        IApplicationEnvironment env = EnvironmentFactory::GetApplicationEnvironment();
        str hostUrl = env.Infrastructure.HostUrl;
        
        // Trim https://
        hostUrl = subStr(hostUrl, 9, strLen(hostUrl) - 8);

        return subStr(hostUrl, 1, strFind(hostUrl, '.', 1, strLen(hostUrl)) - 1);
}

Table browser URL in D365FO

Critical Thinking icon icon by Icons8