Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

How to check that a record in table variable with data-effective enabled is valid now?

(0) ShareShare
ReportReport
Posted on by 9

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

  • Oleg Abapolov Profile Picture
    Oleg Abapolov 9 on at
    RE: How to check that a record in table variable with data-effective enabled is valid now?

    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!

  • Suggested answer
    dolee Profile Picture
    dolee 11,279 on at
    RE: How to check that a record in table variable with data-effective enabled is valid now?

    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);
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,162 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,962 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans