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 / Dataverse - Many to Ma...
Power Apps
Answered

Dataverse - Many to Many - Patching

(0) ShareShare
ReportReport
Posted on by 3

Hi Everyone,

 

I am just dipping my toe into the dataverse and have come across a little issue that I am hopefully someone can help guide me on.

I have created two tables, a Department table and a Courses table

 

A department can be assigned many courses and a course can have many departments.

 

I believe i have all configured correctly on the dataverse side, however when i add a patch command in a canvas app strange things happen.

 

1. I use the code below on a button to patch a record into the departments table and then relate that to selected courses

 

ClearCollect(
coltraining,
Gallery4.AllItems
);
Patch(
Departments,
{
Name: TextInput1.Text,
Manager: Dropdown1.SelectedText
}
);
Refresh(Departments);Set(
newDepartment,
Last(Departments)
);ForAll(
coltraining,
Relate(
newDepartment.Trainings,
ThisRecord
)
);
Reset(TextInput1);
Reset(Dropdown1);
Reset(lstboxavailablecourses);
Clear(colselectedcourses);
Clear(coltraining);

 

However I am getting strange behaviour with this, as when I add a new department and relate some courses to it, the previously added department also gets those courses. I think this maybe due to a timing issue as the newDepartment variable may not have time to update before the patch kicks in. to get around this i split the command up.

 

First I put this code in a button

 

ClearCollect(
coltraining,
Gallery4.AllItems
);
Patch(
Departments,
{
Name: TextInput1.Text,
Manager: Dropdown1.SelectedText
}
);
Refresh(Departments);Set(
newDepartment,
Last(Departments)
);Set(timerreset,false);Set(timerstart,true);

 

Then in the Timer I put this code.

 

ForAll(
coltraining,
Relate(
newDepartment.Trainings,
ThisRecord
)
);
Reset(TextInput1);
Reset(Dropdown1);
Reset(lstboxavailablecourses);
Clear(colselectedcourses);
Clear(coltraining);Refresh(Departments);Refresh(Trainings);Set(timerreset,true);Set(timerstart,false)

 

The theory been that, the button would be pressed the timer would run for 2 seconds, giving enough time for the refresh to complete and the last record to be returned, and it seems to work. Not sure if it is logically but it works. Could someone tell me if this is expected behaviour or perhaps I am missing something obvious.

 

2. The second issue may well stem from the above setup. But when the timer ends I get the following error on it.

 

powerapp_error.JPG

 

 

The records seem to add OK, but this error always appears. Is it telling me that a I am trying to add a record to the departments table that already exists, I dont see how as I am using unique values for the departments.

 

Again, if anyone can help with this it would be much appreciated.

 

Thanks

 

Ronan

Categories:
  • Verified answer
    v-xiaochen-msft Profile Picture
    Microsoft Employee on at

    Hi @monatech ,

     

    Please use table directly instead of using a collection (collection storage table).

     

    So please try this formula:

    Patch(
    Departments,
    {
    Name: TextInput1.Text,
    Manager: Dropdown1.SelectedText
    }
    );
    Refresh(Departments);Set(
    newDepartment,
    Last(Departments)
    );ForAll(
    Trainings,
    Relate(
    newDepartment.Trainings,
    ThisRecord
    )
    );
    Reset(TextInput1);
    Reset(Dropdown1);
    Reset(lstboxavailablecourses);
    Clear(colselectedcourses);
    Clear(coltraining);

     

    Best Regards,
    Wearsky
    If my post helps, then please consider Accept it as the solution to help others. Thanks.

  • monatech Profile Picture
    3 on at

    Sorry for the long wait, thanks so much thats working the best

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 224 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 189 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 126

Last 30 days Overall leaderboard