cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jonathanO
Level: Powered On

Remove() removing all items in ForAll()

Hi All,

I have been stuck on a problem that I hope you can help with. It's a long post, so hopefully you've had some coffee Smiley Happy

I have SQL table that tracks which applications were affected during a system outage. The user clicks into an incident (which represents some sort of outage) and is shown a gallery with a list of applications. The OnSelect property of the gallery maintains a collection to track which applications the user has clicked in the gallery. When the user presses the Save button, the PowerApp needs to insert the records to the database table. I have a solution for this part.

Here is where it get's complicated: A user can come back and make changes to which applications were affected by an outage as they get more information from our users. Therefore, the user needs to be able to select and deselect applications. Each time the user presses Save, the PowerApp needs to identify which are currently selected in the gallery, which are currently in the database table and then either insert/remove based on the discrepancies.

The problem I am having is with the removal. For example, if a user has selected 5 applications and presses save, the PowerApp correctly inserts 5 records for that incident. However, if they come back into the incident and deselect 1 application so that only 4 remain selected, all are removed.

Here is the formula for the OnSelect of the Save button:

//Get the existing applications affected
ClearCollect(
    ExistingApplicationsAffected,
    Filter(
        '[incidentlog].[tblApplicationAffected]',
        IncidentID = Incident.IncidentID
    )
));

// AffectedApplicationCollection is the collection that the gallery maintains when a user selects/deselects an item in the gallery. The If() checks if the Application is already in the table, and if not, inserts it.
ForAll(
    AffectedApplicationCollection,
    If(
        IsEmpty(
            Filter(
                '[incidentlog].[tblApplicationAffected]',
                ApplicationID = AffectedApplicationCollection[@ApplicationID] && IncidentID = Incident.IncidentID
            )
        ),
        Patch(
            '[incidentlog].[tblApplicationAffected]',
            Defaults('[incidentlog].[tblApplicationAffected]'),
            {
                ApplicationID: ApplicationID,
                IncidentID: Incident.IncidentID,
                RowLoadedDatetime: Now(),
                RowUpdatedDatetime: Now(),
                RowCreatedByEmpID: ADUserProfile.employeeId,
                LastUpdatedByEmpID: ADUserProfile.employeeId
            }
        )
    )
);

 

//We separately loop through the existing applications affected to see if they are in the collection maintained by the gallery to see if the user left them selected, if not, remove them.
ForAll(
    ExistingApplicationsAffected,
    If(
        Not(ExistingApplicationsAffected[@ApplicationID] in AffectedApplicationCollection.ApplicationID) && IncidentID = Incident.IncidentID,
        Remove(
            '[incidentlog].[tblApplicationAffected]',
            Filter(
                ExistingApplicationsAffected,
                ApplicationID = ExistingApplicationsAffected[@ApplicationID] && IncidentID = Incident.IncidentID
            )
        )
    )
);


My two questions are this:

1. I know that having two ForAll() statements can't be the most efficient way to do this. Anybody know how both ForAll() statements can be combined?

2. More importantly, the problem seems to be this part of the 2nd ForAll(): 

ForAll(
    ExistingApplicationsAffected,
    If(
        Not(ExistingApplicationsAffected[@ApplicationID] in AffectedApplicationCollection.ApplicationID) && IncidentID = Incident.IncidentID,
        Remove(
            '[incidentlog].[tblApplicationAffected]',
            Filter(
                ExistingApplicationsAffected,
                ApplicationID = ExistingApplicationsAffected[@ApplicationID] && IncidentID = Incident.IncidentID
            )
        )
    )
);



I have tested the If() statement in the 2nd ForAll() and it evaluates true only when a record in the existing application affected collection does not exist in table, so that's not the problem. The problem is the Filter() for the Remove expression evaluates true for all records in the ExistingApplicationAffected collection. I was under the impression that ApplicationID should be scoped to the current record of the collection being evaluated by the ForAll() function. I even used a Disambiguation operator and that did not help. 

Any know why that filter is removing all records?
 

1 REPLY 1
jonathanO
Level: Powered On

Re: Remove() removing all items in ForAll()

I solved this by renaming the primary key column of ExistingApplicationsAffected and changing the 2nd ForAll() to:

ForAll(
    TempExistingApplicationsAffected,
    If(
        Not(TempApplicationID in AffectedApplicationCollection.ApplicationID) && IncidentID = Incident.IncidentID,
        Remove(
            '[incidentlog].[tblApplicationAffected]',
                {ApplicationAffectedID:TempApplicationAffectedID}
        )
    )
);

 

Helpful resources

Announcements
firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

SecondImage

PowerApps Monthly Community Call

Next Wednesday, August 21st at 8am PDT

Top Community Contributors for July 2019

Top Community Contributors for July 2019

Let's thank our top community contributors

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

FifthImage

Dynamics 365 and Power Platform April 2019 Release notes

Features releasing from April 2019 through September 2019!

SixthImage

Power Summit Australia 2019

August 20-23rd 2019

Users Online
Currently online: 17 members 4,248 guests
Please welcome our newest community members: