cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
trjackson77
Resolver I
Resolver I

Updating a collection row using onChange not working

Hi I have 3 text boxes bound to a collection. 

 

txtCount1-3 are bound to colCount1-3

 

I have lblMasterAdjust bound to colMasterAdjust

 

OnChange of either txtCount1-3 I want to calculate the value of colMasterAdjust and update its label.  I want to take the last non-blank (or "") txtCount value to calculate colMasterAdjust.

 

E.G.:

if txtCount3 is not blank then use colCount3 in calc

if txtCount3 is blank but txtCount2 is not then use txtCount2 as calc

txtCountt1 is to be used if txtCount2-3 are blank.

 

The code I am using on the onChange property of txtCount1-3 is:

 

Patch(
    colCount,
    galCount.Selected,
    {
        Count1: txtCount1.Text,
        LVAdjust1: txtCount1.Text - galStocktakeItems.Selected.WarehouseQuantity
    }
);
If(
    IsBlank(ThisItem.Count3) && IsBlank(ThisItem.Count2),
    Patch(
        colCount,
        ThisItem,
        {LVMasterAdjust: ThisItem.Count1}
    ),
    If(
        IsBlank(ThisItem.Count3),
        Patch(
            colCount,
            ThisItem,
            {LVMasterAdjust: ThisItem.Count2}
        ),
        Patch(
            colCount,
            ThisItem,
            {LVMasterAdjust: ThisItem.Count3}
        )
    )
)

 

 

However, I can't get either colMasterAdjust or its label to update with the onChange event.

 

Please can someone point me in the right direction?

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @trjackson77 ,

 

Yes, since your example describes three situation, so I configure the If logic formula on three criteria.

Please let me know if the code works.

Sik

View solution in original post

5 REPLIES 5
trjackson77
Resolver I
Resolver I

UPDATE:

I have refactored some of the code and managed to pin down the error to the patch statement in the second if statement (the first if statement patch works fine).

 

The second patch creates a new record rather than patching the existing record.  I can't spot the difference between the patching in the first and second if statements.

 

If(
    txtCount3.Text <> "",
    Patch(
        colCount,
        galCount.Selected,
        {
            Count3: txtCount3.Text,
            LVAdjust3: txtCount3.Text - galStocktakeItems.Selected.WarehouseQuantity
        }
    ),
    Patch(
        colCount,
        galCount.Selected,
        {
            Count3: "",
            LVAdjust3: ""
        }
    )
);
If(
    IsBlank(txtCount3.Text) && IsBlank(txtCount3.Text),
    Patch(
        colCount,
        galCount.Selected,
        {LVMasterAdjust: txtCount1.Text- galStocktakeItems.Selected.WarehouseQuantity}
    ),
    If(
        IsBlank(txtCount3.Text),
        Patch(
            colCount,
            galCount.Selected,
            {LVMasterAdjust: txtCount2.Text- galStocktakeItems.Selected.WarehouseQuantity}
        ),
        Patch(
            colCount,
            galCount.Selected,
            {LVMasterAdjust: txtCount3.Text- galStocktakeItems.Selected.WarehouseQuantity}
        )
    )
)
v-siky-msft
Community Support
Community Support

Hi @trjackson77 ,

 

 

I just follow the logic of the example you post and then modify your If formula as follows. Please have a try.

 

If(

!IsBlank(txtCount3.Text), 

Patch(colCount,galCount.Selected,{LVMasterAdjust: txtCount3.Text- galStocktakeItems.Selected.WarehouseQuantity})

IsBlank(txtCount3.Text)&& !IsBlank(txtCount2.Text),

Patch(colCount,galCount.Selected,{LVMasterAdjust: txtCount2.Text- galStocktakeItems.Selected.WarehouseQuantity})

IsBlank(txtCount3.Text)&& IsBlank(txtCount2.Text),

Patch(colCount,galCount.Selected,{LVMasterAdjust: txtCount1.Text- galStocktakeItems.Selected.WarehouseQuantity})
)

 

If the issue that create a new record still exist, please try to lookup function to retrieve the record you want to modify, for example:

Patch(colCount,LookUp(colCount, ID=galCount.Selected.ID),{LVMasterAdjust: txtCount3.Text- galStocktakeItems.Selected.WarehouseQuantity})

 

Hope this helps.

Sik

Thanks for your help on this Sik.  If I'm looking correctly have you merged the logic of the two if statements into one if statement?

Hi @trjackson77 ,

 

Yes, since your example describes three situation, so I configure the If logic formula on three criteria.

Please let me know if the code works.

Sik

Sik,

    thanks for your help on this.  Your logic put me on the right track to refactor and simplify the code further.  The final code was as follows:

 

If(
    IsBlank(txtCount1.Text) && IsBlank(txtCount2.Text) && IsBlank(txtCount3.Text),
    Patch(
        colCount,
        First(
            Filter(
                colCount,
                ProductCode = galCount.Selected.ProductCode
            )
        ),
        {
            Count1: Value(txtCount1.Text),
            LVAdjust1: Value(txtCount1.Text - galStocktakeItems.Selected.WarehouseQuantity),
            LVMasterAdjust: ""
        }
    ),
    If(
        IsBlank(txtCount2.Text) && IsBlank(txtCount3.Text),
        Patch(
            colCount,
            First(
                Filter(
                    colCount,
                    ProductCode = galCount.Selected.ProductCode
                )
            ),
            {
                Count1: Value(txtCount1.Text),
                LVAdjust1: Value(txtCount1.Text - galStocktakeItems.Selected.WarehouseQuantity),
                LVMasterAdjust: Value(txtCount1.Text - galStocktakeItems.Selected.WarehouseQuantity)
            }
        ),
        If(
            IsBlank(txtCount3.Text),
            Patch(
                colCount,
                First(
                    Filter(
                        colCount,
                        ProductCode = galCount.Selected.ProductCode
                    )
                ),
                {
                    Count1: Value(txtCount1.Text),
                    LVAdjust1: Value(txtCount1.Text - galStocktakeItems.Selected.WarehouseQuantity),
                    LVMasterAdjust: Value(txtCount2.Text - galStocktakeItems.Selected.WarehouseQuantity)
                }
            ),
            Patch(
                colCount,
                First(
                    Filter(
                        colCount,
                        ProductCode = galCount.Selected.ProductCode
                    )
                ),
                {
                    Count1: Value(txtCount1.Text),
                    LVAdjust1: Value(txtCount1.Text - galStocktakeItems.Selected.WarehouseQuantity),
                    LVMasterAdjust: Value(txtCount3.Text - galStocktakeItems.Selected.WarehouseQuantity)
                }
            )
        )
    )
)

 

Thanks Again.

Helpful resources

Announcements
Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Users online (1,408)