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)

Loop through a container to populate lookup in a field on a form

(0) ShareShare
ReportReport
Posted on by 213

Hi

Some help for a DAX newbie please!

I have a lookup method on a field that displays a field from the database, code as follows:

    Query                   query = new Query();
    QueryBuildDataSource    qbds;
    QueryBuildRange         qbr1;
    QueryBuildRange         qbr2;
    QueryBuildRange         qbr3;

    SysTableLookup  sysTableLookup;

    qbds = query.addDataSource( tableNum(DocuRef));
    qbr1 = qbds.addRange( fieldNum(DocuRef,TypeId));
    qbr2 = qbds.addRange( fieldNum(DocuRef,RefTableId));

    qbr1.value('SALESDOC');
    qbr2.value('359');

    qbds = qbds.addDataSource( tableNum(SalesLine));
    qbds.joinMode(JoinMode::InnerJoin);
    qbds.relations(true);
    qbds = qbds.addDataSource( tableNum(ProdTable));
    qbds.joinMode(JoinMode::InnerJoin);
    qbds.relations(true);

    qbr3 = qbds.addRange( fieldNum(ProdTable,ProdId));
    qbr3.value(queryValue(_prodId));

   // qbr1 = q.addDataSource(tableNum(DocuRef));
    //qbr1.addRange(fieldNum(ProdTable, ProdId)).value();

    sysTableLookup  = SysTableLookup::newParameters(tableNum(DocuRef), _formControl);

    sysTableLookup.addLookupfield(fieldNum(DocuRef, Notes), true);
    sysTableLookup.addLookupfield(fieldNum(DocuRef, TypeId), false);

    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();


This works fine, except for the fact the field "Notes" in the DocuRef table in my system is a carriage return delimited list (so only one selection is returned from the dropdown).

I have been able to split the field "Notes" in a Job as follows:

    queryRun = new QueryRun(query);

    // Retrieves the next record from the query.
    while(queryRun.next())
    {
        // Get Result
        docuref = queryRun.get( tableNum(DocuRef));
        
        notes = docuref.Notes;
        
        _list = Global::strSplit(notes,'\n');
        iterator = new ListIterator(_list);
        
        while (iterator.more())
        {
            packList += iterator.value();
            iterator.next();
        }
        
        for (i = 1; i <= conlen(packList); i++)
        {
            info(conPeek(packList,i));   
        }
         
    }

My question is, how can I combine my two pieces of a code?

For example, if the Notes field contains:

tag1

tag2

tag3

How can I get three seprate entries in the dropdown?  At the moment I get:

tag1tag2tag3

Many thanks in advance

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,884 Most Valuable Professional on at

    It seems that your grid shows a single line only, therefore I see two options:

    1. Remove line breaks, therefore the fact that only the first line is displayed won't be any limitation anymore.
    2. Change the form to show more than a single line.

    It seems that you're trying to implement the first scenario, but you can use a much simpler approach: strReplace() can replace line breaks with spaces, or anything you like. Also, I suggest you create a display method for this and use it (addLookupMethod()) in the lookup instead of Notes field. That's likely the easiest way possible.

  • cjohnson300 Profile Picture
    213 on at

    Hi Martin

    Thanks for replying to my question.  I'm actually trying to implement the 2nd scenario you mentioned,  turning the line break delimited string into a multi line drop down in the field.  How would I accomplish that?

  • Verified answer
    Mea_ Profile Picture
    60,284 on at

    Hi cjohnson300,

    You can populate a temp table with data you want to lookup and create lookup by temp table, please refer to this blog for code example ax2012madhav.blogspot.co.nz/.../lookup-for-temporary-table.html

  • Martin Dráb Profile Picture
    237,884 Most Valuable Professional on at

    I think you don't understand that AX is able to display strings with multiple lines, so your code doesn't do anything useful. The data is already all right; my second scenario is about displaying more than a single line (usual grids don't show multiple lines to keep the GUI compact, but it doesn't mean that you can't do it differently). If you want to go this way, create a new lookup form and set properties (MultiLine and DisplayHeight) of the form control bound to Notes field as appropriate.

  • cjohnson300 Profile Picture
    213 on at

    I understand, but I'm looking to split the string in my example into a drop down box offering the user 3 choices (tag 1, tag 2, tag 3) from the single multiline field.  So although only 1 field and 1 row is being returned by the query,  I want to make it into 3 rows in the drop down

  • Verified answer
    Martin Dráb Profile Picture
    237,884 Most Valuable Professional on at

    Aha, I didn't get it. In that case, I think that using a temporary table is indeed the best way.

  • cjohnson300 Profile Picture
    213 on at

    Many thanks for your input, I now have it working as I had hoped.  In case it helps anyone else in the future, here is my code (all in lookup method):

    public client static void lookupPackHierarchy(FormControl _formControl, str _filterStr, ProdId _prodId, boolean _isProjBudgetLineTypeRevenue = false)
    {
        SysTableLookup          SysTableLookup;
        Query                   query;
        QueryBuildDataSource    qbds;
        LabelTmp                tmpTable;
       
        DocuRef                 dr;
        SalesLine               sl;
        ProdTable               wo;
        
        str                     notes;
        str                     packList;
        ListIterator            iterator;
        List                    _list = new List(Types::String);
        int                     i;
        ;
        
        while select * from dr
            join sl where sl.dataAreaId == dr.RefCompanyId &&
                sl.TableId == dr.RefTableId &&
                sl.recid == dr.RefRecId &&
                dr.TypeId == 'LABEL' &&
                dr.RefTableId == 359
            join wo where wo.InventRefTransId == sl.inventtransid &&
                wo.inventrefid == sl.SalesId &&
                wo.Prodid == _prodId
    
        {
            notes = dr.Notes;
    
            _list = Global::strSplit(notes,'\n');
            iterator = new ListIterator(_list);
    
            while (iterator.more())
            {
                packList = iterator.value();
                tmpTable.Label = packList;
                tmpTable.insert();
                
                //info(strFmt("Inserted a row = %1",packlist));
                
                iterator.next();
            }  
    
        }
       
        SysTableLookup = SysTableLookup::newParameters(tablenum(LabelTmp), _formControl);
        Query = new Query();
        qbds = query.addDataSource(tablenum(LabelTmp));
    
        sysTableLookup.addLookupfield(fieldnum(LabelTmp, Label), true);
    
        sysTableLookup.parmQuery(query);
        // DataSource on form with populated data via .setTmpData():
        sysTableLookup.parmTmpBuffer(tmpTable);
        sysTableLookup.performFormLookup();
       
        
    }


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