Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / Get all the records in...
Finance forum

Get all the records in grid for iteration

Posted on by 99
Hi I am facing a issue while running my validation on EcoResCategoryAddProduct form. I have added the code at closedOk method but it selects only first product from the selected grid  but it actually should iterate for loop for all the products in selected product grid as u can see in below image it is throwing error for first product which ideally should should validate all the records from the selected grid
 

How do I change my for loop, to get all the records from selected grid?
 
Also how to do the db refresh?
 public void closeOk()
    {
        FormRun         callerForm = this.args().caller();
        EcoResProduct           selectedProduct;
        EcoResProductCategory   tmpEcoResProductCategory;
        EcoResProductCategory   existingEcoResProductCategory;
        container       productErrorMessages;
        if (callerForm && this.args().Caller().name() == formStr(EcoResCategory))
        {
            EcoResCategory ecoResCategory = callerForm.dataSource(formDataSourceStr(EcoResCategory, EcoResCategory)).cursor() as EcoResCategory;
            for ( selectedProduct = EcoResProductSelected_ds.getFirst(1)?EcoResProductSelected_ds.getFirst(1):EcoResProductSelected_ds.cursor();
            selectedProduct;
            selectedProduct = EcoResProductSelected_ds.getNext())
            {
                productErrorMessages += this.checkGroup(selectedProduct, ecoResCategory);
            }
            if(productErrorMessages != conNull())
            {
                throw error (strfmt(/@SYS82282/, productErrorMessages));
            }
        }
        next closeOk();
    }
 
  • Suggested answer
    Layan Jwei Profile Picture
    Layan Jwei 6,564 Super User on at
    Get all the records in grid for iteration
    Hi Community User,

    The issue is not with your throw error, as you are throwing an error after you sum all the errors.
    The issue is with the for loop -- you need to loop  for records in the selected grid
        public container productCatErrorMessage; /// this is the container for the checkGroup method
        public void closeOk()
        {
            FormRun         callerForm = this.args().caller();
            EcoResProduct           selectedProduct;
            EcoResProductCategory   tmpEcoResProductCategory;
            EcoResProductCategory   existingEcoResProductCategory;
            container       productErrorMessages;
            if (callerForm && this.args().Caller().name() == formStr(EcoResCategory))
            {
                            
                EcoResProductCategory   tmpEcoResProductCategory;
    
                tmpEcoResProductCategory.setTmp();
                tmpEcoResProductCategory.setTmpData(tmpEcoResProductCategorySelected);
    
    
                while select * from tmpEcoResProductCategory
                {
                    productErrorMessages += this.checkGroup(tmpEcoResProductCategory);
                }
                if(productErrorMessages != conNull())
                {
                    throw error (strfmt("@SYS82282", productErrorMessages));
                }
            }
            next closeOk();
        }


    now in checkGroup method, you can get EcoResCategory or EcoResProduct from tmpEcoResProductCategorySelected like this:

    EcoResCategory       ecoResCategorySelected = EcoResCategory::find(tmpEcoResProductCategorySelected.Category)
    EcoResProduct        ecoResProductSelected    = EcoResProduct::find(tmpEcoResProductCategorySelected.Product)
     
    The other issue is it seems your checkGroup method returns container, make sure that the definition of this container is global not inside the check group method. As i showed you, above the closeOk, so that you make sure all errors appear. if it was inside, it means the container in this method will reset each loop resulting in one message only

    let us know if it works

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,528 Super User on at
    Get all the records in grid for iteration
    Your multiple records looping code seems to be right for me. So for the error handling part, please check below example.
    List   errorList = new List(Types::str);
    str errorMessage;
    
    while() //loop
    {
        errorMessage = //pass error;
        errorList.addEnd(errorMessage);
    
    }
    
    if (!errorList.empty())
    {
       throw error("");//show the string value from the list
    }
     
  • Suggested answer
    Waed Ayyad Profile Picture
    Waed Ayyad 4,907 on at
    Get all the records in grid for iteration
     
    You can try this code.
     

    {
       YourTable tableBuffer;
       MultiSelectionHelper helper = MultiSelectionHelper::construct();
       helper.parmDatasource(YourTable_DS);
      tableBuffer = helper.getFirst();
      while (tableBuffer.RecId != 0)
      {
        //Your logic
        tableBuffer = helper.getNext();
     }
    }
     
    Thanks,
    Waed Ayyad
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
     
  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,528 Super User on at
    Get all the records in grid for iteration
    Throw error will always stop the process in any place like in batch process etc. One way you can try is, create a list and add all errors to the list and once the loop is done, check if list is having value. If it is having value, then in the end throw error using the values in list.
  • Layan Jwei Profile Picture
    Layan Jwei 6,564 Super User on at
    Get all the records in grid for iteration
    Hi CommunityUser,

    Did you try to look at the multiSelectionHelper class?

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future

Helpful resources

Quick Links

Dynamics 365 Community Update

Welcome to the inaugural Community Platform Update. As part of our commitment to…

Dynamics 365 Community Newsletter - August 2024

Catch up on the latest D365 Community news

Community Spotlight of the Month

Kudos to Waed Ayyad!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 289,995 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 227,174 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans