cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
asirbu91
Regular Visitor

Change Context Variable when the selection in a Gallery changes

Hi everyone, 

This should be straightforward but I'm completely stumped. 

I have a toggle control, that needs to change based on some values in a SharePoint list. When the user selects an item in a gallery, I want the app to check the SP list and to set the value of the toggle control accordingly. I've created a contextual variable to set the state of the toggle control with. I'm using an If function to set the values of the contextual variables on the OnSelect property of the Gallery item. The logic works - when I'm using a button to trigger the action, the variables, and the toggle control changes correctly. The problem that I seem to have is that the OnSelect property of the gallery item doesn't trigger the actions 😞 Any help would be much appreciated! 

 

asirbu91_0-1618537331384.png

 

asirbu91_1-1618537527041.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

@Eelman - can't thank you enough for your help! I took another stab at it right now. And... I'm sort of embarrassed to share what the issue was because it is stupidly obvious and simple, and I feel I might have sent you on a wild goose chase. I was so focused on finding mistakes in the code, that I totally forgot and omitted that I put an invisible icon object on top of each gallery item, to simulate a hover effect (have the cursor change to a hand cursor on hover). I was using the OnSelect code on the label object which is basically behind this transparent icon object. That's why it wasn't getting fired. I moved the code to the OnSelect property of the invisible icon above and works now. I will be sure to copy and paste the full code in the future. Many thanks for all the guidance and advice!

 

asirbu91_0-1618620743335.png

 

View solution in original post

6 REPLIES 6
Eelman
Super User
Super User

@asirbu91 

Two quick things you could try:

 

1. Remove the Select(Parent); code. Unless there's a need for it, I always remove it. Also, if the Parent has code there this will run first / concurrently and maybe affecting your flow of control

 

2. You say you created a 'contextual variable', did you do this in the first instance using UpdateContext() and you are now using Set()? If so, this confuses PowerApps. The fix here is to rename the second variable something slightly different eg setToggle_1.

 

Also, where was you button located when this worked? Inside the gallery or outside it?

Hi Eelman,

 

Thank you so much for the reply. I've removed the Select(Parent) code for the OnSelect, and renamed the contextual variable. I also fixed a bit the logic in the IF. This function is the updated OnSelect function for the gallery items:

 

asirbu91_0-1618554954587.png

When I use the same code on a button outside the Gallery, it correctly resets the control based on the selected course (so logic works). In the gallery, however, it doesn't get fired as I switch between gallery items - the value of the toggle control does not change...

 

 

@asirbu91 

What are the Toggle settings? Especially the Default property but any other properties that you have set. Don't you have code in its OnChange property?

@asirbu91 

Try this 

If(
   // Condition 1
   IsBlank(
      Lookup(TM_Onboarding_ActivityStatus, 
      Email = User().Email && CourseName = ThisItem.Title
      )
    ),
    UpdateContext({setToggle1: false}),

   // Condition 2, no need for a second If()
   Lookup(TM_Onboarding_ActivityStatus, 
      Email = User().Email && CourseName = ThisItem.Title, Status
   )="Pending",
   UpdateContext({setToggle1: false}),

   // Else
   UpdateContext({setToggle1: true})
)
 

 

The Default property of your Toggle must be set to setToggle1 and also ensure your toggle's state is getting reset as required. I can't comment on what that is because I don't know your full setup but you can check this.

 

I was able to conditionally set a Toggle outside a gallery using a similar setup to yours so there's no issue with doing this, there just seems to be something else going on eg could be a Data Type mismatch somewhere?

 

PS: Try using the code window when posting future questions as this makes answering posts a lot easier ie we can simply copy and paste your code. Some helpers on the forums won't help if they see an image with a large amount of code they need to re-type. Just a heads-up 🙂 

@Eelman - can't thank you enough for your help! I took another stab at it right now. And... I'm sort of embarrassed to share what the issue was because it is stupidly obvious and simple, and I feel I might have sent you on a wild goose chase. I was so focused on finding mistakes in the code, that I totally forgot and omitted that I put an invisible icon object on top of each gallery item, to simulate a hover effect (have the cursor change to a hand cursor on hover). I was using the OnSelect code on the label object which is basically behind this transparent icon object. That's why it wasn't getting fired. I moved the code to the OnSelect property of the invisible icon above and works now. I will be sure to copy and paste the full code in the future. Many thanks for all the guidance and advice!

 

asirbu91_0-1618620743335.png

 

View solution in original post

Haha, ah ok, that explains it! Still, mark your work as the solution in case anyone comes looking, it may just help someone else 🙂 

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Did you miss the call? Check out the recording here!

Top Solution Authors
Top Kudoed Authors
Users online (10,693)