logo

NJP

Override a one 2 many relation, making it a m2m

Import · Mar 08, 2021 · article

If you have 3 tables:

Table B has a reference to A and another to C.

If you want to relate records from table A to C, making B behave like a m2m, you need to override the Edit that shows on the RL with the action name 'sysverb_edit_o2m' (on table A)

The condition of the new Edit UI action will be:

(new GlideRecord(current.getTableName())).canWrite() && RP.isRelatedList() && !RP.getListControl().isOmitEditButton()

And the script will look like this:

var uri = action.getGlideURI(); 

var path = uri.getFileFromPath(); 

uri.set('sysparm_m2m_ref', current.getTableName()); 

uri.set('sysparm_stack', 'no'); 

uri.set('sysparm_query', ''); 


uri.set('sysparm_collection_related_field', '[field on table B that references table C]'); 

uri.set('sysparm_collection_related_file', '[table C name]'); 

action.setRedirectURL(uri.toString('sys_m2m_template.do')); 

This way, when you click on Edit on the Related list for table B (from Table A form), table B will behave like a m2m table between A and C.

Please let me know if you have any comments,

David Neves

View original source

https://www.servicenow.com/community/developer-articles/override-a-one-2-many-relation-making-it-a-m2m/ta-p/2316732