Thursday, January 2, 2014

How To Iterate all the fields of a table in Ax2012 using x++ code.

In my recent implementation I was having one requirement in this I had to retrieve all the fields of a table and then on certain criteria I had to update these fields in another table. I could do this sing If-else condition but it requires lot of code. So I did it in a better way.

public void updateEstimatesFromCase()
{
    CopyEstimates                   copyEstimates;
    SysDictTable                    dictTable;
    SysDictTable                    caseDictTable;
    SysDictTable                    activityDictTable;
    SysDictField                    dictField;
    SysDictField                    caseDictField;
    SysDictField                    activityDictField;
    FieldId                         fieldId;
    FieldId                         caseFieldId;
    FieldId                         activityFieldId;
    CaseDetailBase                  caseDetailBase = caseDetailBase::find(this.caseId());
    CaseTimeEstimatesTable          caseTimeEstimatesTable;
    ActivityTimeEstimatesTable      activityTimeEstimatesTable;
    ActivityTimeEstimatesTable      buffer;
    ;

    select firstonly caseTimeEstimatesTable
        where caseTimeEstimatesTable.CaseDetailBase == caseDetailBase.RecId;

    select firstOnly activityTimeEstimatesTable
        where activityTimeEstimatesTable.smmActivities == this.smmActivities;

    buf2Buf(buffer,activityTimeEstimatesTable);
    if (activityTimeEstimatesTable.RecId)
    {
        activityTimeEstimatesTable.selectForUpdate(true);
        activityTimeEstimatesTable.update();
    }
    caseDictTable = new SysDictTable(caseTimeEstimatesTable.TableId);
    dictTable = new SysDictTable(this.TableId);
    fieldId = dictTable.fieldNext(0);


    while (fieldId)
    {
        dictField = dictTable.fieldObject(fieldId);
        if (dictField.isSql() && !dictField.isSystem())
        {
            activityTimeEstimatesTable.smmActivities = this.smmActivities;
            if (this.(fieldId) && fieldId != fieldNum(copyEstimates, smmActivities))
            {

                caseFieldId = fieldName2id(tableNum(CaseTimeEstimatesTable),dictField.name());
                activityFieldId = fieldName2id(tableNum(ActivityTimeEstimatesTable),dictField.name());
                activityTimeEstimatesTable.(activityFieldId) = caseTimeEstimatesTable.(caseFieldId);
                if (activityTimeEstimatesTable.RecId)
                {
                    activityTimeEstimatesTable.selectForUpdate(true);
                    activityTimeEstimatesTable.update();
                }
                else
                {
                    activityTimeEstimatesTable.insert();
                }
            }
    }
        fieldId = dictTable.fieldNext(fieldId);
   }
}

No comments:

Post a Comment