cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
NichlasKoeppen
Frequent Visitor

Patch datasource#1 then ForAll Patch datasource#2 - how to error handle

Hello power users, 

I want to know the best way to do the following: 

Case: 

I need to patch #1 SP list (one record). Before patching #2 SP list, I need to make sure that the first patch went through. 

If succesfull, then I need to patch #2 SP list (multiple records from a gallery), and verify that as well. 

 

The code right now (simplified): 

 

 

Patch(Datasource1;Defaults(Datasource1);
{
    Field1:"aaaaa";
    Field2:"bbbbb"
});;

ForAll(Gallery.AllItems;
Patch(Datasource2;Defaults(Datasource2);{
    field1:item1;
    field2:item2
}))

 

 

 

So question is: How do I confirm that first patch was succesfull then move to next ForAll patch, and also verify the result of that - before changing screen? 

 

Thanks ! 🙂 

Br. Nichlas

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User III
Super User III

@NichlasKoeppen 

Please consider changing your Formula to the following:

With({_record:
    Patch(Datasource1;Defaults(Datasource1);
        {
         Field1:"aaaaa";
         Field2:"bbbbb"
        }
    )
    };

    If(IsEmpty(Errors(Datasource1; _record));
        Collect(Datasource2;
            ForAll(Gallery.AllItems;
                Patch(Defaults(Datasource2);
                   {
                     field1:item1;
                     field2:item2
                   }
                )
            )
        )
    )
)

In this formula, we first do the patch and assign the record result to a scoped variable - _record

Then we use the Errors function to check for any errors on the datasource1 record.  

If not, then we do the additions to datasource2.

Also, note: I am using the collect function for this rather than ForAll/patch.  ForAll returns a table, collect returns nothing.  So, in this case we ForAll the items in the Gallery to get a table of records, then we collect them back to the datasource.   Why do this?  First, it performs better than ForAll/Patch and second it does not create a mismatch in the return from within the If statement.

 

I hope this is helpful for you.

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

View solution in original post

4 REPLIES 4
RandyHayes
Super User III
Super User III

@NichlasKoeppen 

Please consider changing your Formula to the following:

With({_record:
    Patch(Datasource1;Defaults(Datasource1);
        {
         Field1:"aaaaa";
         Field2:"bbbbb"
        }
    )
    };

    If(IsEmpty(Errors(Datasource1; _record));
        Collect(Datasource2;
            ForAll(Gallery.AllItems;
                Patch(Defaults(Datasource2);
                   {
                     field1:item1;
                     field2:item2
                   }
                )
            )
        )
    )
)

In this formula, we first do the patch and assign the record result to a scoped variable - _record

Then we use the Errors function to check for any errors on the datasource1 record.  

If not, then we do the additions to datasource2.

Also, note: I am using the collect function for this rather than ForAll/patch.  ForAll returns a table, collect returns nothing.  So, in this case we ForAll the items in the Gallery to get a table of records, then we collect them back to the datasource.   Why do this?  First, it performs better than ForAll/Patch and second it does not create a mismatch in the return from within the If statement.

 

I hope this is helpful for you.

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

View solution in original post

That is really awesome! Thanks! 
What about checking if the patch for Datasource2 also was successful?

How would you check that each item from the gallery successfylly was patched? 

 

Or is this not needed when you are using the collect function? 

RandyHayes
Super User III
Super User III

@NichlasKoeppen 

You could use the same concept with the errors.  The only real difference is you could just check for any errors on the entire datasource.  In the formula I gave, we specifically looked at _record - which was the record we just patched.

However, the record is optional for the Errors function.  You can just specify the entire datasource - Errors(datasource2) to see if there are any errors.

 

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

Thank you a lot @RandyHayes . I'll mark you answer as the solution. 

Have a nice day!

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

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Don't miss the call this month on June 16th - 8a PDT

Top Solution Authors
Top Kudoed Authors
Users online (64,319)