Create M2M table between Reference field and DocumentsID field

SN_Learn
Kilo Patron
Kilo Patron

Hi All,

 

I want to create a m2m table. The first table is the incident table and the second table is oob table which contains a fields of type documentID and this field stores Incident number in it.

 

The objective is to create a m2m table on the above 2 tables and add it to the related list of incident table.

 

Could you please guide me how to create m2m relationship between the above 2 tables? 

 

 

Thankyou.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.
1 REPLY 1

-O-
Kilo Patron
Kilo Patron

Are you sure it is an m2m table that you want?

Creating the table alone will not make the records related to incidents to show up. Something must create records in the m2m table.

B.t.w. m2m tables are not created between "reference fields" and "document id" fields - it does not matter what kind of fields are in the two tables related through the m2m table.

The m2m table will simply reference a record in one table and a record in another table.

So it is the m2m table that will contain and defined what is related without any influence by the related records.

Also document id fields (just as reference fields) don't store "numbers", those store foreign keys - named sys_id in ServiceNow.

 

I get the feeling what you really want is a custom Relationship (find it under System Definition module).

When creating one, you select "Incident" in field "Applies to table" and the other, OOB table in field "Queries from table".

Than, in field "Query with" you add script to filter the other, OOB table to only "select" the records that are related to a given incident.

Something like:

(function refineQuery (current, parent) {
	current.addQuery('<name of the document id dependent field, the one that contains the table name>', parent.getRecordClassName());
	current.addQuery('<name of the document id field>', parent.getUniqueValue());
})(current, parent);

Document id fields are always used in conjunction with a dependent field that contains that name of the table in which the record identified by the document id will be located.