Skip to main content

Notifications

Power Apps Pro Dev & ISV
Suggested answer

how to use ForAll to check the condition for each record from two different tables

Posted on by 7
Hi there,
I am new to Power Apps and I need to make a code change on the existing Canvas App using the formula (Power Fx).
Since the developer who worked on this has retired and we have tight deadline to work on adding new features to old code base which was never released.
The lead of the project suggested I post a question on the power app forum. 
 
The purpose of this Canvas app is to recognize employees' good work by awarding them a gift card. This task is to check if the employee already has an award in a pending state.
Then comparing the "Employee_Email" column from award table with "employee_email" in the employeeList. I believe powerFx is case sensitive. 
 
With my lead's help, I got it to work when only one employee checked. But if there are more than one employee is selected for awards, I have to loop through
the collection "colEmpInfo" which is a collection of Employee. I added my question in the code comment as TODO. I also attached the code to submit the award form for one and multiple employee
If(
 
        CountRows(Filter(Award, Lower(Status.Value)="pending" && Employee_Email = First(colEmpInfo).employee_email ) ) < 1,
 
Below is the completed code for "OnSelect"
 
Switch(
 
    CountRows(colEmpInfo),
 
    0,
 
    UpdateContext({varPopupPendMsgFlag: true});
 
    UpdateContext({varPopupPendTitle: "Attention"});
 
    UpdateContext({varPopupPendMsg: "You have not selected any employee for requesting award"});
 
    UpdateContext({varPopupPendMsgColor: RGBA(255,0,0,1)}),
 
    1,
 
    If(
 
        CountRows(Filter(Award, Lower(Status.Value)="pending" && Employee_Email = First(colEmpInfo).employee_email ) ) < 1,
 
        //ThisItem.employee_email
 
        Navigate(
 
            'Award New Form',
 
            ScreenTransition.Cover,
 
            {varAccountNo: drpReqEmpAwdAccNum.SelectedText.Value}
 
        ),
 
        UpdateContext({varPopupPendMsgFlag: true});
 
        UpdateContext({varPopupPendTitle: "Attention"});
 
        UpdateContext({varPopupPendMsg: "You have a pending award request for the selected employee : " & First(colEmpInfo).employee_email });
 
        UpdateContext({varPopupPendMsgColor: RGBA(255,0,0,1)});
        Clear(colEmpInfo);
 
    ), //selected one employee based on Award count
 
    // if there are more than one employee selected, we
    // need to loop through the collection colEmpInfo
    ForAll( colEmpInfo,
    If( CountRows(Filter(Award, Lower(Status.Value)="pending" && Employee_Email = First(colEmpInfo).employee_email ) ) < 1,
    {
       //TODO, we cannot use the First(colEmpInfo) because we need to iterate through the collection of employee Info
       // but I am not sure the syntax of powerFx. In Java, there is enhanced for loop which I can just declare an element
       // I was trying to follow the below example but I noticed in this documentation, the collection was created using Product
       /**
       ClearCollect( Products,
        Table(
            { Product: "Widget", 'Quantity Requested': 6, 'Quantity Available': 3 },
            { Product: "Gadget", 'Quantity Requested': 10, 'Quantity Available': 20 },
            { Product: "Gizmo", 'Quantity Requested': 4, 'Quantity Available': 11 },
            { Product: "Apparatus", 'Quantity Requested': 7, 'Quantity Available': 6 }
         )
        )
       ForAll( Products,
       If( 'Quantity Requested' > 'Quantity Available',
         {
            Product: Product,
            'Quantity To Order': 'Quantity Requested' - 'Quantity Available'
          }
         )
       )
       */
    }
 )
);
 
    Navigate(
 
        'Multiple Awards New Form',
 
        ScreenTransition.Cover,
 
        {varAwdType: "emp"}
 
    ) //selected multiple employees
 
);
 
UpdateContext({resetChkEmpAwdBody: true});
 
UpdateContext({resetChkEmpAwdBody: false});
 
UpdateContext({resetNameSearch: true});
 
UpdateContext({resetNameSearch: false});
  • ZL-11092118-0 Profile Picture
    ZL-11092118-0 7 on at
    how to use ForAll function to check condition instead of patch
    Thanks for the suggestion. But unfortunately, I cannot use ThisRecord.
     
    The collection "colEmpInfo" is for the selected employee which I want to give award. Not the entire employee list.
    So when I selected only one employee based on Award count, "colEmpInfo" will only have one element which is always First(colEmpInfo).employee_email. I know this is not ideal. But I worked with my team lead, but we could not figure out how to the current record. You suggested to use ThisRecord but he was trying to use ThisItem.
    However, ThisItem is not available in the context, even though he did show me ThisItem in the power app studio.
    The challenge is the code was written a couple of years ago by someone who is retired. 
     
    The reason I have to use First(colEmpInfo).employee_email is because I am comparing 
    award.Employee_Email with employee.employee_email from two different collections.
     
    In the below logic, Filter(Award, Lower(Status.Value)="pending" is to filter from the Award table
    which status = "pending" and the Employee_Email column needs to match the "employee_email"
    column from the employeeList table. Our data source is using the sharepoint. 
    I think Power Fx is case sensitive.
     
    CountRows(Filter(Award, Lower(Status.Value)="pending" && Employee_Email = First(colEmpInfo).employee_email ) ) < 1,
     
    I do see some strange behavior that the colEmpInfo is being cached 
    which means after I uncheck the previous employee I selected, the collection was cleared.
    So I added "Clear(colEmpInfo)" but it seems to mess up the logic and now all the employee seems to
    have pending request and gave me the warning message You have a pending award request for the selected employee
     
            UpdateContext({varPopupPendMsgFlag: true});
            UpdateContext({varPopupPendTitle: "Attention"});
            UpdateContext({varPopupPendMsg: "You have a pending award request for the selected employee : " & First(colEmpInfo).employee_email });
            UpdateContext({varPopupPendMsgColor: RGBA(255,0,0,1)});
            Clear(colEmpInfo)
  • Suggested answer
    FLMike Profile Picture
    FLMike 23,258 on at
    how to use ForAll function to check condition instead of patch
    Hi
     
    Change it to this
     
    If( CountRows(Filter(Award, Lower(Status.Value)="pending" && Employee_Email = First(colEmpInfo).employee_email ) ) < 1,
     
    If( CountRows(Filter(Award, Lower(Status.Value)="pending" && Employee_Email = ThisRecord.employee_email ) ) < 1,
     
    Since you want to access the current for all iteration use ThisRecord inplace of the Collection First etc

    Please mark as the answer

Helpful resources

Quick Links

Welcome to the Power Platform…

We are thrilled to unveil the newly-launched Power Platform Communities!…

Community Update Sept 16…

Power Platform Community Update…

Welcome to the new Power Platform Community!…

We are excited to announce our new Copilot Cookbook Gallery in the Community…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 140,719

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,355

Leaderboard