Skip to main content

Notifications

Announcements

No record found.

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

Get all the records in grid for iteration

(1) ShareShare
ReportReport
Posted on by 82
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 4,220 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,526 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 3,431 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
     
  • Community member Profile Picture
    Community member 82 on at
    Get all the records in grid for iteration
    Hi can you please suggest x++ code for the same, as you can see my code above i am storing the value in container and each time the container has value it stops the loop i mean throw error stops the screen, can you please suggest me x++ code for the scenario you are suggesting.
     
  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,526 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.
  • Community member Profile Picture
    Community member 82 on at
    Get all the records in grid for iteration
    Yes but multiselection can't help me here i am trying to skip the part to select checked records , so i need to use for loop to loop my data.
     
    the catch is after the throw error statement its stops and doesn't get the new record, how do i handle that? ( i tried adding ds.getnext() but it doesn't help) 
    can i please get Your inputs on that?
  • Layan Jwei Profile Picture
    Layan Jwei 4,220 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

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... 284,876 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,425 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,146

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans