cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
lk777
Helper II
Helper II

Switch function using ";" or "," between options

For some reasons Powerapps refused to accept my code with Switch like this:

 

 

Switch(
varButtonPressed,
"Trash_FileSaved",
<do something>,
"Trash_FileUploaded",
<do something>,
"Trash_AllFilesUploaded",
<do something>
)

 

 

The only way to make it work was the following:

 

 

 

Switch(
varButtonPressed,
"Trash_FileSaved",
<do something>;
"Trash_FileUploaded",
<do something>;
"Trash_AllFilesUploaded",
<do something>
)

 

 

By using ";" between options, not ",". I have never experienced this before.

The code actually works, but is this a normal syntax for the switch function?

 

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User III
Super User III

@lk777 

Try refactoring your Formula to the following:

If(varPopResponse.type = "Yes" , 
    Switch( varButtonPressed, 
        "Trash_FileSaved", 
            UpdateContext({empty: RemoveIf(colDocs, FileNameDoc = galFilesSaved_1.Selected.FileNameDoc)}); 
            UpdateContext({varShowPopup: false}),
    
        "Trash_FileUploaded", 
            UpdateContext({varLoadingSpinnerVisibilty: true}); 
            If( !IsBlank(varFileSelectedDelete.Link), PowerAppsDeleteImageFileSP.Run(varFileSelectedDelete.DocFileID)); 
            UpdateContext({empty: RemoveIf(colMerged, FileNameDoc = galFilesUploaded_1.Selected.FileNameDoc)});
            UpdateContext({empty: RemoveIf(colDocs, FileNameDoc = galFilesSaved_1.Selected.FileNameDoc)}); 
            UpdateContext({varShowPopup: false, varLoadingSpinnerVisibilty: false}); 
            If(IsEmpty(colDocs), 
                Reset(dropPhysician_1); Reset(dateOfService_1); Clear(colDocs); Clear(colCombined); Clear(colMerged) 
            ), 
            
        "Trash_AllFilesUploaded", 
           If( !IsEmpty(colMerged), Set( gblAllImageFilesJson, JSON( colMerged, JSONFormat.IgnoreBinaryData ) ) ); 
           UpdateContext({varLoadingSpinnerVisibilty: true}); 
           If( !IsBlank(gblAllImageFilesJson), PowerAppsDeleteAllImageFilesInFolderSP.Run(gblAllImageFilesJson) ); 
           Clear(colDocs); Clear(colCombined); Clear(colMerged); 
           Set( gblAllImageFilesJson, Blank() ); 
           UpdateContext({varShowPopup: false, varLoadingSpinnerVisibilty: false, varDeleteAllFiles: true})          
    ),
    varPopResponse.type = "Cancel", 
        UpdateContext({varShowPopup: false})
)        

The thing about your Formula is that you have multiple return types in it.  Most return nothing, but Remove returns a table.  So, in the above (first I replaced Remove with RemoveIf, as this will be better performance) I am capturing the result of the table from RemoveIf into a variable.  Not for any other reason except that UpdateContext will return nothing...just list all the rest of your functions in the formula.

 

See if that works. 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!

View solution in original post

11 REPLIES 11
RandyHayes
Super User III
Super User III

@lk777 

It's not normal.  Each switch condition is separated by a comma (unless your language entry uses semicolons for separation).

In your situation, it is most likely the "Do something" that is the factor to consider.  

In general, formulas return a value.  When you have multiple actions in a formula, the return types either need to be the same or have no result.

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!

This is my code with the switch function which caused this behavior:

 

 

 

Switch(
    varButtonPressed,
    "Trash_FileSaved",
    If(
        varPopResponse.type = "Yes",
        Remove(
            colDocs,
            LookUp(
                colDocs,
                FileNameDoc = galFilesSaved_1.Selected.FileNameDoc
            )
        );
        UpdateContext({varShowPopup: false}),
        varPopResponse.type = "Cancel",
        UpdateContext({varShowPopup: false})
    );

    "Trash_FileUploaded",

  If(varPopResponse.type = "Yes",  
   UpdateContext({varLoadingSpinnerVisibilty: true});
        If( !IsBlank(varFileSelectedDelete.Link),
        PowerAppsDeleteImageFileSP.Run(varFileSelectedDelete.DocFileID)
    );
    Remove(
        colMerged,
        LookUp(
            colMerged,
            FileNameDoc = galFilesUploaded_1.Selected.FileNameDoc
        )
    );
    Remove(
        colDocs,
        LookUp(
            colDocs,
            FileNameDoc = galFilesSaved_1.Selected.FileNameDoc
        )
    );
    UpdateContext({varShowPopup: false});
    UpdateContext({varLoadingSpinnerVisibilty: false});
    If(
        IsEmpty(colDocs),
        Reset(dropPhysician_1);
        Reset(dateOfService_1);
        Clear(colDocs);
        Clear(colCombined);
        Clear(colMerged)
    ),
    varPopResponse.type="Cancel",
    UpdateContext({varShowPopup: false})
);
  
    "Trash_AllFilesUploaded",
    
   If(varPopResponse.type = "Yes" ,
    If(
        !IsEmpty(colMerged),
        Set(
            gblAllImageFilesJson,
            JSON(
                colMerged,
                JSONFormat.IgnoreBinaryData
            )
        )
    );
    UpdateContext({varLoadingSpinnerVisibilty: true});
    If(
        !IsBlank(gblAllImageFilesJson),
        PowerAppsDeleteAllImageFilesInFolderSP.Run(gblAllImageFilesJson)
    );
    Clear(colDocs);
    Clear(colCombined);
    Clear(colMerged);
    Set(
        gblAllImageFilesJson,
        Blank()
    );
    UpdateContext({varShowPopup: false});
    UpdateContext({varLoadingSpinnerVisibilty: false});
	UpdateContext({varDeleteAllFiles: true}),
    varPopResponse.type = "Cancel",
    UpdateContext({varShowPopup: false}))
    
)

 

 

This code works. Why?

RandyHayes
Super User III
Super User III

@lk777 

Try refactoring your Formula to the following:

If(varPopResponse.type = "Yes" , 
    Switch( varButtonPressed, 
        "Trash_FileSaved", 
            UpdateContext({empty: RemoveIf(colDocs, FileNameDoc = galFilesSaved_1.Selected.FileNameDoc)}); 
            UpdateContext({varShowPopup: false}),
    
        "Trash_FileUploaded", 
            UpdateContext({varLoadingSpinnerVisibilty: true}); 
            If( !IsBlank(varFileSelectedDelete.Link), PowerAppsDeleteImageFileSP.Run(varFileSelectedDelete.DocFileID)); 
            UpdateContext({empty: RemoveIf(colMerged, FileNameDoc = galFilesUploaded_1.Selected.FileNameDoc)});
            UpdateContext({empty: RemoveIf(colDocs, FileNameDoc = galFilesSaved_1.Selected.FileNameDoc)}); 
            UpdateContext({varShowPopup: false, varLoadingSpinnerVisibilty: false}); 
            If(IsEmpty(colDocs), 
                Reset(dropPhysician_1); Reset(dateOfService_1); Clear(colDocs); Clear(colCombined); Clear(colMerged) 
            ), 
            
        "Trash_AllFilesUploaded", 
           If( !IsEmpty(colMerged), Set( gblAllImageFilesJson, JSON( colMerged, JSONFormat.IgnoreBinaryData ) ) ); 
           UpdateContext({varLoadingSpinnerVisibilty: true}); 
           If( !IsBlank(gblAllImageFilesJson), PowerAppsDeleteAllImageFilesInFolderSP.Run(gblAllImageFilesJson) ); 
           Clear(colDocs); Clear(colCombined); Clear(colMerged); 
           Set( gblAllImageFilesJson, Blank() ); 
           UpdateContext({varShowPopup: false, varLoadingSpinnerVisibilty: false, varDeleteAllFiles: true})          
    ),
    varPopResponse.type = "Cancel", 
        UpdateContext({varShowPopup: false})
)        

The thing about your Formula is that you have multiple return types in it.  Most return nothing, but Remove returns a table.  So, in the above (first I replaced Remove with RemoveIf, as this will be better performance) I am capturing the result of the table from RemoveIf into a variable.  Not for any other reason except that UpdateContext will return nothing...just list all the rest of your functions in the formula.

 

See if that works. 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!

View solution in original post

If I remove "Trash_FileUploaded" part of the Switch function, PowerApps accepts "," between 2 remaining options. It seems that this removed part causes this issue.

RandyHayes
Super User III
Super User III

@lk777 

Ah yes...I missed that one.  Your PowerAutomate call will return a value as well...you should capture that into a variable as well.

 If( !IsBlank(varFileSelectedDelete.Link), UpdateContext({runResult: PowerAppsDeleteImageFileSP.Run(varFileSelectedDelete.DocFileID)}) ); 
_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!

When I mentioned that part "Trash_FileUploaded", I meant that the original code worked.

And I tried your code, it actually accepted a flow without capturing a result of a flow into a variable.

There is something mysterious about this Switch function, my code worked with ";", though it shouldn't ?
Can it be some Microsoft forgiveness approach to help code illiterate people to do their job 🙂 ?

 

RandyHayes
Super User III
Super User III

@lk777 

Not so sure on the last part of that statement.  I do know that in many situations (usually when using formula in a With block) the returns of the conditional (if or switch) formulas have to match signature...otherwise, things go a little wacky. 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!

@RandyHayes 

I have just checked your code and it works. Thank you. It is good to know what Switch expects as a return. But I am still wondering why my code worked as well.

RandyHayes
Super User III
Super User III

@lk777 

Yes...why yours worked is a bit of a mystery.  But, I would suspect that would be one of those things where, it works now, but somewhere down the road (on a new PowerApp release) it doesn't.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too!

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

Power Apps Community Call

Monthly Power Apps Community Call

Did you miss the call?? Check out the Power Apps Community Call here!

secondImage

Experience what’s next for Power Apps

See the latest Power Apps innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Power Platform ISV STudio

Power Platform ISV Studio

ISV Studio is the go-to Power Platform destination for ISV’s to monitor & manage applications post-AppSource publish.

Top Solution Authors
Top Kudoed Authors
Users online (54,841)