GlideRecord not inserting if I add query()

123456789456123
Kilo Contributor

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.

1 ACCEPTED SOLUTION

dvp
Mega Sage
Mega Sage

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


}


View solution in original post

7 REPLIES 7

Ulrich Jugl
ServiceNow Employee
ServiceNow Employee

GlideRecord - Scoped   - check to use deleteRecord() instead of deleteRow()


Apologies I am using deleteRecord() not deleteRow()


dvp
Mega Sage
Mega Sage

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


}


I tried this and the record on table a doesn't get deleted and the record never gets inserted on table b.