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

Conditional OnSuccess after SubmitForm() based on 'Form'.Mode

Hi,

 

I am trying to write an IF() statement in my OnSuccess of a form based on it's form mode prior to submitting the record.

 

I have a form which dynamically changes the mode to New if I've clicked a button to create a new record or Edit if I have selected a record in my gallery. The gallery sets a global variable on selection with the value being the selected item (ThisItem) and the item property of the form is that global variable.

 

If the form is a new form i.e. I am creating a new record, I want to be able to navigate back to the home screen if the form is successfully submitted, and notify the user with a success notification.

If the form is an edit form, I want to be able to navigate back to the record screen (screen with my gallery of records) and notify the user with a different success notification.

 

I am using this syntax in my form OnSuccess:

 

If(Form1.Mode = FormMode.New, Navigate('Home Screen', Fade) && Notify("The record was successfully created", Success), Navigate('Record Screen',Fade) && Notify("Your changes were saved", Success))

 

This formula does not work if I am submitting a new record for example. It always uses the else value/steps of the statement. I think this could be because the form is auto setting to 'Edit' mode after a submission. This in MS Docs seemed interesting:

 

The form switches from New mode to Edit mode when any of these changes occurs:

  • The form is successfully submitted, and a record is created. If the gallery is set to automatically move selection to this new record, the form will be in Edit mode for the created record so that the user can make additional changes.
  • The EditForm function runs.
  • The ResetForm function runs. For example, the user might select a Cancel button that's been configured with this function.

 

So I suppose the question is, how do I 'unset' the gallery from automatically selecting the new record?

1 ACCEPTED SOLUTION

Accepted Solutions
iAm_ManCat
Super User
Super User

Heya,

 

You can't make the gallery not update, but since what you're trying to achieve is a conditional navigate then I think a better way to achieve this would be to set a Global variable when you implement the EditForm/NewForm functions.

 

Something like this for your call to the New function:

 

 

 

Set(gblPreviousFormMode, 
    If(IsBlank(gblCurrentFormMode), 
       FormMode.New,
       gblCurrentFormMode
    )
);
NewForm(FormName);
Set(gblCurrentFormMode, FormMode.New);

 

 

 

And your call to the edit function:

 

 

Set(gblPreviousFormMode, 
    If(IsBlank(gblCurrentFormMode), 
       FormMode.Edit,
       gblCurrentFormMode
    )
);
EditForm(FormName);
Set(gblCurrentFormMode, FormMode.Edit);

 

 

 

 

Now you can reference gblPreviousFormMode in your OnSuccess call and it won't be looking at the current state of the form, but what it was prior to you changing it

 

 

 

 

If(gblPreviousFormMode = FormMode.New, 
   Navigate('Home Screen', Fade); Notify("The record was successfully created", Success), 
   Navigate('Record Screen',Fade); Notify("Your changes were saved", Success)
)

 

 

 

 

@iAm_ManCat


Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


View solution in original post

3 REPLIES 3
iAm_ManCat
Super User
Super User

Heya,

 

You can't make the gallery not update, but since what you're trying to achieve is a conditional navigate then I think a better way to achieve this would be to set a Global variable when you implement the EditForm/NewForm functions.

 

Something like this for your call to the New function:

 

 

 

Set(gblPreviousFormMode, 
    If(IsBlank(gblCurrentFormMode), 
       FormMode.New,
       gblCurrentFormMode
    )
);
NewForm(FormName);
Set(gblCurrentFormMode, FormMode.New);

 

 

 

And your call to the edit function:

 

 

Set(gblPreviousFormMode, 
    If(IsBlank(gblCurrentFormMode), 
       FormMode.Edit,
       gblCurrentFormMode
    )
);
EditForm(FormName);
Set(gblCurrentFormMode, FormMode.Edit);

 

 

 

 

Now you can reference gblPreviousFormMode in your OnSuccess call and it won't be looking at the current state of the form, but what it was prior to you changing it

 

 

 

 

If(gblPreviousFormMode = FormMode.New, 
   Navigate('Home Screen', Fade); Notify("The record was successfully created", Success), 
   Navigate('Record Screen',Fade); Notify("Your changes were saved", Success)
)

 

 

 

 

@iAm_ManCat


Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


lowcodelewis
Frequent Visitor

Thank you so much for this Sancho!

 

I literally just thought of a global variable aha!... I haven't quite done it as nicely as you have though so I might make some changes! All I've done so far is set an additional global variable to true or false based on whether the appropriate button is clicked or whether a record is selected from the gallery. Over the hurdle! 🙂

 

Thank you 🙂

shoog
Dual Super User
Dual Super User

The form mode will get reset after the submit, so you can't access the previous value in OnSuccess.

You can store the form mode just before the submit function

Set(varMode, Form1.Mode)

and reference that variable in your OnSuccess formula instead of Form1.Mode.

Helpful resources

Announcements
Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Users online (1,509)