Update and insert records from one table to another table using business rule

Shalika
Tera Expert

I have 2 tables -- Chromebook table and computer table. When any user inserts or updates any record in chromebook table, the same should also get inserted or updated into computer table.

I am writing after BR on Chromebook table as follows -

(function executeRule(current, previous /*null when async*/ ) {

var gr = new GlideRecord('cmdb_ci_computer'); //your Computer table name
gr.addQuery('sys_id', current.u_serial_number); //you can query the records based unique fields.ex: incident number
gr.query();
if (gr.next()) {
//Update records
gr.name = current.u_name; //here updating the values on fields
gr.serial_number = current.u_serial_number;
gr.location = current.u_location;
gr.model_id = current.u_model;
gr.install_status = current.u_status;
gr.owned_by=current.u_user;
gr.short_description = current.u_notes;
gr.update();
} else {

//create new records
gr.initialize();
gr.name = current.u_name; //here updating the values on fields
gr.serial_number = current.u_serial_number;
gr.location = current.u_location;
gr.model_id = current.u_model;
gr.install_status = current.u_status;
gr.owned_by=current.u_user;
gr.short_description = current.u_notes;
gr.insert();

}
}

)(current, previous);

 

Here the record is getting inserted into computer table, but when any user tries to update the record it is not working

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Change this line for starters

gr.addQuery('sys_id', current.u_serial_number); //you can query the records based unique fields.ex: incident number

gr.addQuery('serial_number', current.u_serial_number); //you can query the records based unique fields.ex: incident number

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Change this line for starters

gr.addQuery('sys_id', current.u_serial_number); //you can query the records based unique fields.ex: incident number

gr.addQuery('serial_number', current.u_serial_number); //you can query the records based unique fields.ex: incident number

Shalika
Tera Expert

Hii,

The above BR does not work for inserting new records