Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

While Select statement and Containers

Posted on by Microsoft Employee

Is there any way Containers can be used in a while select statement? This is what I want to achieve: 

Container Items;
PurchLine purchLine;
int i;
;
Items = [9109-0018,9109-0019,9109-0022,9109-0025];


for (i=1; i <= conlen(Items); i++)
{
while select PurchId from purchLine
where purchLine.ItemId == conpeek(custTransCont, i)
{

info(purchLine.PurchId);
}
}

But AX won't let me compare the value grabbed from conpeek. I guess it cannot... but wondering if you gents got any solution for it? Maybe I am not thinking it through

*This post is locked for comments

  • Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: While Select statement and Containers

    Note that the code is quite inefficient, because it executes a database query for each item in the container. A better approach would be constructing a query with several ranges and running it once. Like this:

    Query q = new Query();
    QueryRun qr;
    QueryBuildDataSource ds = q.addDataSource(tableNum(PurchLine));
    
    ds.addSelectionField(fieldNum(PurchLine, PurchId));
    
    for (i = 1; i <= conlen(items); i++)
    {
        itemId = conpeek(items, i);
        ds.addRange(fieldNum(PurchLine, ItemId).value(queryValue(itemId));
    }
    
    qr = new QueryRun(q);
    
    while (qr.next())
    {
        PurchLine purchLine = qr.get(tableNum(PurchLine));
        info(purchLine.PurchId);
    }

    By the way, I would avoid containers unless there is a good reason for using them. A set of strings would be a good alternative in this case.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: While Select statement and Containers

    I don't understand but where did the custTransCont come from?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: While Select statement and Containers

    Oh my god, I feel so dumb.

    Thanks!

  • Verified answer
    André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,280 Super User 2024 Season 2 on at
    RE: While Select statement and Containers

    Hi Khosla,

    You can create a variable where you first store the Item from the container. This variable can be used in the select statement as workaround, so try this:

    Container Items;

    PurchLine purchLine;

    ItemId     itemId;

    int i;

    ;

    Items = [9109-0018,9109-0019,9109-0022,9109-0025];

    for (i=1; i <= conlen(Items); i++)

    {

    itemId = conpeek(custTransCont, i);

    while select PurchId from purchLine

    where purchLine.ItemId == itemId

    {

    info(purchLine.PurchId);

    }

    }

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 Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans