I receive a record in table variable, for example DirPartyRelationship, and must perform some logic in case the record is valid now and another logic in case the record is not-valid.
It is possible to compare ValidFrom and ValidTo fields, however I expect some system method like isValidNow or some better approach exists.
I found nothing in Global, xRecord or DateTimeUtil classes.
Could you recommend such system method?
*This post is locked for comments
Hi Dominic,
Thank you very much for the reply.
As the record with data in ValidFrom and ValidTo fields is available, I wanted to avoid additional database call.
I wrote a method in Global class:
public static boolean isRecordValidNow(Common _record) { DictTable dictTable; FieldId validFromFieldId, validToFieldId; DictField dictField; utcdatetime currentDateTime; date currentDate; boolean ret; if (_record.TableId) { dictTable = new DictTable(_record.TableId); // if date effective is enabled on table - compare Valid fields if (dictTable && dictTable.isValidTimeStateTable()) { validFromFieldId = dictTable.getValidTimeStateValidFromFieldId(); validToFieldId = dictTable.getValidTimeStateValidToFieldId(); dictField = new DictField(_record.TableId, validFromFieldId); if (dictField) { // compare valid fields based on base type of date effective fields if (dictField.baseType() == Types::UtcDateTime) { // utcNow is used by Kernel when ValidTimeStateFieldType = utcDateTime currentDateTime = DateTimeUtil::utcNow(); if (_record.(validFromFieldId) <= currentDateTime && _record.(validToFieldId) >= currentDateTime) { ret = true; } } else if (dictField.baseType() == Types::Date) { // today is used by Kernel when ValidTimeStateFieldType = Date currentDate = today(); if (_record.(validFromFieldId) <= currentDateTime && _record.(validToFieldId) >= currentDateTime) { ret = true; } } } } } return ret; }
However my approach is not safe as ValidTimeState logic can be changed in next versions of AX. Your suggestion costs more, but is more safe.
By the way, while working on this method I came to another question https://community.dynamics.com/ax/f/33/p/155091/362930.aspx, on which you also submitted your ideas.
Thank you!
Hi Oleg,
I don't know of any system method that does that. But you can add the method below in Global to check whether a record is valid now.
public static NoYesId isValidNow(Common _common) { DictTable dt = DictTable::construct(tableId2name(_common.TableId)); Common buffer; if (!dt.isValidTimeStateTable()) throw error("This is not a valid time state table"); buffer = dt.makeRecord(); select firstOnly RecId from buffer where buffer.RecId == _common.RecId; return (buffer.RecId != 0); }
André Arnaud de Cal...
292,162
Super User 2025 Season 1
Martin Dráb
230,962
Most Valuable Professional
nmaenpaa
101,156