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 / Collection item remove...
Power Apps
Answered

Collection item remove and add in collection then replicate same changes in CDS entity

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi,

I have one Gallery on screen. using collection to load data in  Gallery. I want to add record in collection and remove some record from collection. once addition and deletion is completed want to  update same change in CDS entity. my code is working to adding record in collection and remove code also.

But how can I populate same changes in entity level too after collection changes is done?
Using below code.

ClearCollect(HazardRecordsCollection,Filter(Hazards,ChemicalID=VarChemicalselect1));
Item property of Gallery :- HazardRecordsCollection
Add remove Icon on item of gallery :- Remove(HazardRecordsCollection,ThisItem)
Add button to add new record:-  Collect(HazardsymboldCollection,{Hazard_Name:"HazardIsCorrosive",Chemical_ID:Varchimicalid})

Above code is working. I have deleted 3 record from gallery(HazardRecordsCollection) and add new record in gallery.
now same I want to populate on entity from collection. using below code but it is not working.
on save button of screen :- onselect -->Collect(Hazards,HazardRecordsCollection)

 

Please suggest how can I replicate same change on entity from collection.



Regards,

Lalit Kadam

 

 

Categories:
  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    Hi @Anonymous 

    Is there some reason that you are using collections when you could just as easily add and modify items in your entity directly?

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Gallery is one part of my screen there are other text box and drop down control as well. all changes should get update to gather on singal save button on database therefore I want update first on collection then entity.

  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    @Anonymous 

    As you are using the Collect() function to update your entity, be aware that this function can only add new records and cannot remove or modify existing records.  You would need to use a Patch() command to modify existing records and a Remove() or RemoveIf() command to delete one.  The Update() command is used to replace an entire record.  UpdateIf() is used to select the records to replace based on a formula.  The syntax for Update() and UpdateIf() from the documentation is:

     

     

    Update( DataSource, OldRecord, NewRecord [, All ] )
    DataSource – Required. The data source that contains the record that you want to replace.
    OldRecord – Required. The record to replace.
    NewRecord – Required. The replacement record. This isn't a change record. The entire record is replaced, and missing properties will contain blank.
    All – Optional. In a collection, the same record may appear more than once. Specify the All argument to remove all copies of the record.
    
    UpdateIf( DataSource, Condition1, ChangeRecord1 [, Condition2, ChangeRecord2, ... ] )
    DataSource – Required. The data source that contains the record or records that you want to modify.
    Condition(s) – Required. A formula that evaluates to true for the record or records that you want to modify. You can use column names of DataSource in the formula.
    ChangeRecord(s) - Required. For each corresponding condition, a change record of new property values to apply to records of DataSource that satisfy the condition. If you provide the record inline using curly braces, property values of the existing record can be used in the property formulas.

     

    Also, once you have made modifications to the underlying entity, you will need to ClearCollect() your collection to reflect the changes. 

     

     

  • Verified answer
    v-xida-msft Profile Picture
    Microsoft Employee on at

    Hi @Anonymous ,

    Do you want to update the same changes applied on your collection to your CDS Entity when you press the "Submit" button?

     

    I have made a test on my side, please consider take a try with the following workaround:

    Modify the formula within the "Remove"Icon in your Gallery to following:

    Collect(RemoveCollection, ThisItem); // Add this formula
    Remove(HazardRecordsCollection,ThisItem)

    Modify formula within the "Add" Icon in your Gallery to following:

    Collect(AddCollection, {Hazard_Name:"HazardIsCorrosive",Chemical_ID:Varchimicalid}); // Add formula here
    Collect(
     HazardsymboldCollection,
     {
     Hazard_Name: "HazardIsCorrosive", Chemical_ID: Varchimicalid
     }
    )

    Then modify the formula within the OnSelect property of the "Submit" button to following:

    ForAll( // Remove records from Entity
     RemoveCollection,
     RemoveIf(Hazards, Hazard_Name = RemoveCollection[@Hazard_Name] && Chemical_ID = RemoveCollection[@Chemical_ID])
    );
    ForAll( // Add records to Entity
     AddCollection,
     Patch(
     Hazards,
     Defaults(Hazards),
     {
     Hazard_Name: AddCollection[@Hazard_Name],
     Chemical_ID: AddCollection[@Chemical_ID]
     }
     )
    );
    Clear(RemoveCollection);Clear(AddCollection) // Clear RemoveCollection and AddCollection

    Please consider take a try with above solution, then check if the issue is solved.

     

    ForAll function and Patch function

     

    Best regards,

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

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 382 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 329

#3
WarrenBelz Profile Picture

WarrenBelz 187 Most Valuable Professional

Last 30 days Overall leaderboard