cancel
Showing results for 
Search instead for 
Did you mean: 

Exit formula on condition

Please add an escape expression for formula's like the exit sub command in VBA.

 

Example:

If(true
 ,do something (1)
,do something (2)
 ; exit/stop formula
)
;do something (3)
;do something (4)

do something (3)/(4) should not run when the if statement is true. If false the exit/stop should end formula.

 

This addition will ensure that multiple repetition of formulas are no longer needed , because you can exit the formula before code runs.

Status: New
Comments
Level 10

To answer a few questions in advance. There is a workaround to get this behaviour which I use:

 

UpdateContext({ValidateForm:true})
;If(condition1, do something1; UpdateContext({ValidateForm:false}); condition 2 , do something2;UpdateContext({ValidateForm:false}))
;Switch(ValidateForm, true
, Run Rest off Formula
)

 

With this code I am able to validate a form before running functions as Patch, SubmitForm, etc.

 

Stil would like to have the exit option, so the variable is no longer needed. And the Switch function can be deleted. 

Level 10

Also posted a other thread on the community board, see

https://powerusers.microsoft.com/t5/Expressions-and-Formulas/Exit-formula-on-condition/m-p/110375/hi...

 

Closed that thread and posted this Idea instead.

Level: Powered On

If they only need to be run when true, can't 3 and 4 just reside inside the True branch of the IF function?

Edit: apologies, re-read the condition, but still struggle to understand how "do something (3)/(4) should not run when the if statement is true." is avoided when true

So presuming there was an exit function, the behaviour of the code is, from my understanding:
True: do 1, do 3, do 4, end
False: do 2, exit

Am I missing something?

Level 10

 Hello adicristea,

 

I think what you're missing is the idea off a validation sequence on multiple (nested) conditions. So in the If statement there could be a number off conditions and outcomes, like if(condition1, outcome1, condition2, outcome2, condition3, outcome3,condition4, if(condition5,outcome5), outcome6)

 

Some conditions will lead to the extra script to run, in other conditions you just want to stop the formula with the if statement, but after some action like make font color red and display error textbox.

 

In the last example I do this with a variable ValidateForm, but this mean you first have to set the variable en updatecontext it on matching condition, then use is in a if statement to stop the script from running when false.

 

A simple argument like stop() in this formula, which only function is to stop the formula from running further wwould reduce the formula big time, cause the variable and the if statement on validateForm is not neaded.

 

What I say, this is exactly what's exit sub does in VBA.

 

Level: Powered On

Can you give me a specific example you'd benefit from Exit, please? I mean in English, no need to use PowerApps format, so it will be "If(variable1 is true, update context on variable2 with a value of 1, else exit the function)".

Level 10

I though I was writing my earlier comments in Englisch Smiley Very Happy

 

As I said before it will decrease the amount off code to write in a formula, because you won't need the validation variable and use this in an if statement. Don't know how to explain this better then less code makes the formula more readable and easier to understand when it's needs to be updated.

 

Oke, an example (but with powerapps formula):

Lets say I have a combobox with a list off options 1 and 2, a textbox (required with option 1) and an other combobox (requires a selection with option 2). The powerapps formula:

Current situation:

UpdateContext({ValidateForm:true})
;Switch(Combobox
,1, Switch(isblank(textbox), true, UpdateContext({ValidateForm:false})
,2, Switch(! Isnull(combobox), true, UpdateContext({ValidateForm:false})
) ;Switch(ValidateForm, true , Run Rest off Formula )

With escape option (I called it stopformula()):

Switch(Combobox
,1, Switch(isblank(textbox), true, stopformula()
,2, Switch(! Isnull(combobox), true, stopformula()
) , Run Rest off Formula

At stopformula() PowerApps should totally stop processing rest off the formula.

 

Hope you now see the difference in both formula's and the code needed to do the trick.

Level: Powered On

@KroonOfficeSol, I wasn't picking on your English, what I meant is that I wanted only the logic, not the actual formulas.

I see what you mean, but I would still do it differently, there is actually no need for a context variable to be used:

IF(
   (Combobox = 1 && isblank(textbox)) || (Combobox = 2 && !isnull(combobox2)),
   RunFormula
)

Level 10

He adicristea,

 

I just have the whish to have an option to exit the formula with one statement. I understand you're not with the PowerApps development team and therefore not able to grant my whish, I am correct aren't I.

 

I don't want to have a discussion on how to deal with this kind off logic, because clearly I understand how your example works. You would understand when you have read my earlier comments, I just want an option to stop the formula from running further if possible and would suggest the PowerApps team to add this to the roadmap.

 

On your logic statement. I try to keep my examples simple for everyone to understand what I mean. For the example your right that your way is fine, but it becomes different when the swith/if part for validation becomes more complex and the code to run if passed is say 6-7 formula's. Surround complex code in the if statement then is a problem to read, maintain and even to get the correct syntax. So I want to cut the formula in readable peaces, but be sure that code not runs if not wanted. Every programm languages has a exit statement to end a method, why should PowerApps shouldn't have a simular statement.

Level: Powered On

It is suprising that there is even a discussion about why it should be done.

Exiting a block of code can of course be acomplished with a bunch of logic. You can even go as far as adding a variable and check its status further down to avoid doing stuff. But why make things complicated? Visual Basic has "Exit" (that on PowerApps of course was used for something else), C# and tsql have "Return".

Level: Powered On

I'd love to see something like this. I think the added value is that it can help to decrease complexity / nesting blocks in larger formulas.