cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Joao_Macau
Frequent Visitor

ForAll -> IF Statement -> Remove row from sharepoint (Problem)

Hello to all!

 

I'm trying to build an action that defines what to do to a sharepoint list in function of a column (Ação) that I have in a collection (collectionA). 

 

In my CollectionA, I've a column (Ação) with three types of string records:

"Equal"

"Add"

"Change"

"Remove"

 

 

ForAll(ColletionA;
If(Ação="Add";
Patch('MaR.OOP';Defaults('MaR.OOP');{Title:ThisRecord.OOP;'0A ID OE':ThisRecord.OE;'AF Valor':ThisRecord.Novo});
    
    If(Ação="Change";
Patch('MaR.OOP';LookUp('8.MaROOP';Concatenate(OOP;OE)=Title&'0A ID OE');{'AF Valor':ThisRecord.Novo});

        If(Ação="Remove";
        Remove('MaR.OOP';LookUp('8.MaROOP';Concatenate(OOP;OE)=Title&'0A ID OE';ID))

Refresh('MaR.OOP');;ClearCollect('8.MaROOP';'MaR.OOP');;

 

 

 

 

At the moment my formula function works well for all except the last one... The Remove Action.

 

I calling the ID to be removed with a lookup (in a ForAll loop) but i cant understand why this lookup if providing me an error... in the action "Change" worked perfectly.. 

 

'MaR.OOP' -  is my sharepoint List

'8.MarOOP' - is the Collection that i'm using to collect the value of 'MaR.OOP'

 

 

Can anyone help me? 

 

thank very much 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@Joao_Macau 

A couple of things:

Your problem is that ForAll is not a For/Loop.  It is a formula that creates tables based on the information provided.

The information you are providing is a Record (from the first If) a Record from the second if, and the Remove will return a Table.  So the inner record of your ForAll is mismatched and not valid.

 

You mentioned two collections, but your formula also uses a "CollectionA".  What is the relevance of that?

 

You need not create multiple collections of the same information as this can all be achieve with 1 or no collections.

 

Consider RemoveIf instead of the Remove function as your success in matching a record will be much higher.

RemoveIf should reference the primary key of the list to delete from - in your case ID.

Although you can RemoveIf by other criteria, the ID is the most reliable as your list grows in size.

 

Your ThisRecord references are referencing the wrong values in your current formula.  ThisRecord needs to be used with caution as it references the most recent record in the formula chain.  So for example in your "change" logic, it is referencing the LookUp values, not the record from the table iteration of the ForAll.

 

Please consider changing your Formula to the following:

Collect('MaR.OOP';
    ForAll(
        Filter(ColletionA; Ação="Add") As _item;
        {Title: _item.OOP; '0A ID OE': _item.OE; 'AF Valor':_item.Novo}
    )
);;
Patch('MaR.OOP',
    ForAll(
        Filter(ColletionA; Ação="Change") As _item;
            {ID: _item.ID;   
             'AF Valor': _item.Novo}
        )
    )
);;    
ForAll(
    Filter(ColletionA; Ação="Remove") As _item;
    RemoveIf('MaR.OOP'; ID=_item.ID)
)

Also, you do NOT need the costly Refresh on your list that you had in the formula.  The changes you make will be immediately reflected in your app.

 

The above formula assumes that you have the information you need for your records in the CollectionA collection.  For simplicity of your formula, I would strongly consider making sure that collection has the values you want rather than doing another lookup to another collection to get the same values.

IF for some reason that is by design, then let me know and we can alter the above to fit that.

 

I hope this is helpful for you.

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

3 REPLIES 3
RandyHayes
Super User
Super User

@Joao_Macau 

A couple of things:

Your problem is that ForAll is not a For/Loop.  It is a formula that creates tables based on the information provided.

The information you are providing is a Record (from the first If) a Record from the second if, and the Remove will return a Table.  So the inner record of your ForAll is mismatched and not valid.

 

You mentioned two collections, but your formula also uses a "CollectionA".  What is the relevance of that?

 

You need not create multiple collections of the same information as this can all be achieve with 1 or no collections.

 

Consider RemoveIf instead of the Remove function as your success in matching a record will be much higher.

RemoveIf should reference the primary key of the list to delete from - in your case ID.

Although you can RemoveIf by other criteria, the ID is the most reliable as your list grows in size.

 

Your ThisRecord references are referencing the wrong values in your current formula.  ThisRecord needs to be used with caution as it references the most recent record in the formula chain.  So for example in your "change" logic, it is referencing the LookUp values, not the record from the table iteration of the ForAll.

 

Please consider changing your Formula to the following:

Collect('MaR.OOP';
    ForAll(
        Filter(ColletionA; Ação="Add") As _item;
        {Title: _item.OOP; '0A ID OE': _item.OE; 'AF Valor':_item.Novo}
    )
);;
Patch('MaR.OOP',
    ForAll(
        Filter(ColletionA; Ação="Change") As _item;
            {ID: _item.ID;   
             'AF Valor': _item.Novo}
        )
    )
);;    
ForAll(
    Filter(ColletionA; Ação="Remove") As _item;
    RemoveIf('MaR.OOP'; ID=_item.ID)
)

Also, you do NOT need the costly Refresh on your list that you had in the formula.  The changes you make will be immediately reflected in your app.

 

The above formula assumes that you have the information you need for your records in the CollectionA collection.  For simplicity of your formula, I would strongly consider making sure that collection has the values you want rather than doing another lookup to another collection to get the same values.

IF for some reason that is by design, then let me know and we can alter the above to fit that.

 

I hope this is helpful for you.

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

Joao_Macau
Frequent Visitor

Thank you so much for the reply!!! It worked

 

Also you open a new world for me! 

thank very much sir!

RandyHayes
Super User
Super User

@Joao_Macau 

Happy to help!

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Users online (2,254)