web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
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 39

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

I have the same question (0)
  • Suggested answer
    dolee Profile Picture
    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);
    }

  • Oleg Abapolov Profile Picture
    39 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!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#2
Community Member Profile Picture

Community Member 2

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans