Querying m2m table

Sathiskumar_D
Giga Sage

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? 

1 ACCEPTED SOLUTION

J_31
Kilo Sage

// 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();
}

View solution in original post

1 REPLY 1

J_31
Kilo Sage

// 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();
}