web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Cancel and exit event ...
Power Apps
Answered

Cancel and exit event in any code line

(0) ShareShare
ReportReport
Posted on by 158

Hello:

 

I have lots of lines of code on OnSelect button event. I would like to exit event, preventing executing the rest of event code if one condition is vàlid. Is it possible? How can implement that without having a big If condition that includes all? Does a specific instruction exist to accomplish it?

Thank you very much.


Example

line of code 1

line of code 2

line of code 3

If condition - If is valid exit event and not execute following lines of code

line of code 4

line of code 5

line of code 6

line of code 7

line of code 8

line of code 9

line of code 10

....

 

 

Categories:
I have the same question (0)
  • Daniel Bocklandt Profile Picture
    5,151 Super User 2026 Season 1 on at

    You could "Cheat" to get it done. 

    you would still need an If statement but instead of using the whole code, you could make it set a variable:

    If(valid.Event=true, Set(varContinue,false),Set(varContinue,true))

    this variable you could put as default in a toggle for example

    //Default Property of your toggle
    varContinue

     

    In your onCheck Property of the toggle you could put the rest of your Code:

    //OnCheck property
    line of code 4
    line of code 5
    line of code 6
    ...
    Set(varContinue,false)

     

    of course you wouldn't want to have an extra toggle somewhere where everybody can see it. you can either put the visible property to false or position it 'outside" of your screen so only you can see it when editing 

    //X proerty
    -200

     

     

     

  • Gochix Profile Picture
    1,937 Moderator on at

    @francescjp ,

     

    You can't exit the code directly in the code without If statement or some sort of a workaround.


    _____________________________________________________________________________________
    Please give a thumbs up if I resolved your issue! Please click Accept as Solution to close the topic!

  • francescjp Profile Picture
    158 on at

    Hello @DBO_DV :

     

    I have found a better solution.

    Thank you for your colaboration.

  • francescjp Profile Picture
    158 on at

    Hello @Gochix  :

     

    Yes, there is one way to do it.

    Thank you for your colaboration.

  • Verified answer
    francescjp Profile Picture
    158 on at

    Hello:

     

    I have found this solution that is near what @DBO_DV  explained. It would be included in OnSelect event:

     

    Set(varContinue, true);

     

    // first part of code;

     

    // Verify condition and update variable

    If(

    Condition1=false && varContinue, Set(varContinue, false) && Notify("Condition1 not passed", NotificationType.Error) );

     

    // Use variable to decide if continue executing

    If(

    varContinue,

    // second part of code );

     


    //REPEAT THE SAME PART WITH NEW PORTION OF CODE 
    // Verify condition and update variable

    If(

    Condition2=false && varContinue, Set(varContinue, false) && Notify("Condition2 not passed", NotificationType.Error) );

     

    // Use variable to decide if continue executing

    If( varContinue,

    // third part of code );

     

    //REPEAT THE SAME PART WITH NEW PORTION OF CODE 

    ....

     

    Thank you very much.

  • Daniel Bocklandt Profile Picture
    5,151 Super User 2026 Season 1 on at

    Hey @francescjp

     

    Isn’t that exactly what you didn’t want to do have everything in a big if condition? Now it will still run through everything. But if that solves your question I must have not understood you properly.

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 401 Most Valuable Professional

#2
11manish Profile Picture

11manish 201 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 128 Super User 2026 Season 2

Last 30 days Overall leaderboard