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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Find all relations of a certain field using tableId and fieldId

(0) ShareShare
ReportReport
Posted on by

Hi

I would like some help to create a loop that finds all related table relations (which are 2 in this case) using a tableId and a FieldId.
I have an issue that my code only finds the first relation instead of both relations 

As an example:
MainTable has field A with 2 relations:

Relation 1 MainTable.A = FirstRelation.A && MainTable.B = 2 
Relation 2 MainTable.A = SecondRelation.A && MainTable.B = 1 


So my code only finds FirstRelation and retuns the tableId of table  FirstRelation eg. tabelId = 16402

Could some one help me to write a Job that uses a tableId and fieldId that loops all the relations of this fieldId
 

Loop should be in the if statement.

static void findAllTableRelations(Args _args)
{
TableId                 _tableId;
FieldId                 _fieldId;
SysDictField            sysDictField;    
SysDictRelation         sysDictRelation; 

//real values of the table- and fieldId
_tableId                = 124670; //MainTable
_fieldId                = 60136; //field A

sysDictField            = new SysDictField(_tableId, _fieldId);
sysDictRelation         = new SysDictRelation(_tableId);


if (sysDictRelation)
{
        //this should be some kind of loop so that both related tables are found
        refTableId      = sysDictRelation.loadFieldRelation(sysDictField.id(), TableScope::IncludeBaseTables); 
        
        breakpoint;
}
}

Thanks in advance
Tomas


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Sukrut Parab Profile Picture
    71,710 Moderator on at

    This is how we can loop through all the fields of a table in X++ , you have to modify this to use SysDictRelation class  to find out all the relations.

    static void Job1(Args _args)

    {

       SysDictTable    dictTable = new SysDictTable(tableNum(PurchLine));

       SysDictField    dictField;

       TreeNode        treeNode;

       FieldId         fieldId = dictTable.fieldNext(0);

       while (fieldId)

       {

           dictField = dictTable.fieldObject(fieldId);

           if (dictField.isSql() && !dictField.isSystem())

           {

               treeNode = dictField.treeNode();

               info(strFmt("%1 | %2 | %3",

                   dictField.name(),                                               // Field name

                   treeNode.AOTgetProperty("Label"),                               // Label id

                   SysLabel::labelId2String(treeNode.AOTgetProperty("Label"))));   // Label string

           }

           fieldId = dictTable.fieldNext(fieldId);

       }

  • Community Member Profile Picture
    on at

    Hi Sergeant,

    I don't know if you have solved it, I found your question and I was looking for something similar.

    Maybe this can help, use the relations method and then get all the sysDictRelation.

    static void findAllTableRelations(Args _args)
    {
     int relationLine, allRelationInTable;
     TableId _tableId;
     FieldId _fieldId;

     SysDictTable sysDictTable;
     SysDictRelation sysDictRelation;

     Set tableRelationSet, result;
     SetEnumerator tableRelationSetEnumerator;

     _tableId = 124670;
     _fieldId = 60136;

     sysDictTable = new SysDictTable(_tableId);
     result = new Set(Types::Class);

     tableRelationSet = sysDictTable.relations();
     tableRelationSetEnumerator = tableRelationSet.getEnumerator();

     while( tableRelationSetEnumerator.moveNext() )
     {
       sysDictRelation = tableRelationSetEnumerator.current();
       allRelationInTable = sysDictRelation.lines();

       for ( relationLine = 1; relationLine <= allRelationInTable; relationLine ++ )
       {
         if( _fieldId == sysDictRelation.lineTableValue(relationLine))
         {
           result.add(sysDictRelation);
         }
       }
     }
    }

    hope this will help :)

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans