powerappspowerapps-canvaspowerapps-formulacommon-data-service

How can I insert selected Listbox items as a new record in to a common data service table


I have a PowerApps canvas App and I have a requirement of creating new records for all selected listbox items in to a table. I am acually trying to implement a many to many relationship scenario

my Listbox name is: ListBox1 and my table name is ContactsTable inside the page I have put a button and on click of button I put the following PowerApps logic:

ForAll(
    ListBox1.SelectedItems,
    Patch(
        ContactsTable,
        Defaults(ContactsTable),
        { EmpID: 2, Name: ListBox1.Selected.Name }));

it creates the last selected item repeated based on the number of items i select from listbox any Idea would be great.


Solution

  • When specifying the Name property of the record that you are inserting into the Contacts table, you need to use the record that is being iterated in the ForAll - using the ThisRecord qualifier is a good way to do that:

    ForAll(
        ListBox1.SelectedItems,
        Patch(
            ContactsTable,
            Defaults(ContactsTable),
            { EmpID: 2, Name: ThisRecord.Name }));
    

    Technically you don't even need the ThisRecord (i.e., Name should be in scope), but I find that using it make it clearer.