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.

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