Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

X++ Possible to get table relationship, then identify the ReplacementKey field to perform logic

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I have to perform certain business logic based on the value of a field on a table.  

Table1.FIeld1 we will call it.  Field1 is a RefRecId in reference to:

Table2.RecId.

On the form that Table1 is a datasource for it shows as Table2.Name.

The table and field can be dynamic but I need to be able to:

Look at Table1.Field1 and find what table the recId is referencing.

FInd the record in Table2 based on that recId and some how get to the Table2.name value.  I believe this is set up through the replacement key index.

Is there a way to do this through X++ code?  I've been playing around with dictRelation, dictField class but not seeing any methods that get me anywhere.

Thanks

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: X++ Possible to get table relationship, then identify the ReplacementKey field to perform logic

    Thank you @Diluted. I was looking for the same solution my case and it helped me too.

    Also maybe someone looks for enum values. Id like to add that too.

       public static str getEnumValue(EnumId _enumId, int _enumValue)

       {  

           DictEnum    dictEnum;

           str                ret;

           dictEnum = new DictEnum(_enumId);

           ret            = dictEnum.value2Name(_enumValue);

           return ret;

       }

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: X++ Possible to get table relationship, then identify the ReplacementKey field to perform logic
    private str getRecIdValue(TableId _tableId, FieldId _fieldId, RefRecId _refRecId)
    {
        DictTable       dictTable;
        DictRelation    dictRelation;
        DictIndex       dictIndex;
        TableId         relTable;
        FieldId         relField;
        Common          common;
        str             ret;
        
        dictRelation = new DictRelation(_tableId);    
        dictRelation.loadFieldRelation(_fieldId);
        relTable = dictRelation.externTable();
        
        dictTable = new DictTable(relTable);
        
        common = dictTable.makeRecord();
        select common where common.recId == _refRecId;
        
        dictIndex = new dictIndex(relTable, dictTable.replacementKey());
        relField = dictIndex.field(1); //Replacement key should only have 1 value
        
        ret = common.(relField);
        
        return ret;
    }

    The above code I wrote resolved my issue and allowed me to grab the value of the replacement key to the related table based on the RecId I am given from the user.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: X++ Possible to get table relationship, then identify the ReplacementKey field to perform logic

    I will update as I find more but by using dictRelation to get the related table through load FieldRelation, I can use dictTable to get the replacement key index through the replacementKey method.  I think this will allow me to get the field by just grabbing the first field from the index through dictIndex.Field.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans