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

Setting the selected date as a global variable for further use.

I have a form with two date pickers. I'd like to save it as a global variable after selecting a date in date picker 2. Then, after saving the form, I would like the saved date in the new form to be set as the default value for date picker 1. I tried the following codes in on change for data picker 2:

 

"Set(ChangedDose; DataCardValue50.SelectedDate)"

 

and also the following code for the default on date picker 1:

 

"ChangedDose"

 

I assigned the save record button to the following code on select:

 

"SubmitForm(Form1);;Refresh('Data1');;UpdateContext({resetdate: true});;UpdateContext({resetdate: false})"

 

Reset for both date pickers is set to local variable "resetdate".

 

Unfortunately, when selecting a date, the global variable is not saved correctly and the blank value is returned with the date type.

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@Aravial 

This is all best done in your OnSuccess action of your form.

 

Your Formula for submitting the for should ONLY ever be: SubmitForm(Form1)

A form submits asynchronously, so it immediate performs the functions after your submitform, even before the form has submitted. So changing the form or deriving anything from it during that phase is not guaranteed to accurate.

 

Your OnSelect of your save record button should be:  SubmitForm(Form1)

 

Your OnSuccess action Formula on the form should be:

    Set(ChangedDose; Self.LastSubmit.fieldNameForDatepicker);;

    UpdateContext({resetdate: true});;UpdateContext({resetdate: false})

Note: refreshing the datasource is not needed and will only slow your app.  The data you submitted will all be in the datasource already.

 

In the above, change the field name to the field that contains the date from DatePicker 2.

Get rid of the OnChange formula for the datapicker.

 

You can keep your DefaultDate on the other date picker as ChangeDose

 

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.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
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

1 REPLY 1
RandyHayes
Super User
Super User

@Aravial 

This is all best done in your OnSuccess action of your form.

 

Your Formula for submitting the for should ONLY ever be: SubmitForm(Form1)

A form submits asynchronously, so it immediate performs the functions after your submitform, even before the form has submitted. So changing the form or deriving anything from it during that phase is not guaranteed to accurate.

 

Your OnSelect of your save record button should be:  SubmitForm(Form1)

 

Your OnSuccess action Formula on the form should be:

    Set(ChangedDose; Self.LastSubmit.fieldNameForDatepicker);;

    UpdateContext({resetdate: true});;UpdateContext({resetdate: false})

Note: refreshing the datasource is not needed and will only slow your app.  The data you submitted will all be in the datasource already.

 

In the above, change the field name to the field that contains the date from DatePicker 2.

Get rid of the OnChange formula for the datapicker.

 

You can keep your DefaultDate on the other date picker as ChangeDose

 

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.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
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
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.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

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