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

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to Iterate Through Table Records one by one

(0) ShareShare
ReportReport
Posted on by

Hi all,

I want to create a logic which will Iterate through table records one by one and if a match found some action will be executed.

Please Let me know how could I do this. 

I tried (while select)  but Is fetching just the first record where as I want it to Iterate through all the table records till a match is found.

Please do the needful ,

Regards,

Nishath Salman N.

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    236,289 Most Valuable Professional on at
    RE: How to Iterate Through Table Records one by one

    while select surely can iterate multiple records. Either you use it incorrectly, or your query return a single record only.

    It's hard say without more information from you.

    Also, use where class if possible, because all record when you need only a few is a waste of resources.

  • Community Member Profile Picture
    on at
    RE: How to Iterate Through Table Records one by one

    Hi Martin,

    Sorry To response you late and thanks for your reply.

    I ll Provide you my Please Let me where I am committing mistake ?.

    BELOW PASTED IS MY CODE :

    select firstOnly10 RecId from dirPartyTable;

    //where dirPartyTable1.Name == Org_Name.text();

       while(dirPartyTable.RecId)

       {

                    select MPSalesOrderPool,MPSite,MPWarehouse,MPPaymTerms,MPPaymMethod,MPCashDisc from smmbusreltable1

                           where smmbusreltable1.RecId == dirPartyTable.RecId;

                   // while(dirPartyTable.RecId)

                         if((smmbusreltable1.MPSalesOrderPool != "")&&(smmbusreltable1.MPSite != "")

                              &&(smmbusreltable1.MPWarehouse != "")&&(smmbusreltable1.MPPaymTerms)

                              &&(smmbusreltable1.MPPaymMethod != "")&&(smmbusreltable1.MPCashDisc != ""))

                           {

                           SalesPoolId.text(smmbusreltable1.MPSalesOrderPool);

                           SalesOrder_InventSiteId.text(smmbusreltable1.MPSite);

                           InventLocation.text(smmbusreltable1.MPWarehouse);

                           Payment_PaymTermId.text(smmbusreltable1.MPPaymTerms);

                           Payment_PaymMode.text(smmbusreltable1.MPPaymMethod);

                           Payment_CashDisc.text(smmbusreltable1.MPCashDisc);

                           }

       }

                           info("Sorry!! No Such Record Found........");

    ---------------------------------------------------------------------------------------------------

    I HAVE COMMENTED THE LINE BELOW ( SELECT ) STATEMENT :

    //where dirPartyTable1.Name == Org_Name.text();

    --------------------------------------------------------------------------------------------------

    COZ IF HAVE USE IT THEN I AM GETTING BELOW ERROR :

    Error executing code: FormRun (data source) has no valid runable code in method 'init'.

    Stack trace

    (C)\Forms\CustTable\Methods\init

    (C)\Classes\SysSetupFormRun\init - line 3

    PLEASE DO THE NEEDFUL.

    REGARDS,

    NISHATH SALMAN

  • Community Member Profile Picture
    on at
    RE: How to Iterate Through Table Records one by one

    LET ME GIVE YOU SOME INFORMATION MARTIN.

    STEP 1: I HAVE CREATED A PROSPECT AND EDIT THE CUSTOMER FOR WHICH WILL THEN THE DATA GET INSERTED INTO TABLE (smmBusRelTable) - I have added some fields into the table.

    STEP 2:  now what i want is when I do Opportunity conversion to customer on customer table form some fields should get populated the same data what i have inserted earlier.

    NOTE :

    I have written assignment logic in the method ( relationtoCustomer())  of CustTable .

    Now when I do conversion from Prospect to Customer  my requirement is working fine . But I want the same to happen when I do Opportunity Conversion to customrer.

    But here Just let me know the mistake I have done in my code . Please

  • Suggested answer
    Martin Dráb Profile Picture
    236,289 Most Valuable Professional on at
    RE: How to Iterate Through Table Records one by one

    If you have at least one record in DirPartyTable, your code will produce an infinite loop.

    // This will set dirPartyTable.RecId to the ID of the first record.
    // For example, dirPartyRecId will be 123456789.
    select firstOnly10 RecId from dirPartyTable;
    
    // Your loop will run until dirPartyTable.RecId is zero.
    // But we said it's 123456789 and you never change the value again,
    // therefore it will run forever.
    while(dirPartyTable.RecId) {...}

    You indeed should do something like this:

    while select RecId from dirPartyTable
        where dirPartyTable.Name == Org_Name.text()
    {
        ...
    }
    

    The error "has no valid runable code" means that the code isn't compilable. Compile the method explicitly to see which compilation errors you have them and fix them before trying to run your code.

    Nevertheless it seems that you're trying to read a form control value when initializing a form, which doesn't make good sense. Users couldn't set such a value before your initialize and run the form.

    Also trying to set values of the same controls in the loop means that you'll keep overwriting them and you'll end up with values of the very last record only. It seems to me that you miss the whole concept of data sources - form controls are usually bound to tables and you merely modify the query used to fetch the data.

  • Suggested answer
    Mea_ Profile Picture
    60,284 on at
    RE: How to Iterate Through Table Records one by one

    Hey Nishath Salman,

    You need to use while select as Martin mentioned, take a look at this msdn article msdn.microsoft.com/.../aa558063.aspx

  • Martin Dráb Profile Picture
    236,289 Most Valuable Professional on at
    RE: How to Iterate Through Table Records one by one

    Your last reply suggests that your code has nothing to do with your actual requirement. If I understand it correctly, you want to set values of some custom fields on a single customer record, when creating it from an opportunity. Do you see anything like that in your code? Me neither, so please remove it and start working on what you actually require.

  • Community Member Profile Picture
    on at
    RE: How to Iterate Through Table Records one by one

    Hi Martin ,

    I have used (while select) statement as you said but I am getting error as below

    ERROR :

    FormStringControl object not initialized.

    Stack trace

    (C)\Forms\CustTable\Methods\init - line 24

    (C)\Classes\SysSetupFormRun\init - line 3

  • Martin Dráb Profile Picture
    236,289 Most Valuable Professional on at
    RE: How to Iterate Through Table Records one by one

    You clearly try to use a form control before it's initialized. But we know that you need something completely different, so it's not worth discussing in details. Remove your code from the form and focus on your actual requirement.

  • XB Profile Picture
    1,875 on at
    RE: How to Iterate Through Table Records one by one

    Hi

    Super(); on init , initializes form, controls , datasources (queries) so set your code after super and better set after super() on run.

    
                          

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

#2
Guy Terry Profile Picture

Guy Terry 2 Moderator

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans