Salesforce Certified Platform App Builder – 3 – Data Modeling and Management Part 3
- Creating a Lookup Relationship
So now we’re going to create a Lookup field, sometimes called a Lookup Relationship, and it’s where you relate one record to another across different objects at Salesforce. And so I’m on a case for this example and I pulled one up from the Cases tab and you notice over here on the right are the details of this case. And these are some of the standard fields that come by default with a case record. And so I want to add a Lookup field to the Case object. And for this example, I want to do a look up to another contact. We have a primary contact here on the case and it’s Stella Pavlovlova who’s a contact with United Oil and Gas Corp. So if I click on that account, I can see if there’s any other contacts related to this account that would be on the Related tab.
There’s also Lauren Boyle, Avid Green and Arthur Song. And so let’s say for this case then Stella Pavlova is the primary contact that we have recorded on the case. And that was case number 1002, for example. But we want to have a secondary contact that we want to add to the case object so we could record someone being a secondary for this case in case Stella is not available. And so I’m going to go to Setup and add a new field that uses a Lookup Relationship. So if I go to Objects and Fields and then Object Manager and I’m working with the Case Object which is a standard object in Salesforce and go to Fields and Relationships, I want to create a new field on the Case object. And there’s a section here for relationships. There’s Lookup Relationship and then there’s also External Lookup Relationship, which we’ll be getting into in the next lecture.
So we’re going to do Lookup Relationship and then scroll down and click Next. We’re going to relate this Lookup Relationship field to the Contact object and then click Next. And so the field label is Contact by Default. We want to change this to give a little more detail. This will be secondary contact so it’s not confused with the primary contact that’s already on the case object. So we’re going to leave the child Relationship name the default of cases. But you can hover over here to get a little more help on this. It says the child relationship name is an internal reference and is used for integration purposes. Be careful when changing the child relationship name as it may affect existing integrations. I recommend that you just leave the default on this field. Unless there’s some good reason to do otherwise, I’m not going to make this field required.
So we leave this unchecked and then with Lookup Relationships, you can make the decision of what to do if the Lookup record is deleted and the Lookup record would be the secondary contact. So in this example, we’re going to make Arthur Song a secondary contact on that case. So for example, if Arthur Song’s contact record was deleted, what would we do with this case? Would we either clear the value of the secondary contact field and says you cannot choose this option if you make this field required? The other option is don’t allow deletion of the lookup record that’s part of a Lookup relationship and so it would block the user from being able to delete that contact record because he’s associated with some cases because he’s populated in the secondary contact field. So I’m just going to leave this by default to clear the value of this field.
And then you can do Lookup filters. This is a powerful functionality that you can do with Lookup fields is you can add criteria to limit the search results. One starting point is you can click Insert suggested criteria and here are some that are just populated by default dynamically. And I am going to use this first one here because we only want secondary contacts on cases that belong to the account that also the primary contact belongs to. You don’t want a case for dealing with one company and inadvertently select the wrong John Doe that belongs to a different company. So I’m going to click Insert and once again this is Contact. The account ID matches the case account ID. So the contacts account ID matches the case account ID.
So I’m going to click Insert and so the filter type, you can do this as optional. The user can remove the filter inner values that don’t match criteria. I’m going to do that so that basically by default it’ll try to bring up matches that belong to the same account, but then you can optionally override that and include a secondary contact that belongs to a different account account. I’ll show what that looks like here. In a moment I’m going to enable the filtering by having this checked. I’m going to add this additional information to the Lookup window. So now I’m going to click Next and then we’ll look at this from the front end to see what this looks like to the user. This Next screen should be familiar. It’s establishing the field level security for this field and we’re making it visible to certain profiles and making it editable by leaving the read only column unchecked and clicking Next. There are several different page layouts for case that comes standard out of our free developer account. And so we’re just going to click Next and add this field to all the page layouts. Now when you create a Lookup relationship field, then there will be a related list that comes along with this.
And you want to pay attention here. This is telling you to specify the title that the related list will have in all of the layouts associated with the parent. So the parent would be the secondary contact and so the related list label will be added to the contact page layout for these different page layouts that are specified and we want to provide further guidance for what this related list will be labeled. We don’t want it to just be cases, but a little further guidance to know that these are cases where this person serves as the secondary contact. So I’m going to say secondary contact cases and then I’m going to click Save. And so now we’re going to go back to our case that we were on. By clicking the Cases tab and selecting this case number 1002, we see Stella Pavlova is the primary contact and that’s under Contact Name. That’s a standard filled on case.
But we want to look for our secondary contact field, which we just added, which isn’t showing up. So I’m going to refresh my browser and then we’ll return back to this case and we’ll see if we can find it in the details. Here we have the secondary contact and this is a Lookup field. So if I click on this pencil icon, it’ll bring up the lookup. I’m going to search for Arthur’s Song and select him and click Save. So now Arthur has been added as a secondary contact on this case and here is his contact record. So if I click on his contact record and then for the related list, we’ve got the cases. And this would be any cases that he’s the primary contact on, but then we have the secondary contact cases. That is the related list label that we specified earlier.
And so it lists any cases where he is the secondary contact for which is 1002. And so if I wanted to change the secondary contact to someone that doesn’t belong to this company, let’s select someone that doesn’t belong. So right now, if you notice, I allowed for people that don’t belong to this company to be associated with the case. I’m going to go ahead and click Save here and it’ll allow that even though he belongs with a different company. Now, your use case, that might be allowable because you might have cases where there’s multiple contacts tied into that. But let’s look at this in Salesforce Classic. So this is case 1002. So as we switch over to Classic, I can do a search for that case number and pull it up this way. And so for the secondary contact, I want to show you what this looks like in Salesforce Classic. So let’s say that I want to put this back to Arthur Song. For instance, I could select him and here’s where you can see the additional help text which is only available in Classic.
It didn’t provide this enlightening experience. So it looks like this additional search window information is only available in Classic, where I’m saying select a secondary contact. For this case, it needs to be a contact that belongs to the same account that this case belongs to. And I’m going to select Arthur Song and revert this back to someone that belongs to the same account. And now I’m going to show you the experience in Classic if I tried to instead do someone that doesn’t belong to this account. So I’m searching for Andy. And so here I’m not able to pull up someone that belongs to a different account because the search filter works in Salesforce Classic and it didn’t work properly in Lightning experience.
And so you can see that there’s no matching search results, even though we know there’s someone named Andy. That’s a contact in our database in Salesforce, because we’ve got a search filter to just have that criteria, just allow for results for where the contact account and the case account equal the same. And so it’s saying that results are narrowed down by criteria set by the administrator. And so I did allow for the user to be able to override those search results, but they’ve got to actually click Show all results. And this is what I was trying to exemplify in Lightning.
Since it’s not supported, it just lets you just go right through and select Andy Young. So I do anticipate Salesforce enabling search filters more fully enlightening experience, but right now that’s not the case. So now I can override that search filter criteria by clicking that link that I showed earlier and clicking Andy Young and clicking Save. In the end though, we’ve created a lookup relationship and that’s between the contact in the case with this new lookup relationship field called Secondary contact. And so in the next lecture, we’re going to learn how to create external look up relationships.
- Creating an External Lookup Relationship
So now I’m going to touch briefly on creating an external Lookup Relationship field. So we previously created this Secondary Contact Lookup field and when we were doing that, you may have noticed another field type called External Lookup. And so we’ll be able to touch on this briefly right now and we’ll revisit it more in full once we get into creating external objects. But we won’t be able to go all the way through creating an external Lookup field because we have no external objects in our instance. But let’s go ahead and start the process and I’ll show you what I’m talking about.
So if I click the Force Quick Access menu and I’m in Salesforce Classic right now, and click View Fields and we see our Secondary Contact field under the Custom Fields and Relationships section and this is a lookup under the Contact object. And so if we click New to create a new custom field, and then underneath the Lookup Relationship, there’s this External Lookup relationship and that says it creates a relationship that links this object, which were on the case, to an external object whose data is stored outside of the Salesforce organization.
And so, like I said, we don’t have an external object created in our own Salesforce instance yet, but I want to go ahead and click this radio button and click Next to show you the next screen at least. And so here is where we want to choose the related external object. And this is where we’re going to come to an end on this particular lecture. But I wanted to at least address that field since you’re seeing that as it relates to creating these new fields because it’s close to that other Lookup Relationship field. And so we’re going to revisit this later once we get into the lectures around creating external objects.
And so I wanted to at least put a placeholder here for this particular field type so that you know that it is an option. So bear this in mind because we are going to be hooking this in and hooking your own salesforce instance into some external data sources that I’m really excited to show you because this really starts to get more developer related tasks as we get deeper into this course and deeper into the things that will be on the exam.
You’re going from more administrative type functions and setup and configuration to getting into more development related tasks and so you’re well on your way. Hang in there. And next though, we’re going to be getting into another really interesting field that we’re going to be creating, which is a Master Detail Relation.
- Creating a Master-Detail Relationship
So now we’re going to create a master detail relationship between two objects. And the way that’s done is by creating a master detail field on the child object that is also known as the detail object in this relationship. And so I’ve got the schema builder up and I’ve got the four primary custom objects that we’ve created thus far for our application that we’re building throughout at this course. And those objects are the production object, the episode object, the person object, and the production position object. And these four have no lines in between them so they’re not related at all. And so I wanted to show an example of how to create a master detail relationship. And we most recently created this episode custom object and this has to do with an episode of a TV show which is a type of production.
And so I’m going to have a relationship created between the episode and the production. So the production object will be the master and the episode will be the detail. The idea behind this is that you can’t have episodes of a TV show without the TV show itself. And so I’m going to click on the Elements tab and then down here below, under the different types of fields you can create, I’m going to drag the master detail option onto the episode object. And so that brings up the Create Master detail field window on the object of episode underscore underscore C because that is a custom object. So for our field label I’m going to call it TV Show. Then in the description I’m just describing this as the TV show production that this episode belongs to. And so I’m going to relate this new master detail field to the production object. This will be the master.
So this is select the other object to which this object is related. So then the child relationship name I’m going to leave that the default of episodes. Then for the sharing setting, we need to select the minimum access level required on the master record to create, edit or delete related detail records. So once again the master record would be the production and specifically it’s a production record type of TV show. And then the child or the detail record is the episode object. So you can either do Read only and that allows users with at least read access to the master record to create, edit or delete related detail records. Or we could select Read Write, which allows users with at least read Write access to the master record to create, edit or delete detail records. So we’re going to leave this at Read and Write and then the related list label.
This is where you specify the title that the related list will have in all of the layouts associated with the parent. And so for example, if you were on the detail page for a TV show, the related list for the specific episodes would have a related list called Episodes. So we’re going to leave that as it is then as well, this repairable Master Detail, if you check this, it allows child records can then be repaired to other parent records after they are created. So I like to make this repairable when I do a Master Detail relationship so that if a user specifies the wrong TV show, for instance, they could then repair that and let’s say they selected Bonanza instead of Gun Smoke. They could then make that change if they need to. So that gives greater flexibility. And so I’m going to click Save now.
And so you see this error message on the top? It’s saying you cannot create a new Master Detail relationship on an existing custom object if records already exist. You must first create a Lookup relationship, populate the Lookup field with data in all records and then change the relationship type to Master Detail. So rather than going through all those steps that describes there, I’m going to delete the custom object record that I created. If you recall on the Episode object, I had this one record that I entered for Pilot back when we created a season number field. And so I’m going to delete this record and click OK. And then I’m going to go back to the Schema Builder and make sure that I can go ahead and save this Master Detail relationship. So I’m going to click Save and voila.
We now are able to make that connection. Now, it’s not that big a deal to delete that one record, but if you’re wanting to have two different objects enter into a master detail relationship, and you’ve got thousands of records already populated, then you would need to first create that as a lookup field and then populate all those records with the necessary data before you could flip it to a master detail relationship. But the primary thing to keep in mind or to worry about at this point is that you can see now that we’ve got this connector between production and Episode and that’s a Master Detail relationship from Episode underscore C to production underscore underscore C.
So we will be creating additional Master Detail relationships in later lectures. But for now I want to bring this particular lecture in for landing because I don’t want to go too in depth at any one time. And so we’re piecing this out and building upon prior understanding and knowledge. And so we’re just going to continue on now from here and learn how to make another type of field. And the next one for us to create is an encrypted.