cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
RussellGove
Level 10

Conditionally execute a block of code

Hi,

I have this script in the onSelect of my save Button:

If (IsBlank(tbCommentOnSafeBehavior),UpdateContext({'CommentOnSafeBehiviorMissing':true}),UpdateContext({'CommentOnSafeBehiviorMissing':false}));


ClearCollect(MyPatchedRecord,Patch('Visible Felt Leadership',Defaults('Visible Felt Leadership'),{Title:""},
EditForm1.Updates,EditForm2.Updates,EditFormSafe.Updates));
ResetForm(EditForm1);
ResetForm(EditForm2);
ResetForm(EditFormSafe);
ResetForm(EditFormUnsafe1);
ResetForm(EditFormUnsafe2);
ForAll(PhotoGallery,PhotoHandler.UploadPhoto(Title,First(MyPatchedRecord).ID,Photo));
Clear(PhotoGallery);
Navigate(BrowseScreen1,Fade)

 

I only want to execute  the code from ClearCollect to navigate if CommentOnSafeBehiviorMissing is false.

 

I was hoping i could wrap all those statements in curly braces:

if (!CommentOnSafeBehiviorMissing,{ClearCollect(MyPatchedRecord,Patch('Visible Felt Leadership',Defaults('Visible Felt Leadership'),{Title:""},
EditForm1.Updates,EditForm2.Updates,EditFormSafe.Updates));
ResetForm(EditForm1);
ResetForm(EditForm2);
ResetForm(EditFormSafe);
ResetForm(EditFormUnsafe1);
ResetForm(EditFormUnsafe2);
ForAll(PhotoGallery,PhotoHandler.UploadPhoto(Title,First(MyPatchedRecord).ID,Photo));
Clear(PhotoGallery);
Navigate(BrowseScreen1,Fade)})

 

But that syntax id not work. 

 

How can I accomplish the above? Does the scripting engine support script blocks with curly braces? Is there a 'Return' statement so I can just exit the running script?

1 REPLY 1
Super User
Super User

Re: Conditionally execute a block of code

Hi @RussellGove

 

To execute multiple commands within an If clause, you can specify semi-colon delimited commands without any curly braces.

 

Therefore, your code would look something like this:

 

If (!CommentOnSafeBehiviorMissing,
    ClearCollect(MyPatchedRecord,
                         Patch('Visible Felt Leadership',
                                   Defaults('Visible Felt Leadership'),
                                   {Title:""},
                                   EditForm1.Updates,
                                   EditForm2.Updates,
                                   EditFormSafe.Updates
                            )
     );
     ResetForm(EditForm1);
     ResetForm(EditForm2);
     ResetForm(EditFormSafe);
     ResetForm(EditFormUnsafe1);
     ResetForm(EditFormUnsafe2);
     ForAll(PhotoGallery,
                PhotoHandler.UploadPhoto(
                Title,First(MyPatchedRecord).ID,Photo)
      );
     Clear(PhotoGallery);
     Navigate(BrowseScreen1,Fade)
)

Helpful resources

Announcements
thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Kudoed Authors (Last 30 Days)
Users online (4,100)