The CreatorCon Call for Content is officially open! Get started here.

What purpose does the sys_m2m table actually serve?

Stephen W_
Giga Guru

I understand it is used to create many to many relationships..

However, the first step for creating a m2m relationship, is to create an intermediary table.

I've created a table which has only two distinct fields, just location1 and location2 reference fields.

I've added the related object(red items in the slushbucket) to the form.   I've also added the related list to the bottom of the form.

I can add locations to a location using this related object form to associate the locations.

I haven't done anything with the sys_m2m table yet, because I fail to see what value it adds.

Can anyone enlighten me?

Thanks

1 ACCEPTED SOLUTION

tltoulson
Kilo Sage

Hi Steve,



First and foremost, in the wiki article Creating a Many-to-Many Relationship - ServiceNow Wiki, ServiceNow clearly states that for new Many to Many relationships you should always use the M2M Definitions table.   I would not take that lightly.   ServiceNow could be doing any number of things under the hood with Glide Objects or DB Objects for any number of reasons.   The point is we don't really know but we do know that ServiceNow supports M2M relationships via the M2M Definitions table.



On a more practical note, the M2M relationship definition method creates a related list that by default uses the "Edit" button slush bucket.   In most cases, the preferred behavior on a many to many relationship is to select from a slush bucket rather than create new records.



So yes, you can create a table that references two tables but that isn't truly a M2M relationship in ServiceNow terms.   To create a ServiceNow M2M relationship, use the sys_m2m table.



I hope this helps!


View solution in original post

13 REPLIES 13

Sorry for orphaning that.   I realized after posting that that I am ignorant for the most part about when and how slush-bucket lists are rendered, when see the joiner table records in UI, etc., and that's a difference between sys_m2m-created vs. traditionally-created joiner table.   So my conclusion was wrong (that only difference in the end are the sys_m2m reference records).   I don't have time to play with and learn that now so thought best to abort.


...someone is bound to read this thing at a later date...

 

Exactly, Travis. Your last sentence is spot on. I am the someone 🙂

Anurag Tripathi
Mega Patron
Mega Patron

Hi Steve,



I feel by creating a new table and adding 2 location reference fields what you have done here is almost the same thing that sys_m2m is supposed to do. Since you have added the reference fields so you are able to attach records and related objects let you map it through the related list. But why to take so much pain when you already have an OOB feature to accomplish it. But when working on a tool its always good to use as many as oob features as possible. Also, if in future you face any issues during the upgrade or anything SN will handle it but this customization you have done will not be supported by them.



Also , are you getting the "Edit" button on your related list??


-Anurag

mev
Tera Contributor

Does anyone know how to import an excel spreadsheet that contains a many to many relationship? 

For example, I'm trying to import all AD groups that are associated with each job role.  I've already built the definition on the sys_m2m table, but I'm trying to avoid having to map the relationships manually if possible.  

Thanks,

Patrick

mev
Tera Contributor

NVM.. I didn't realize this created an actual table for me to run my import against.  Easy Peasy.