The CreatorCon Call for Content is officially open! Get started here.

transfer records from a base table to an extended table

robertpolickosk
Mega Expert

Good Afternoon,

I have created a table which is extended from an existing base table.   I would like to convert some of the existing records in the base table to records in the extended table without duplicating records in the base table.   I have tried to create a script assuming that the sys_id of the extended table should be the same as the sys_id of the extended table.

var grGroupMember = new GlideRecord('sys_user_grmember');

grGroupMember.addEncodedQuery('group.active=true^group.u_assignment=true^group.parent=32698401d4e95d00e6e503ce769e5605^ORgroup.parent=2023a465294dd104e6e58c1e41116c8a');

grGroupMember.query();

gs.print(grGroupMember.getRowCount());

var grGroupRRMember;

while (grGroupMember.next()) {

  grGroupRRMember = new GlideRecord('u_group_round_robin_member');

  grGroupRRMember.initialize();

  grGroupRRMember.sys_id = grGroupMember.sys_id;

  grGroupRRMember.user = grGroupMember.user;

  grGroupRRMember.group = grGroupMember.group;

  grGroupRRMember.u_assignment_available = true;

  grGroupRRMember.update();

}

Unfortunately, it created duplicate records.   Is what I am trying to do possible?

Thank you in advance.

Respectfully,

Robert

1 ACCEPTED SOLUTION

manikorada
ServiceNow Employee
ServiceNow Employee

Try this script:



var grGroupMember = new GlideRecord('sys_user_grmember');


grGroupMember.addEncodedQuery('group.active=true^group.u_assignment=true^group.parent=32698401d4e95d00e6e503ce769e5605^ORgroup.parent=2023a465294dd104e6e58c1e41116c8a');


grGroupMember.query();


gs.print(grGroupMember.getRowCount());


var grGroupRRMember;


while (grGroupMember.next()) {


  grGroupMember.sys_class_name = 'u_group_round_robin_member';


  grGroupMember.update();


}


View solution in original post

4 REPLIES 4

manikorada
ServiceNow Employee
ServiceNow Employee

Try this script:



var grGroupMember = new GlideRecord('sys_user_grmember');


grGroupMember.addEncodedQuery('group.active=true^group.u_assignment=true^group.parent=32698401d4e95d00e6e503ce769e5605^ORgroup.parent=2023a465294dd104e6e58c1e41116c8a');


grGroupMember.query();


gs.print(grGroupMember.getRowCount());


var grGroupRRMember;


while (grGroupMember.next()) {


  grGroupMember.sys_class_name = 'u_group_round_robin_member';


  grGroupMember.update();


}


Thank you Mani!   Once you said it, I had that "Duh" moment. 🙂


German Alvarez2
Tera Expert

Hi Robert,



Yo have to take into account that the extension of a table is like a join of the base table and an auxiliary table, so, every record that are in tha base table are also in the child table, (but not accessible) and vice versa but accessible in this case.


    --> p.e: This is why, if you change a field from parent's thinking that it will only affect the child, it really will affect the parent and the rest of its childs!



So, the best way to promote a register from parent to child table is to change the sys_class_name from parent's name to the new one.


      --> You only will need to care about automations on child's fields, and maybe some business rules that will only affect at the child, and its conditions relies on "changes(), changesTo(), changesFrom()" and things like that.


Thank you German!   Well clarified.