web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Weird "Patch" Behavior...
Power Apps
Answered

Weird "Patch" Behavior with CDS Data Objects - the use of AddColumns seems to screw up the Patch operations

(0) ShareShare
ReportReport
Posted on by 142

I am doing my first Patch command in a while (since CDS v1 and now I am working with CDS v2) and experiencing weird behavior.

When I use the following command on an unmodified CDS entity ('Plant Inventories") everything is good.

image.png

 

Patch('Plant Inventories'),
 plant,
 {
 'Plant Description': txtPlantDescription.Text,
 'Location Description': txtLocationDescription.Text
 }
)

 

image.png

I load the record"plant" up when I navigate to a new screen with the Save button using this code and use it in the Patch function to identify the record to be updated.  Before I do the AddColumns command below, this record does not have the two columns from the AddColumns command as you would expect.

 

Navigate(AddEditPlants,ScreenTransition.Fade, {editMode: true, plant: galSearchPlants.Selected})

 

 

 When I add the more complex AddColumns statement below to the Gallery to provide multi-field Search across both standard Text and Lookup (to another Entity) columns, then my Patch statement fails with a strange result "the specified column "PlantFullCategoryName" does not exist." (the message is just hiding the Save button) that I have yet to decipher. When the 'plant' record is instantiated after I execute the AddColumns, these two columns are dutifully added to 'plant' and I am suspecting that is the root of the problem somehow.

 

SortByColumns(
 Search(
 AddColumns(
 'Plant Inventories',
 "PlantFullName",
 'Plant Name'.cr571_plantname,
 "PlantFullCategoryName",
 'Plant Category '.cr571_plantcategoryname
 ),
 txtSearchString.Text,
 "PlantFullName",
 "PlantFullCategoryName",
 "cr571_description",
 "cr571_locationdescription"
 ),
 "PlantFullName",
 If(
 SortDescending1,
 Ascending,
 Descending
 )
)

 

 image.pngimage.pngNow we know it exists in the "plant" object because it is being shown in the blue highlighted field (as is the other one) but I suspect that it is warning me that this doesn't exist in the 'Plant Inventories' CDS object (since I AddColumns on it).  My first instinct was to DropColumns on 'plant' but that didn't work (or I didn't do it right).

 

It was good just to write this out to clearly state what is happening and I would appreciate anyone with experience Patching to a CDS object that as had an AddColumns operation done to it chiming in with some assistance.  Should I retry DropColumns on both the 'Plant Inventories" CDS object and the temporary record 'plant'? just one or the other? Am I doing something else wrong?

 

Also, when I solve this little problem, I will be looking for the correct syntax to update a LookUp field to another CDS object.  I have an old example from 3 years ago on CDS V1 that I suspect is woefully out of date and haven't found another one for CDS v2.

 

Thanks,

Mark

206-679-3277

I have the same question (0)
  • Verified answer
    v-bofeng-msft Profile Picture
    Microsoft Employee on at

    HI @markslosberg:

    Firstly, I would like to confirm with you whether my following understanding is correct:

    • There is a gallery control(galSearchPlants) whose items property is SortByColumns(Search(AddColumns(……”.
    • You want to modify the record which have been selected in the gallery(galSearchPlants) with patch function.
    • You hope the patch function can update the value in the 'Plant Inventories' entity.

    Secondly, let me explain why the patch function dont work.

    The point is Plant’s data source is not 'Plant Inventories.

    By plant”’s definitionplant: galSearchPlants.Selected, We learned that the dynamic table tableA(SortByColumns(Search(AddColumns(……”(Allow me to call it dynamic tableA)is plant”’s data source. Its a new dynamic temporary table that contain all fields in Plant Inventories and two new fields("PlantFullName","PlantFullCategoryName"),So tableA and Plant Inventories are two different data sources

    Finally, here is my solution to optimize the code for your reference:

    Patch('Plant Inventories', /* “)” should be removed*/
     Lookup('Plant Inventories',’Plant ID’=plant.’Plant ID’),
     {
     'Plant Description': txtPlantDescription.Text,
     'Location Description': txtLocationDescription.Text
     } 
    )

    Best Regards,

    Bof

  • MS-22072102-0 Profile Picture
    142 on at

    Perfect - I was stumbling to a solution like this but you zeroed in on it.

     

    I may have one more question regarding the right way to Patch "LookUp" fields in the CDS entity.  I will work on it this morning and see if I can solve it myself first.

     

    Thank you.

  • MS-22072102-0 Profile Picture
    142 on at

    @v-bofeng-msft 

    Thank you for the help so far.

    So now I am struggling with my second question about the right way/syntax to Patch the three Lookup fields (Plant Name, Plant Category and Plant Supplier) both for existing records (like we just did) and for new records.

     

    I have DropDown Boxes containing the proper selections for Plant Category and Plant Name (and Supplier but I haven't gotten there just yet) and I want to put those selections into the appropriate Plant Category and Plant Name Lookup fields in the same Plant Inventories entity. 

    image.pngimage.png

    How would I alter this code, which works beautifully BTW on updating the Text fields, to include the updated values for the three Lookups. 

    Patch(
     'Plant Inventories',
     LookUp('Plant Inventories','Plant ID'=plant.'Plant ID'),
     {
     'Plant Description': txtPlantDescription.Text,
     'Location Description': txtLocationDescription.Text,
     }
    )

    I would think it would look something like 

    Patch(
     'Plant Inventories',
     LookUp('Plant Inventories','Plant ID'=plant.'Plant ID'),
     {
     'Plant Description': txtPlantDescription.Text,
     'Location Description': txtLocationDescription.Text,
     'Plant Category': drpPlantCategory.Selected.'Plant Category',
     'Plant Name': drpPlantNames.Selected.'Plant Name'
     }
    )

    but when I do this I get:

    image.png

    I also have another example from three years ago that worked in CDS v1 that looks like this that I am wondering if it is still valid in some way.  I have seen one reference like this related to SharePoint List Lookup functions but not for CDS yet  (note example is for a different data schema but the principle is the same).

    UpdateContext(
     {
     varietyData: {
     '@odata.type': "#Microsoft.PowerApps.Runtime.Common.ConnectorCommon.Models.Lookup",
     CropDescription: crop.CropDescription,
     PrimaryId: crop.PrimaryId
     },
     plantData: {
     '@odata.type': "#Microsoft.PowerApps.Runtime.Common.ConnectorCommon.Models.Lookup",
     VendorId: organization.VendorId,
     FullName: organization.FullName
     },
     supplierData: {
     '@odata.type': "#Microsoft.PowerApps.Runtime.Common.ConnectorCommon.Models.Lookup",
     PrimaryId: CropSubTypeDropdown.SelectedText.PrimaryId,
     CropSubTypeDescription: CropSubTypeDropdown.SelectedText.CropSubTypeDescription
     }
     }
    );
    If(
     Not(editMode),
     UpdateContext(
     {
     NewVarietyObject: Patch(
     'Crop Variety',
     Defaults('Crop Variety'),
     {
     Crop_: cropData,
     Vendor: venderData,
     CropSubType_: cropSubType,
     VarietyComment: VarietyCommentTextInput.Text,
     VarietyDescription: VarietyDescriptionTextInput.Text,
     PDFURL: "https://croptrial.blob.core.windows.net/pdfs/Plant-Descriptions.pdf"
     }
     )
     }
     )
    );

     Once I get the "editMode" example Patches working I will be wanting to add new records too but I am assuming (hoping) that the syntax will be essentially the same but with the  Default('Plant Inventories') phrase replacing the Lookup function.

     

    Thanks in advance.

    Mark

  • Verified answer
    MS-22072102-0 Profile Picture
    142 on at

    I think I solved it like this:

    Patch(
     'Plant Inventories',
     LookUp(
     'Plant Inventories',
     'Plant ID' = plant.'Plant ID'
     ),
     {
     'Plant Description': txtPlantDescription.Text,
     'Location Description': txtLocationDescription.Text,
     'Plant Name': LookUp(
     'Plant Names',
     'Plant Name' = drpPlantNames.Selected.'Plant Name'
     ),
     'Plant Category': LookUp(
     'Plant Categories',
     'Plant Category Name' = drpPlantCategory.Selected.Result
     )
     }
    )

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 335 Most Valuable Professional

#2
11manish Profile Picture

11manish 165

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 96 Super User 2026 Season 1

Last 30 days Overall leaderboard