
Creating a M2M table
In the following demo, we'll create a many-to-many relationship between Problems and Virtual War Rooms. This isn't functionality that we're likely to need to use, but it makes for a decent demo:
- In the application navigator filter text bar, enter sys_m2m.list, and press Enter. This will take you to the Many to Many Definitions table. Even in your relatively stock developer instance, you should see at least two or three many-to-many definitions. Note that these records are not the M2M tables themselves, they are simply the definition records for those tables. Instead, they define the many-to-many relationship, for example, the names of the two tables that are linked.
This is a great example of using the .list shortcut to navigate to a table that isn't otherwise accessible from the application navigator! It's also a great example of why it's important to remember a table's name, as well as its label.
- At the top-left, click on New, to be taken to the New record form.
- In the From table field, we're going to choose our left-hand table. The left table is generally considered the primary in the M2M relationship, but this isn't important here. For our demo, select Virtual War Room [u_virtual_war_room].
- In the To table field, select Problem [problem] as the right table. The other fields on the form will auto-populate.
- You may then want to edit the pre-filled values, such as the many to many table name. I'm going to call mine u_war_room_prob.
- Finally, click on Create Many to Many at the top-right, or bottom-left of the form.
The result of creating this M2M table definition, is that a new table has been created with the name we specified (I used u_war_room_prob). This table's form will have two Reference fields: One for the Problem, and one for the Virtual War Rooms. Thus, the Problem and Virtual War Room tables are now linked via this intermediary table!
Normally when you have a Reference field on one (child) table that points to a record in another (parent) table, you can add a related list on the parent table form in order to show all of a given record's child-records. However, in the case of a M2M table, the Reference field isn't on the child record-it's on an intermediary record. Luckily, ServiceNow understands this M2M relationship as defined in the sys_m2m table, and makes it easy for us to display this relationship on both tables, using related lists. Here's how:
- Head on over to the Problem table. If you want to be slick about it, enter problem.list into the application navigator filter, and press Enter.
- Open any problem record by either clicking on the display value, or clicking on the Reference icon
.
- Note that you may need to click Open Record after clicking on the Reference icon, if you're on List v3.
The view that shows up when hovering over a Reference icon is called the sys_popup view. By default, the default view is shown in the popup, but you can customize the fields that show here, by creating a new view for a given table with that name (sys_popup).
- In the problem record, right-click on the header and go to Configure | Related Lists:
- In the corresponding list, select the Related Lists called simply Virtual War Rooms, and either double-click it, or click the right-arrow between the two list buckets:
- Click Save, and you should be returned to the problem form, where you'll see a new related list: Virtual War Rooms.
- As mentioned earlier in this section, it's probably rare that we're going to be making use of this relationship since it currently requires a manual association between a problem and a virtual war room record, so even though we might expect most problems to be generated from major incidents, which would also result in virtual war rooms being created, let's make sure that this related list only shows up when it has something in it. Right-click in the header of the Virtual War Rooms related list at the bottom of the problem form, and go to Configure | List Control.
- Check the box Omit if empty.
Now let's repeat the preceding steps on the Virtual War Room table, and add the Problems related list there. This way, we can see the relationship from both directions!
It's likely that most relationships between a war room and a problem will be one-to-one, but by making this M2M table and relationship, we allow for the possibility of having multiple problems generated from one major incident associated with multiple war rooms, or one problem addressed by multiple war rooms from multiple major incidents, which were only discovered later, to be related to the same problem.