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

I just tested in my instance and it is working as expected



May i know what are you passing to number


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


andymcdonald
Kilo Guru

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


    }    


}