Thursday, February 23, 2017

Rename Primary key field in AX 2012

//Rename Primary key field in AX 2012

//But is not advisable to do it from Backend
// Only for Code review purpose

void renamePrimaryKey()
{
    Dialog          dialog;
    DialogField     dialogField;
    int             i;
    str             oldValue;
    str             newValue;
    FieldId         fieldId;
    FormDataSource  formDataSource;
    formDataSource      = callerForm.dataSource();
    dialog = new Dialog();
    dialog.addText(strFmt("@SYS70880", common.(dictField.id())));
    // Ignore EDT validation as the rename scenario only requires
    // the type of the EDT and its label and help text.
    dialogField = dialog.addField(dictField.typeName(), '', '', true);
    dialogField.lookupButton(1);
    if (! dialog.run())
        return;
    if (dialogField.value())
    {
        oldValue = common.(dictField.id());
        newValue = dialogField.value();
        if (Box::yesNo(strFmt("@SYS70878",oldValue,newValue),
                       DialogButton::No,
                       strFmt("@SYS70879",dictField.label()),
                       strFmt("@SYS29047",dictField.label())))
        {
            //SIG - start
            if (isConfigurationkeyEnabled(configurationKeyNum(SIG)))
            {
                SIGBaseDocument::checkAndCacheRename(common,dictField.id(),newValue);
            }
            //SIG -end
            startLengthyOperation();
            fieldId = dictField.id();
            try
            {
                ttsbegin;
                common.(fieldId) = dialogField.value();
                common.renamePrimaryKey();
                if (common.TableId == tableNum(UserInfo))
                {
                    common.update();
                }
                ttscommit;
            }
            catch (Exception::Error)
            {
                ttsabort;
            }

            for (i=1; i <= callerForm.dataSourceCount(); i++)
            {
                formDataSource = callerForm.dataSource(i);
                if ( formDataSource.cursor() == formObjectSet.cursor() /*element.docCursor() */)
                {
                    while (formDataSource && formDataSource.joinSource())
                    {
                        formDataSource = this.findDataSource(formDataSource.joinSource());
                    }
                    if (formDataSource)
                    {
                        formDataSource.reread();
                        formDataSource.refresh();
                        formDataSource.active();
                    }
                    break;
                }
            }
        }
    }
    element.close();
}

No comments:

Post a Comment

Convert Call stack to readable format in D365FO X++

//Input --container _xppCallStack = xSession::xppCallStack();  Public static str POL_formatXppCallStack(container _xppCallStack, int _skipFr...