- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2023 05:26 PM
Hello everyone,
I got two tables.
Table A
Table B.
there is m2m relationship between table A and B.
Now if i want to change m2m table record value, how to script this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2023 07:00 PM
// Set the IDs of the records in Table A and Table B that are associated with the many-to-many relationship.
var tableAId = 'a_primary_key';
var tableBId = 'b_primary_key';
// Create a new GlideRecord object for the many-to-many relationship table.
var m2mGR = new GlideRecord('m2m_table');
// Use an encoded query to find the record in the many-to-many relationship table that corresponds to the association between Table A and Table B.
m2mGR.addQuery('table_a', tableAId);
m2mGR.addQuery('table_b', tableBId);
m2mGR.query();
// If a matching record is found, update the relevant field(s) with the new value(s).
if (m2mGR.next()) {
m2mGR.setValue('field1', 'new_value1');
m2mGR.setValue('field2', 'new_value2');
m2mGR.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2023 07:00 PM
// Set the IDs of the records in Table A and Table B that are associated with the many-to-many relationship.
var tableAId = 'a_primary_key';
var tableBId = 'b_primary_key';
// Create a new GlideRecord object for the many-to-many relationship table.
var m2mGR = new GlideRecord('m2m_table');
// Use an encoded query to find the record in the many-to-many relationship table that corresponds to the association between Table A and Table B.
m2mGR.addQuery('table_a', tableAId);
m2mGR.addQuery('table_b', tableBId);
m2mGR.query();
// If a matching record is found, update the relevant field(s) with the new value(s).
if (m2mGR.next()) {
m2mGR.setValue('field1', 'new_value1');
m2mGR.setValue('field2', 'new_value2');
m2mGR.update();
}