Monday, September 25, 2023

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];
    }

No comments:

Post a Comment

Table browser URL in D365FO

Critical Thinking icon icon by Icons8