X++ -Statement generieren um Datensätze anzulegen

Vor kurzem hatte ich die Notwendigkeit einige Datensätze von einer AX-Umgebung in eine andere zu kopieren. Es ging um eine sehr flache Tabelle (d.h. ohne großartige Referenzen auf andere Tabellen oder Datensätze), wodurch ich folgenden Job dafür nutzen konnte.

Der Job durchläuft ein Select-Statement (ganz am Ende des Jobs) und kopiert mir in die Zwischenablage ein X++-Statement, mit welchem man die Datensätze - beispielsweise über einen eigenen Job - einfügen kann.

Inspiriert ist der Code übrigens von der Methode buildInsertScript() des Formulares SysRecordInfo.
 

static void buildInsertScriptAX2009(Args _args)
{
    CustGroup CustGroup;

    #define.indent('    ')

    SysDictTable dictTable;
    Counter     fieldCounter;
    Counter     arrayCounter;
    DictEnum    dictEnum;
    DictField   dictFieldTemp;
    DictField   dictFieldArray;
    str         fieldValue;
    TextBuffer  textBuffer;
    Source      source;

    boolean setFieldValue(fieldId _fieldId, common _common)
    {
        boolean ret = true;

        fieldValue = "";

        if (dictFieldTemp.baseType() != Types::Container)
        {
            fieldValue = strfmt("%1", _common.(_fieldId));
        }

        switch (dictFieldTemp.baseType())
        {
            case Types::String :
                fieldValue = strReplace(fieldValue, '\n', '\\n');
                fieldValue = '\"' + fieldValue + '\"';
                break;

            case Types::Container :
                fieldValue = '[' + fieldValue + ']';
                break;

            case Types::Enum :
                dictEnum = new DictEnum(dictFieldTemp.enumId());
                fieldValue = dictEnum.name() + '::' + dictEnum.index2Symbol(_common.(_fieldId));
                break;

            case Types::Real :
                fieldValue = num2str(_common.(_fieldId), 2, -1, 1, 0);
                break;

            case Types::Date :
                fieldValue = date2str(_common.(_fieldId), 123,2,4,2,4,4);
                if (fieldValue == '')
                {
                    ret = false;
                }
                fieldValue = strReplace(fieldValue, '/', '\\');
                break;

            case Types::UtcDateTime :
                fieldValue = DateTimeUtil::toStr(_common.(_fieldId));
                if (fieldValue == '')
                {
                    ret = false;
                }
                break;
        }
        return ret;
    }

    void loopRecord(common _common)
    {
        ;
        source += #indent + strfmt('%1.clear();\r\n', dictTable.name());

        for (fieldCounter = dictTable.fieldNext(0); fieldCounter > 0; fieldCounter = dictTable.fieldNext(fieldCounter))
        {
            dictFieldTemp = new DictField(dictTable.id(), fieldCounter);

            if (dictFieldTemp.isSystem())
                continue;

            if (dictFieldTemp.arraySize() > 1)
            {
                arrayCounter = 1;
                while (arrayCounter <= dictFieldTemp.arraySize())
                {
                    dictFieldArray = new DictField(dictTable.id(), fieldId2Ext(dictFieldTemp.id(),arrayCounter));
                    if (setFieldValue(dictFieldArray.id(), _common))
                    {
                        source = source + #indent + strfmt("%1.%2 = %3;", dictTable.name(),
                                                                          dictFieldArray.name(DbBackend::Native, arrayCounter),
                                                                          fieldValue) + '\r\n';
                    }
                    arrayCounter++;
                }
            }
            else
            {
                if (setFieldValue(fieldCounter, _common))
                {
                    source = source + #indent + strfmt("%1.%2 = %3;", dictTable.name(), dictFieldTemp.name(), fieldValue) + '\r\n';
                }
            }
        }

        source += #indent + strfmt('%1.insert();\r\n', dictTable.name());
    }

    ;

    // Modify needed records here
    dictTable = new SysDictTable(tableNum(CustGroup));

    while select CustGroup
    {
        loopRecord(CustGroup);
    }

 

 

    textBuffer = new TextBuffer();
    textBuffer.setText(source);
    textBuffer.toClipboard();

    info("@SYS87601");
}

Das Ergebnis des Jobs ist in der Zwischenablage zu finden, und sieht beispielsweise wie folgt aus:

CustGroup.clear();
CustGroup.CustGroup = "K-DRITT";
CustGroup.Name = "Kunden Drittland";
CustGroup.ClearingPeriod = "";
CustGroup.PaymTermId = "";
CustGroup.TaxGroupId = "";
CustGroup.PaymIdType = "";
CustGroup.insert();
CustGroup.clear();
CustGroup.CustGroup = "K-EU";
CustGroup.Name = "Kunden EU";
CustGroup.ClearingPeriod = "";
CustGroup.PaymTermId = "";
CustGroup.TaxGroupId = "";
CustGroup.PaymIdType = "";
CustGroup.insert();
CustGroup.clear();
CustGroup.CustGroup = "K-INL";
CustGroup.Name = "Kunden Inland";
CustGroup.ClearingPeriod = "";
CustGroup.PaymTermId = "";
CustGroup.TaxGroupId = "";
CustGroup.PaymIdType = "";
CustGroup.insert();
 

In Dynamics AX 2012 sieht der gleiche Job etwas anders aus:

static void buildInsertScriptAX2012(Args _args)
{
    CustGroup CustGroup;
   
    #define.indent('    ')
   
    SysDictTable dictTable;
    Counter     fieldCounter;
    Counter     arrayCounter;
    DictEnum    dictEnum;
    DictField   dictFieldTemp;
    DictField   dictFieldArray;
    str         fieldValue;
    TextBuffer  textBuffer;
    Source      source;

    boolean setFieldValue(common _common, FieldName _fieldName, ArrayIdx _arrayIndex = 1)
    {
        boolean ret = true;
        fieldValue = "";

        if (dictFieldTemp.baseType() != Types::Container)
        {
            fieldValue = strFmt("%1", _common.getFieldValue(_fieldName, _arrayIndex));
        }

        switch (dictFieldTemp.baseType())
        {
            case Types::String :
                fieldValue = strReplace(fieldValue, '\n', '\\n');
                fieldValue = '\"' + fieldValue + '\"';
                break;

            case Types::Container :
                fieldValue = '[' + fieldValue + ']';
                break;

            case Types::Enum :
                dictEnum = new DictEnum(dictFieldTemp.enumId());
                fieldValue = dictEnum.name() + '::' + dictEnum.value2Symbol(_common.getFieldValue(_fieldName, _arrayIndex));
                break;

            case Types::Real :
                fieldValue = num2str(_common.getFieldValue(_fieldName, _arrayIndex), 2, -1, 1, 0);
                break;

            case Types::Date :
                fieldValue = date2str(_common.getFieldValue(_fieldName, _arrayIndex), 123,2,4,2,4,4, DateFlags::None);
                if (fieldValue == '')
                {
                    ret = false;
                }
                fieldValue = strReplace(fieldValue, '/', '\\');
                break;

            case Types::UtcDateTime :
                fieldValue = DateTimeUtil::toStr(_common.getFieldValue(_fieldName, _arrayIndex));
                if (fieldValue == '')
                {
                    ret = false;
                }
                break;
        }
        return ret;
    }
   
    void loopRecord(common _common)   
    {
        ;

        for (fieldCounter = dictTable.fieldNext(0); fieldCounter > 0; fieldCounter = dictTable.fieldNext(fieldCounter))
        {
            dictFieldTemp = new DictField(dictTable.id(), fieldCounter);

            if (dictFieldTemp.isSystem())
                continue;

            if (dictFieldTemp.arraySize() > 1)
            {
                arrayCounter = 1;
                while (arrayCounter <= dictFieldTemp.arraySize())
                {
                    dictFieldArray = new DictField(dictTable.id(), dictFieldTemp.id(), arrayCounter);
                    if (setFieldValue(_common, dictFieldArray.name(), arrayCounter))
                    {
                        source = source + #indent + strFmt("%1.%2 = %3;", dictTable.name(),
                                                                          dictFieldArray.name(DbBackend::Native, arrayCounter),
                                                                          fieldValue) + '\r\n';
                    }
                    arrayCounter++;
                }
            }
            else
            {
                if (setFieldValue(_common, dictFieldTemp.name()))
                {
                    source = source + #indent + strFmt("%1.%2 = %3;", dictTable.name(), dictFieldTemp.name(), fieldValue) + '\r\n';
                }
            }
        }

        source += #indent + strFmt('%1.insert();\r\n', dictTable.name());
    }
   
    // Modify needed records here   
    dictTable = new SysDictTable(tableNum(CustGroup));

    while select CustGroup
    {
        loopRecord(CustGroup);
    }
 

    textBuffer = new TextBuffer();
    textBuffer.setText(source);
    textBuffer.toClipboard();

    info("@SYS87601");
}
Dieser Beitrag bezieht sich auf die Versionen:
Dynamics AX 2009, Dynamics AX 2012

 
 

 

 
 
 
Beiträge des aktuellen Monats
November 2024
MoDiMiDoFrSaSo
 123
45678910
11121314151617
18192021222324
252627282930 
 
© 2006-2024 Heinz Schweda | Impressum | Kontakt | English version | Mobile Version
Diese Webseite verwendet Cookies, um Benutzern einen besseren Service anzubieten. Wenn Sie weiterhin auf der Seite bleiben, stimmen Sie der Verwendung von Cookies zu.  Mehr dazu