cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MVP

Beware of Patching Context Variables with CDS Option Sets Defined in other formulas

I ran into a major error today that I could not figure out for the most part of the day.

I have a canvas app with a connection to a CDS instance, where I'm trying to update an entity with multiple option set attributes on a single screen.  I've created a Gallery whose data source is a local collection with the name of the field, and the value of the option set from the entity in CDS.  The gallery contains a label for the field, and then 4 buttons, one for each of the possible option set values.  Each time you click a button, it updates the ThisItem's value to be the particular option set value defined by the button clicked.

The OnHidden formula is responsible for performing the update back to the DB.  In an attempt to be a good little developer and not update columns that I don't need to, I am checking to see if the value changed, and then patching an entity to save with all of the changed values.  For example, this formula works perfectly fine elsewhere in my app:


image.png
But when I tried to use it with the data collection from my gallery, it would error:image.png

The "changes" variable in the Patch gives the biggest error:

image.png


Funny thing though, opening the formula, and pushing the space bar results in the formula re-evaluating itself, and the errors goes away, and the patch to the CDS Datasource works correctly, so somehow in the on load for some reason, it gets an error for the _interestoverall column, even though it shouldn't have one.

If I replace the Patch calls in the update of the changes context variable to be just direct sets, no error occurs, but my logic would no longer work, only updating the last property that changed.

I've hit some edge case of some weird meeting place of OptionSets and Patches.  

 

I did create a work around be removing all of my patches and replacing them with DropColumns from a table:

 

image.png

 


So just a heads up for the community, the way patching works for canvas app has some core flaws with it that need to be resolved for the sanity of the community using them.  Just beware!

1 ACCEPTED SOLUTION

Accepted Solutions
MVP

Re: Beware of Patching Context Variables with CDS Option Sets Defined in other formulas

Did find a minimal reproduce-able version of this:

I have narrowed down the logic to recreate the bug to having a single screen, with a single button, with the OnSelect being this:

Set(Assessment, First('Risk Assessments'));
 
UpdateContext({
    changes: Patch({}, { new_interestoverall: Assessment.'Interest Overall' })
});
 
UpdateContext({
    changes: Patch(First(Table(changes)), { new_interestsysteminteraction: Assessment.'Interest System Interaction' })
});
 
Patch('Risk Assessments', Assessment, changes);
 
Where "Risk Assessemnt" is a CDS entity, and new_interestoverall and new_interestsysteminteraction are attributes using a Global OptionSet.

View solution in original post

3 REPLIES 3
Community Support
Community Support

Re: Beware of Patching Context Variables with CDS Option Sets Defined in other formulas

Hi @Dlabar ,

Do you mean that sometimes patch function works for CDS entity, but not works for collection?

The data type in CDS entity and collection is different.

If you save option set data in collection from CDS entity, it will not be option set in collection.

You need to use other formulas to update this field in collection.

Based on your description, do you mean that you've solved this problem and just want to post your idea?

If so, I suggest you post your idea here:

https://powerusers.microsoft.com/t5/PowerApps-Ideas/idb-p/PowerAppsIdeas

This place is used to post feedback.

 

 

 

Best regards,

 

Community Support Team _ Phoebe Liu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
MVP

Re: Beware of Patching Context Variables with CDS Option Sets Defined in other formulas

I'm describing a bug that would be extremely difficult to recreate, and as such, not one that I believe would benefit reporting.  It is a bug, not an "idea" so it doesn't belong on the idea site.  The actual bug has to do with the OptionSetValue not being typed correctly on load of the app with the patch function.  So this is me posting something, so if anyone else ever runs into the same issue I did, they would know that it's a bug, and how to perform the work around.  @v-yutliu-msft 

MVP

Re: Beware of Patching Context Variables with CDS Option Sets Defined in other formulas

Did find a minimal reproduce-able version of this:

I have narrowed down the logic to recreate the bug to having a single screen, with a single button, with the OnSelect being this:

Set(Assessment, First('Risk Assessments'));
 
UpdateContext({
    changes: Patch({}, { new_interestoverall: Assessment.'Interest Overall' })
});
 
UpdateContext({
    changes: Patch(First(Table(changes)), { new_interestsysteminteraction: Assessment.'Interest System Interaction' })
});
 
Patch('Risk Assessments', Assessment, changes);
 
Where "Risk Assessemnt" is a CDS entity, and new_interestoverall and new_interestsysteminteraction are attributes using a Global OptionSet.

View solution in original post

Helpful resources

Announcements
MBAS Gallery 2020

MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

‘Better Together’ T-Shirt Contest – Winner Announced!

‘Better Together’ T-Shirt Contest – Winner Announced!

And the winner is...

firstImage

New Ranks and Rank Icons released on April 21!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power Apps Community!

Power Platform 2020 release wave 1 plan

Power Platform 2020 release wave 1 plan

Features releasing from April 2020 through September 2020

Top Solution Authors
Top Kudoed Authors
Users online (6,091)