You are on page 1of 35

Complex Data Relationships

Hands-on Lab
Table of Contents
Lab Overview..................................................................................................................................................................... 1
Environment .................................................................................................................................................................................. 1
Scenario ............................................................................................................................................................................................ 1
Exercise 1 – Add Relationships and review existing entitles ........................................................................ 3
Task 1: Import the starting solution .................................................................................................................................... 3
Task 2: Record primary speaker for session..................................................................................................................10
Task 3: Add the lookup field to the model-driven app form. ..................................................................................12
Task 4: Record session attendance ....................................................................................................................................14
Task 5: Correct behavior on Event to Session 1:N relationship ............................................................................15
Exercise 2 - Show session details on canvas app.............................................................................................. 18
Task 1: Explore canvas application ....................................................................................................................................18
Task 2: Complete event screen session gallery ............................................................................................................20
Task 3: Populate the Count of Speakers and Attendees............................................................................................20
Task 4: Show the name of the Event Owner...................................................................................................................22
Exercise 3 – Record session attendance............................................................................................................... 24
Task 1: Set the Session Attendance Count ......................................................................................................................24
Task 2: Set Session Primary Speaker ................................................................................................................................24
Task 3: Add controls and enable attendee registration ............................................................................................25
Task 4: Test your work ............................................................................................................................................................28
Lab Overview
Environment
The lab requires a PowerApps environment with a Common Data Service (CDS) database created
and the sample apps installed.

Scenario
This lab is designed to help you understand how to work with Common Data Service relationships
in both canvas and model-driven PowerApps.
Contoso has built an event management solution and designed a CDS data model that includes the
follow:
• Events
• Sessions
• Registration for attendees and speakers
• Association of sessions and speakers
• Automatic generation of the unique registration number
• Restrictions for event registration (a contact only can only be registered once for an event
and registration type, e.g. speaker).
The following is a simple diagram of the data model:

To make it easy for you to get started we have pre-created this part of the data-model in CDS and
have provided you with a solution that contains the entity definitions. Also included in there is a
model-driven and canvas app. The model-driven app is intended to be used by the office staff to
manage setup and registration for the event. The canvas app is intended to be used the day of the
event to quickly lookup information and to mark who attended what session.
With the current data model-above, Contoso doesn’t know who the primary speaker is at a
session. They also don’t know which contacts attended each session. As part of this lab you will
be modifying the data model to:
1. Designate one of the speakers as the primary
2. Record session attendance
The person who was building the canvas app didn’t know how to work with all of the related data,
so it is only partially completed. In this lab you will be helping Contoso finish the canvas app by
adding working with the data relationships in the app.
Exercise 1 – Add Relationships and review
existing entitles
In this exercise you will be importing the pre-built assets into your development environment and
then making the changes to the CDS data model to add the new relationships.

Task 1: Import the starting solution


In this task, you will import the starting solution and examine what is in the solution and create
sample records.
1) Navigate to https://make.powerapps.com/ and make sure you are in the correct
environment.
2) Select Solutions and click Import.

3) Click Browse.
4) Select the EventManagementBase.zip file locate in the lab resources folder and click
Open.
5) Click Next.

6) Click Next again.


7) Click Import and wait for the import to complete.
8) Click Publish All Customizations and then Close.
9) Click to open the Event Management solution.

10) Filter the view on Entities.

11) You should see the entities shown in the image below.

12) Filter on Canvas App.


13) You should see the Event Day Manager app.

14) Filter on Model-Driven App.


15) You should see the Event Manager application.

16) Filter on Option set, Process, and Other, and examine what is in under each category.
17) Select Apps and click to open the Event Management application.

18) Select Events and click + New.


19) Enter Denver Dynamics Day for Name, select Start and End Dates in the future, and
then click Save.

20) Select the Sessions tab and click New Session.

21) Enter Taking your Dynamics 365 users farther for name, provide Start and End
dates/time and click Save.

22) Go to the Speakers sub-grid and click Add Existing.


23) Select Jim Glynn and click Add.

24) Click + New.

25) Select Denver Dynamics Day for Event, enter From classic to unified in 30 minutes for
Name, provide Start and End dates/time and click Save.

26) Go to the Speakers sub-grid and click Add Existing.


27) Select Maria Campbell and click Add. You should have one speaker in the Speakers sub-
grid.
28) Click on the Event name.

29) Select the Registrations tab and click New Registration.

30) Select Nancy Anderson for Contact, select Attendee for Registration Type, and click
Save.
31) Registration code should get generated.

32) Select Registrations and add the rest of the registrations shown in the image below.
Contact Registration Type Paid Fee
Yvonne McKay Speaker Yes 15000
Susanna Stubberod Speaker Yes 15000
Scott Konersmann Attendee No
Robert Lyon Attendee No
Paul Cannon Attendee No

33) Close the application.


Task 2: Record primary speaker for session
Now that we have the base solution imported, you need to make two changes to the data model.
- Add a one-many (1:N) relationship between Contact and Session to represent the Primary
Speaker
- Add a Many to Many (N:N) relationship between Contact and Session to represent contacts
that attended the session.
When completed your new data model will look like the following:

In this task, you will create 1:N relation between Contact and Session and add primary speaker
lookup field to the Session form.
1) Navigate to https://make.powerapps.com/ and make sure you are in the correct
environment.
2) Select Solutions and open the Event Management solution.

3) Click to open the Session entity.


4) Select the Relationships tab and click Add Relationship.

5) Select Many-to-One.

6) Select Contact for Related (One), enter Primary Speaker for Display Name, and click
General.
7) This is an internal name and it is required to be unique. When you have multiple 1:N
relationships between the same entities it is best give it a meaningful name. Change the
name to contact_session_primaryspeaker. Click Done.

8) Click Save Entity.

Task 3: Add the lookup field to the model-driven app form.


1) Select the Forms tab and open the Main form.

2) Drag the Primary Speaker field to the form and drop it above the Summary field.
3) Select the Primary Speaker field.

4) Go to the properties pane, change the Default View to Registered Speakers, and uncheck
the Allow Users to Change checkbox.

5) Click Save.
6) Click Publish.
7) Click on the  Back button.
Task 4: Record session attendance
1) Navigate to https://make.powerapps.com/ and make sure you are in the correct
environment.
2) Select Solutions and open the Event Management solution.

3) Click to open the Contact entity.

4) Select the Relationships tab and click Add Relationship.

5) Select Many-to-Many.
6) Select Session for Related (Many), enter attended_sessions for Relationship Name,
and click Done.

7) Click Save Entity.

Task 5: Correct behavior on Event to Session 1:N relationship


In reviewing the data model, it was discovered that the behaviors on the Event to Session
relationship had been left as default, which is Referential, Remove Link. This means that if an
event is deleted all session records will remain. In this case we want if an event is deleted to have
the session records automatically deleted. To accomplish this, we are going to adjust the
behaviors on the relationship to be Parental.
The following explains the types of behaviors you can configure:
• Referential, Remove Link: Any related records can be navigated to, and actions taken on one will not affect the other.

• Referential, Restrict Delete: Any related records can be navigated to. Actions taken on the parent record will not be
applied to the child record, but the parent record cannot be deleted while the child record exists.

• Parental: Any action taken on a record of the parent entity is also taken on the related child entity records.

• Custom: Custom behavior for each possible action can be selected.

1) Navigate to https://make.powerapps.com/ and make sure you are in the correct


environment.
2) Select Solutions and open the Event Management solution.
3) Click to open the Sessions entity.

4) Select the Relationships tab and click to open the Event relationship.

5) Expand the Advanced Options section.


6) Change the Relationship Type to Parental and click Done.

7) Click Save Entity.

8) Select Solutions and Publish All Customizations.


Exercise 2 - Show session details on canvas
app

Task 1: Explore canvas application


1) Navigate to https://make.powerapps.com/ and make sure you are in the correct
environment.
2) Click to open the Event Management solution.
3) Click on the More Commands button of the Event Day Manager app and click Edit.

4) Click Allow.

5) The designer should load with an error. You will fix the error in Task 2.
6) Select the MainScreen.
7) Hold the ALT key of your keyboard and click on the Event.
8) The visible controls will change.

9) Click on the … button of the App and select Run OnStart. This action will reset the
application its initial state.
Task 2: Complete event screen session gallery
1) Select the SessionGallery.

2) Set Items to the formula below. This will use the 1:N relationship between Event and
Session to get to the sessions of the selected event. The error that was showing next to the
gallery should go away and you should start seeing some session data when you drill into
an event.
EventGallery.Selected.Sessions

Task 3: Populate the Count of Speakers and Attendees


1) Select lblCountSpeaker.
2) Set the Text value to the formula here. You should get an error.
CountIf(EventGallery.Selected.Registrations, 'Registration Type' =
[@'Registration Type'].Speaker)

3) Select lblCountAttendees and set the Text value to the formula below.
CountIf(EventGallery.Selected.Registrations, 'Registration Type' =
[@'Registration Type'].Attendee)

4) You will now fix the errors. Select the View tab and click Data Sources.

5) Click Add Data Source.

6) Select Common Data Service.


7) Select Registrations, Users, Sessions, and Teams, and then click Connect.

8) You should now have five entities selected.

9) The errors should go away. Close the Data pane.

Task 4: Show the name of the Event Owner


Every user/team owned record in CDS has an owner field. The owner field can point to either a
user or a team record. This type of field is referred to as polymorphic because it can be set to
more than one type of record.
To use the data value of this field we must first check what type of record it is. You check this by
using the IsType function. To use the values we must tell PowerApps what record type we want
to look at the data as. To do this we use the AsType() function
1) Select lblEventOwnerName and set the Text value to the formula below. This will check if
record is owned a user or team and display the full name of the user or the team name
accordingly.
If(IsType(EventGallery.Selected.Owner, Users),
AsType(EventGallery.Selected.Owner, Users).'Full Name',
AsType(EventGallery.Selected.Owner, Teams).'Team Name')
Exercise 3 – Record session attendance
In this exercise, you will be modifying the application to record if a contact attends a session. You
will do that by using the Relate() function to associate the contact with the session.

Task 1: Set the Session Attendance Count


1) Expand the SessionScreen and select lblCountSession.

2) Set the text value to the formula below.


CountRows(SessionGallery.Selected.Contacts)

Task 2: Set Session Primary Speaker


1) Expand the SessionScreen
2) Select lblSpeakerName label.

3) Change the Text value of lblSpeakerName from “James Phillips” to the formula below.
SessionGallery.Selected.'Primary Speaker'.'Full Name'
Task 3: Add controls and enable attendee registration
1) Expand/select the SessionScreen
2) Select the Insert tab and click Label.

3) Rename the label lblAttendeeName.

4) Move the label below the attendee label.

5) Click Button.

6) Move the button below the label.


7) Rename the button ButtonRegister.

8) Change the button text to Come in!

9) Select iconLookupAttendee.

10) Select the OnSelect property and set it to the formula below. This sets a variable
ScanAttendee to the first contact that matches entered registration code, sets a variable
ScanAttendeeName to the full name of the contact.
Set(ScanAttendee,First(Filter(Registrations,'Registration
Code'=txtAttendeeCode.Text)).Contact);Set(ScanAttendeeName,ScanAttendee.'Full
Name');

11) Select lblAttendeeName and set Text property value to ScanAttendeeName.

12) Select ButtonRegister and set the DisplayMode value to the formula below. This formula
will enable the button only when a valid attendee is found.
If(IsBlank(ScanAttendeeName), DisplayMode.Disabled, DisplayMode.Edit)

13) Set the OnSelect property of the ButtonRegister to the formula below. This formula will
associate session and attendee, and clears out variables and control.
Relate(
LookUp(Sessions, Session =
SessionGallery.Selected.Session).contoso_attended_sessions, ScanAttendee);
Reset(txtAttendeeCode);
Set(ScanAttendee, Blank());
Set(ScanAttendeeName, Blank());

14) Click File and Save.


15) Click Publish.

16) Click Publish this Version.

Task 4: Test your work


1) Navigate to https://make.powerapps.com/ and make sure you have the correct
environment.
2) Select Apps and click to open the Event Management application.

3) Select Events and double click to open the event.


4) Select the Registrations tab and make a note of the Speaker registration types. You
should have two for this event.

5) Select the Sessions tab and click to open one of the sessions.

6) You should have a Primary Speaker lookup on the form. Click on the lookup.

7) You should only see the speaker registration types and the Change View button should be
disabled. Select Yvonne McKay.
8) Select Registrations and double click to open one of the registration records.

9) Copy the Registration Code.

10) Go back to https://make.powerapps.com/


11) Select Apps and click to open the Event Day Manager application.

12) Allow camera if prompted.


13) Click to select event.
14) The counts and owner name labels should show data.

15) Go to the Session gallery and select one of the sessions.


16) The session screen should load, and the primary speaker should populate. Type the
Registration Code and click on the lookup icon. You can get this code from one of the
registration records in the model driven app.

17) The attendee name should display. Click Come In.

18) Close the canvas application.


19) Navigate to https://make.powerapps.com/ and make sure you have the correct
environment.
20) Select Apps and click to open the Event Management application.
21) Select Events and double click to open the event.
22) Click to open the Event Management application.
23) Select Events and click to open Denver Dynamics Day.
24) Select Sessions and click to open the session you used for testing the canvas app.

25) Click Related and select Contacts.

26) You should see the contact you used to test the canvas application.

You might also like