cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
cidreno
Helper I
Helper I

Don't understand If() statement within With() statement

I have an If() statement within a With() statement. It works if I only have a true value, and does not work if I have a true and an else value.

What's the story with that?

And how can I make it work?

 

Here's the code that works

    With(
        Patch(
            Worker,
            Defaults(Worker),
            {PrimerNombre: dcvPrimerNombre.Text,
            ApellidoPaterno: dcvApellidoPaterno.Text,
            ApellidoMaterno: dcvApellidoMaterno.Text,
            Telefono: dcvTelefono.Text
            }
        ),
        If(
            IsEmpty(Errors(Worker)),
            ForAll(
                combTradesWorker.SelectedItems,
                Patch(
                    WorkerTrade,
                    Defaults(WorkerTrade),
                    {
                        WorkerID: WorkerID,
                        TradeID: TradeID
                    }
                )
            )
        )
    )

 

And here's the code that doesn't

    With(
        Patch(
            Worker,
            Defaults(Worker),
            {PrimerNombre: dcvPrimerNombre.Text,
            ApellidoPaterno: dcvApellidoPaterno.Text,
            ApellidoMaterno: dcvApellidoMaterno.Text,
            Telefono: dcvTelefono.Text
            }
        ),
        If(
            !IsEmpty(Errors(Worker)),
            Notify("Error"),
            ForAll(
                combTradesWorker.SelectedItems,
                Patch(
                    WorkerTrade,
                    Defaults(WorkerTrade),
                    {
                        WorkerID: WorkerID,
                        TradeID: TradeID
                    }
                )
            )
        )
    )

 

Difference is that the If statement has a logical statement, a true value and an else value in the code that doesn't work and just a logical statement and a true value in the code that works 

 

Back story: Worker and WorkerTrade are tables in my SQL database. combTradesWorker is a comboBox in my app

2 ACCEPTED SOLUTIONS

Accepted Solutions
Sunil-Narnaware
Super User
Super User

@cidreno instead of If, can you try IfError function and see if it works?

View solution in original post

👍that worked!!!

still don't understand why the If statement wouldn't take a true value and an else value

View solution in original post

5 REPLIES 5
Sunil-Narnaware
Super User
Super User

@cidreno instead of If, can you try IfError function and see if it works?

👍that worked!!!

still don't understand why the If statement wouldn't take a true value and an else value

@cidreno even I was surprised, might be some limitation with 'WITH' function.. (not sure).. if the above answer helped, can you 'Accept it as Solution' so that if people are in similar situation they can also easily see it..

@cidreno 

I'd say that @Sunil-Narnaware's suggestion is the best approach.

In answer to your specific question above, this will be because Power Apps cannot parse If statements with inconsistant return values. My post here may help explain this.

https://powerusers.microsoft.com/t5/Building-Power-Apps/Strange-behaviour-of-Concurrent-function-and...

 

Ahhhh... that makes the sense. Good to know. Thanks! 

Helpful resources

Announcements
October Events

Mark Your Calendars

So many events that are happening this month - don't miss out!

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 (4,732)