cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
paulinuk
Post Patron
Post Patron

Code that patches a variable breaks a screen

Hello

 

I have an edit form that I have a text box for the weather on

 

The idea is that the user presses the weather icon and the app will get the current weather and update the box

 

I am doing this using


Set(varLocation, Text(Location.Latitude)&","&Text(Location.Longitude));
UpdateContext({Weather: MSNWeather.CurrentWeather(varLocation, "Metric")});
Set(varCurrentDiaryEntry, Patch(varCurrentDiaryEntry, {Field_2: Weather.responses.weather.current.cap}));

This code is only invoked when the icon is clicked, but as soon as this exists, my screen no longer works

 

There are no errors

 

What is the problem?

 

I have also tried this on a button outside of the edit form

 

Paul

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenBelz
Super User
Super User

Hi @paulinuk ,

Firstly, if you are trying to set a Variable, you should be able to do this

With(
   {
      wWeather: 
      MSNWeather.CurrentWeather(
         Location.Latitude & "," & Location.Longitude,
         "Metric"
      )
   },
   Set(
      varCurrentDiaryEntry,
      {Field_2: wWeather.responses.weather.current.cap}
   )
);

Your output would then be

varCurrentDiaryEntry.Field_2

and simply sets a Variable. What happens to your screen?

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

Visit my blog Practical Power Apps

 

View solution in original post

10 REPLIES 10
WarrenBelz
Super User
Super User

Hi @paulinuk ,

Firstly, if you are trying to set a Variable, you should be able to do this

With(
   {
      wWeather: 
      MSNWeather.CurrentWeather(
         Location.Latitude & "," & Location.Longitude,
         "Metric"
      )
   },
   Set(
      varCurrentDiaryEntry,
      {Field_2: wWeather.responses.weather.current.cap}
   )
);

Your output would then be

varCurrentDiaryEntry.Field_2

and simply sets a Variable. What happens to your screen?

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

Visit my blog Practical Power Apps

 

Thanks 

 

Yes this is what I am trying to do but as soon as I put this code into an icon inside or a button outside the form the screen doesnt work at all

 

I have also seen the message below which could be related but I have no idea how to resolve

paulinuk_0-1642301511569.png

 

Hi @paulinuk ,

What has the Item of the Form got to do with this? What is the code on there?

This error happens when I put the code you showed above in a button outside of the form

 

Im wondering if the designer is trying to evaluate this code at design time when the variable is nul?

@paulinuk ,

The error you have posted refers to the Item of the Form - what is the code there?

I have also tested the code I posted (I also use this function) and it is valid whether inside or outside a form as it does not refer to anything in the form.

paulinuk
Post Patron
Post Patron

I have changed this to be the best practice and the error has gone again, it seems to come up every now and then

 

My current code for the Item of form is 

 

LookUp('Site Diary Entries',ID=varCurrentDiaryEntryId)

 

The problem now is that I cant use your logic as that updates a variable, which is now not being used as the Item for this form due to this error?

Hi @paulinuk ,

But the Variable you are setting on the weather is varCurrentDiaryEntry which is actually a Table with Field2 containing the weather. The Variable in your Form Item is CurrentDiaryEntryId - how is that stopping you using the weather code?

 

paulinuk
Post Patron
Post Patron

Because I thought I had to update the exact variable that the form is bound to.

 

The weather code is working fine now

 

When the user clicks on a weather button on the edit form I want to change the value of the Weather text box to be the weather returned from MSN

 

paulinuk_0-1642336912392.png

 

I’ve tried loads of different ways but nothing updates the text box on my form do I need to do something to force the box to refresh?

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

Top Solution Authors
Top Kudoed Authors
Users online (2,529)