cancel
Showing results for 
Search instead for 
Did you mean: 

Adv Hands on Lab 3: Complex Data Relationships with Common Data Service

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.

 

Note: The lab requires a PowerApps environment with a Common Data Service (CDS) database created and the sample apps installed.

 

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.

  • Navigate to https://make.powerapps.com/ and make sure you are in the correct environment.
  • Select Solutions and click Import.
  • Click Browse.
  • Select the zip file locate in the lab resources folder and click Open.
  • Click Next.
  • Click Next
  • Click Import and wait for the import to complete.
  • Click Publish All Customizations and then Close.
  • Click to open the Event Management
  • Filter the view on Entities.
  • You should see the entities shown in the image below.
  • Filter on Canvas App.
  • You should see the Event Day Manager
  • Filter on Model-Driven App.
  • You should see the Event Manager
  • Filter on Option set, Process, and Other, and examine what is in under each category.
  • Select Apps and click to open the Event Management
  • Select Events and click + New.
  • Enter Denver Dynamics Day for Name, select Start and End Dates in the future, and then click
  • Select the Sessions tab and click Add New Session.
  • Enter Taking your Dynamics 365 users farther for name provide Start and End dates/time and click
  • Go to the Speakers sub-grid and click Add Existing.
  • Select Jim Glynn and click Add.
  • Click + New.
  • Provide the information below and click Save. From classic to unified in 30 minutes.
  • Go to the Speakers sub-grid and click Add Existing.
  • Select Maria Campbell and click Add. You should have one speaker in the Speakers sub-grid.
  • Click on the Event
  • Select the Registrations tab and click Add New.
  • Provide the information below and click Save.
  • Registration code should get generated.
  • Select Registrations and add the rest of the registrations shown in the image below.
  • 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.

  • Navigate to https://make.powerapps.com/ and make sure you are in the correct environment.
  • Select Solutions and open the Event Management

 

  • Click to open the Session
  • Select the Relationships tab and click Add Relationship.
  • Select Many-to-One.
  • Select Contact for Entity, enter Primary Speaker for Display Name. Do not click done yet!
  • Click on General to show the name for the relationship. 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 Click Done.
  • Click Save Entity.

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

  • Select the Forms tab and open the Main
  • Drag the Primary Speaker field to the form and drop it above the Summary field.
  • Select the Primary Speaker
  • Go to the properties pane, change the Default View to Registered Speakers, and uncheck the Allow Users to Change checkbox.
  • Click Save.
  • Click Publish.
  • Close the form editor browser tab.
  • Click Done.

 

Task 4: Record session attendance

  • Navigate to https://make.powerapps.com/ and make sure you are in the correct environment.
  • Select Solutions and open the Event Management
  • Click to open the Contact
  • Select the Relationships tab and click Add Relationship.
  • Select Many-to-Many.
  • Select Session for Related Entity, enter attended_sessions for Relationship Name, and click Done.
  • 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.

 

  • Navigate to https://make.powerapps.com/ and make sure you are in the correct environment.
  • Select Solutions and open the Event Management
  • Click to open the Sessions
  • Select the Relationships tab and click to open the Event
  • Expand the Advanced Options
  • Change the Relationship Type to Parental and click Done.
  • Click Save Entity.
  • Select Solutions and Publish All Customizations.

Exercise 2 - Show session details on canvas app

 

Task 1: Explore canvas application

  • Navigate to https://make.powerapps.com/ and make sure you are in the correct environment.
  • Select Apps, click on the More Commands button of the Event Day Manager app, and click Edit.
  • Click Allow.
  • The designer should load with an error. You will fix the error in Task 2.
  • Select the MainScreen.
  • Hold the ALT key of your keyboard and click on the Event.
  • The visible controls will change.
  • 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

  • Select the SessionGallery.
  • 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

  • Select lblCountSpeaker.
  • Set the Text value to the formula here. You should get an error.

CountIf(EventGallery.Selected.Registrations, 'Registration Type' = [@'Registration Type'].Speaker)

  • Select lblCountAttendees and set the Text value to the formula below.

CountIf(EventGallery.Selected.Registrations, 'Registration Type' = [@'Registration Type'].Attendee)

  • You will now fix the errors. Select the View tab and click Data Sources.
  • Click Add Data Source.
  • Select Common Data Service.
  • Select Registrations, Users, Sessions, and Teams, and then click Connect.
  • You should now have five entities selected.
  • The errors should go away, and you should now see a speaker count. Close the Data

 

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

  • 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

  • Expand the SessionScreen and select lblCountSession.
  • Set the text value to the text below.

CountRows(SessionGallery.Selected.Contacts)

 

Task 2: Set Session Primary Speaker

  • Expand the SessionScreen
  • Select lblSpeakerName
  • Change the Text value of lblSpeakerName from “James Phillips” to the text below.

SessionGallery.Selected.'Primary Speaker'.'Full Name'

 

Task 3: Add controls and enable attendee registration

  • Expand/select the SessionScreen
  • Select the Insert tab and click Label.
  • Rename the label lblAttendeeName.
  • Move the label below the attendee label.
  • Click Button.
  • Move the button below the label.
  • Rename the button ButtonRegister.
  • Change the button text to Come in!
  • Select iconLookupAttendee.
  • 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');

  • Select lblAttendeeName and set Text property value to ScanAttendeeName.
  • 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)

  • 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());

  • Click File and Save.
  • Click Publish.
  • Click Publish this Version.

 

Task 4: Test your work

  • Navigate to https://make.powerapps.com/ and make sure you have the correct environment.
  • Select Apps and click to open the Event Management
  • Select Events and double click to open the event.
  • Select the Registrations tab and make a note of the Speaker registration types. You should have two for this event.
  • Select the Sessions tab and click to open one of the sessions.
  • You should have a Primary Speaker lookup on the form. Click on the lookup.
  • You should only see the speaker registration types and the Change View button should be disabled. Select Yvonne McKay.
  • Select Registrations and double click to open one of the registration records.
  • Copy the Registration Code.
  • Go back to https://make.powerapps.com/
  • Select Apps and click to open the Event Day Manager
  • Click to select event.
  • The counts and owner name labels should show data.
  • Go to the Session gallery and select one of the sessions.
  • 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.
  • The attendee name should display. Click Come In.
Meet Our Blog Authors