Introduction
In this blog, we will learn how to use the relate() and unrelate() functions in the Canvas Apps.By the name relate you would have understood that it is going to link or point to something. So, relate() function links two records through a one-to-many or many-to-many relationship in Microsoft Dataverse. While unrelate does the opposite, it removes the link between two records in Microsoft Dataverse.
Syntax –
Relate() – Relate(Table1RelatedTable, Table2Record) – For a record of Table1, the table of Table2 records related through a one-to-many or many-to-many relationship.
Unrelate() – Unrelate(Table1RelatedTable, Table2Record)– For a record of Table1, the table of Table2 records related through a one-to-many or many-to-many relationship.
Let’s understand the concept in detail. Suppose we have tables Teacher and Student where the Teacher has a one-to-many relationship with the Student table. That means one teacher can have multiple students.
Step 1 – Go to make.powerapps.com select the environment and go to solution and create a solution. Inside the Solution select new > App > Canvas Apps. Create a canvas app for a tablet with a blank screen.
Step 2 – In the Data pane, select Add data > select the required tables in our example it is teacher and student.
Step 3 – On the Insert tab, add a blank vertical Gallery control. On the Properties tab, set the Teacher’s gallery Items property to Teachers and its Layout to Image, title, and subtitle. You can also add the “Teachers” label as shown below through the insert tab.
Step 4 – Add a second blank vertical Gallery control, and ensure that it’s named Student. On the Properties tab, set Student’s Layout to title and In the formula bar, set the Items property of Student’s to Teacher.Selected.Students. Add a Combo box control on the Properties tab, set Teachers_List’s Items property to Teachers and scroll down and turn off the Allow multiple selection option. In the formula bar, set Teachers_List’s DefaultSelectedItems property to LookUp(Teachers, Teacher = ThisItem.Teacher.Teacher)
Step 5 – In the Student gallery, set the OnSelect formula for Btn_RelateUnrelateStudent to this formula:
Formula: –
If( IsBlank( ComboBox1.Selected ),
Unrelate( Gallery1.Selected.Students, ThisItem ),
Relate( ComboBox1.Selected.Students, ThisItem )
;
Refresh(Students)
Step 6 – Insert a new gallery named Unassigned_Student and in the formula bar, set its Items property to the below formula on the Filter(Students,IsBlank(Teacher.Teacher)) and on selecting Btn_RelateStudent, write below formula Relate( TeachersList.Selected.Students, ThisItem ); Refresh(Students).
As shown in the above images “Valerie P. Erhat” has three students under her. Now we are changing Pauline’s teacher from “Valerie P. Erhat” to “Lawrence M. Mills” as shown below:
As a result, we can see now there is no Pauline under Valerie as she has been transferred under Lawrence.
Now, if we select nothing for Pauline and click on the arrow then the link between Pauline and the teacher is removed and she is added to the students not involved.
Conclusion
It is easy to relate and unrelated records through Canvas Apps functions.