Skip to main content

Notifications

Microsoft Dynamics AX forum
Answered

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

  • Martin Dráb Profile Picture
    Martin Dráb 225,000 Super User 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... 283,682 Super User 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);

    }

    }

Helpful resources

Quick Links

Community Spotlight of the Month

Kudos to Mohamed Amine Mahmoudi!

Blog subscriptions now enabled!

Follow your favorite blogs

TechTalk: How Dataverse and Microsoft Fabric powers ...

Explore the latest advancements in data export and integration within ...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 283,682 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,000 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,146

Featured topics

Product updates

Dynamics 365 release plans