- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2017 08:09 AM
I have two custom tables, table a and table b. Once a record is updated on table a I want to move the record to table b and delete it from table a. I should note that this is being run from a business rule.
My approach:
//first gather all of the data i am going to delete
var a = new GlideRecord('tableA');
a.addQuery('number',number); //find the record
a.query();
if (a.next()) {
name = a.getDisplayValue('name');
}
//this works i get the records i want
//now i am going to insert the collected data into table b
var b = new GlideRecord('tableB');
b.initialize();
b.setValue('name', name);
b.insert();
b.update();
//here is where it gets weird when i attempt to delete the old record on table a
var del = new GlideRecord('tableA');
del.addQuery('number',number);
del.query(); //if i leave this line as is the record will not be inserted into table b. however if i comment this line out the record will be inserted but the old record
//wont be deleted from table a.
del.next();
del.deleteRow();
del.update();
Can anyone show me what I am doing wrong?
Thank you in advance.
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2017 08:19 AM
I'm not sure why you are querying multiple times to delete the same record
here is the updated code..
var a = new GlideRecord('tableA');
a.addQuery('number',number); //find the record
a.query();
if (a.next()) {
var b = new GlideRecord('tableB');
b.initialize();
b.setValue('name', a.getDisplayValue('name'));
b.insert();
a.deleteRecord();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2017 09:00 AM
I just tested in my instance and it is working as expected
May i know what are you passing to number
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2017 09:05 AM
what Business Rule are you using, can we have async or after update business rule on table A with below and see if that helps.
(function executeRule(current, previous /*null when async*/) {
var b = new GlideRecord('tableB');
b.initialize();
b.name = current.name;
b.insert();
var a = new GlideRecord('tableA');
a.addQuery('number',current.number); //find the record
a.deleteRecord();
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2017 09:17 AM
this script should definitely work for you:
var a = new GlideRecord('tableA');
if (a.get('number',<your_number>)){ // make sure <your_number> is a valid value for the number field
var name = a.name.toString();
a.deleteRecord();
var b = new GlideRecord('tableB');
b.newRecord();
b.setValue('name', name);
b.insert();
}
}