Bojensen Blogs

Accessing record fields

The usual way

Obviously to access a field of a record, you usually just use its name:

      table_name.field_name

Example:

CustTable ct;
;

select firstonly ct;
info(ct.CustAccount);

The different way

But, what do you do, if you want to access the fields of a record, where you do not know the field names?

In this case you can access field values via their field ID:

      table_name.(field_id)

Example:

 

CustTable ct;
;

select firstonly ct;
info(ct.(fieldnum(CustTable, CustAccount)));

For example the following function outputs any record to the infolog. This is achieved by iterating over the DictTable object of the passed record. The DictTable will give us the field Id’s, which in turn are used to access the field value.

 

static void Info(common _common, boolean _includeSys = false)
{
    SysDictTable   dictTable;
    SysDictField   dictField;
    int            fieldIdx;
    fieldid        commonFieldId;
    ;

    dictTable = new SysDictTable(_common.TableId);
    setPrefix("Record");
    setPrefix(dictTable.label());
    for(fieldIdx=1; fieldIdx <= dictTable.fieldCnt(); fieldIdx++)
    {
      commonFieldId =  dictTable.fieldCnt2Id(fieldIdx);
      dictField = dictTable.fieldObject(commonFieldId);
      if(!dictField.isSystem() || _includeSys)
        if(dictField.baseType() == Types::Container)
          info(strfmt("%1 = <%2>", dictField.label(), dictField.baseType() ));
        else
          info(strfmt("%1 = %2", dictField.label(), _common.(commonFieldId)));
    }
}

Example of accessing dimension fields.

int        idx;
FieldId    fieldId;
FieldId    fieldIdExt;

CustTrans  custTrans;
;

idx = SysDimension::Center+1;

select firstOnly custTrans
    where custTrans.Dimension[idx];

fieldId = fieldNum(CustTrans, Dimension);

fieldIdExt = fieldId2Ext(fieldId, idx);

info(custTrans.(fieldIdExt));

Accessing record fields – Axaptapedia

Comments are closed.