Store value of a field in an array from a table and create new record in the same table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 08:20 PM
I wanted to store a field value from a m2m table in an array which is of any year and through COUNT, I am preventing duplicate records. And then using that array and FY23 year, I wanted to filter records from an another table and then create records again in m2m table.
Below code is not working, I believe, I am doing some syntax error.
Please help me on this asap. Thanks!
var arr = [];
var store = new GlideAggregate('table_1');
store.addQuery('u_record_1=<sys_id of u_record_1>');
store.addAggregate('COUNT', 'u_field_name');
store.groupBy('u_field_name');
store.addHaving('COUNT', '=', 1);
store.query();
while (store.next()) {
arr.push(store.getValue("u_field_name"));
}
var map = new GlideRecord('table_2');
map.addEncodedQuery('u_year_fy23=<sys_id of u_year>^<backend name of u_field_name in table_2>.sys_idIN' + arr.toString());
map.query();
while (map.next()) {
var map2 = new GlideRecord('table_1');
map2.initialize();
map2.u_record_1 = '<sys_id of u_record_1>';
map2.u_field_name = map.sys_id.toString();
map2.insert();
}