Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to query and update multiple tables in one script

Happy S
Tera Expert

I am trying to update tickets under the tables - sc_task, sc_request and sc_req_item using the script below but apparently only tickets in the 1st table - sc_task got updated while for the other 2 tables, the value is not updated at all..

Mind to let me know why is it not working?

var gr = new GlideRecord('sc_task');
gr.addQuery('contact_type', '');
gr.query();

while (gr.next()) {
gr.setValue('contact_type', 'Self-service');
gr.autoSysFields(false);
gr.setWorkflow(false);
gr.update();


var Request = new GlideRecord('sc_request');
gr.addQuery('contact_type', '');
gr.query();

while (gr.next()) {
gr.setValue('contact_type', 'Self-service');
gr.autoSysFields(false);
gr.setWorkflow(false);
gr.update();


var Req = new GlideRecord('sc_req_item');
gr.addQuery('contact_type', '');
gr.query();

while (gr.next()) {
gr.setValue('contact_type', 'Self-service');
gr.autoSysFields(false);
gr.setWorkflow(false);
gr.update();
}
}
}

2 ACCEPTED SOLUTIONS

Charles Louis1
Giga Expert

Hi 

Plz try this just some code changes

var gr = new GlideRecord('sc_task');
gr.addQuery('contact_type', '');
gr.query();

while (gr.next()) {
gr.setValue('contact_type', 'Self-service');
gr.autoSysFields(false);
gr.setWorkflow(false);
gr.update();


var req = new GlideRecord('sc_request');
req.addQuery('contact_type', '');
req.query();

while (req.next()) {
req.setValue('contact_type', 'Self-service');
req.autoSysFields(false);
req.setWorkflow(false);
req.update();


var req_item = new GlideRecord('sc_req_item');
req_item.addQuery('contact_type', '');
req_item.query();

while (req_item.next()) {
req_item.setValue('contact_type', 'Self-service');
req_item.autoSysFields(false);
req_item.setWorkflow(false);
req_item.update();
}
}
}

 

Please Mark it correct if its helpful.

View solution in original post

Community Alums
Not applicable

Hi There,

You are updating 3 tables but i dont see any dependency in your code, if there is no dependency (i am predicting to see your code) please try below 

Var tab=['sc_task','sc_request','sc_req_item'];

for (var i = 0; i < tab.length; i++) {

var gr = new GlideRecord(tab[i]);
gr.addQuery('contact_type', '');
gr.query();
gr.setValue('contact_type', 'Self-service');
gr.autoSysFields(false);
gr.setWorkflow(false);
gr.updateMultiple();

}

 

Hope this will help you.

 

View solution in original post

2 REPLIES 2

Charles Louis1
Giga Expert

Hi 

Plz try this just some code changes

var gr = new GlideRecord('sc_task');
gr.addQuery('contact_type', '');
gr.query();

while (gr.next()) {
gr.setValue('contact_type', 'Self-service');
gr.autoSysFields(false);
gr.setWorkflow(false);
gr.update();


var req = new GlideRecord('sc_request');
req.addQuery('contact_type', '');
req.query();

while (req.next()) {
req.setValue('contact_type', 'Self-service');
req.autoSysFields(false);
req.setWorkflow(false);
req.update();


var req_item = new GlideRecord('sc_req_item');
req_item.addQuery('contact_type', '');
req_item.query();

while (req_item.next()) {
req_item.setValue('contact_type', 'Self-service');
req_item.autoSysFields(false);
req_item.setWorkflow(false);
req_item.update();
}
}
}

 

Please Mark it correct if its helpful.

Community Alums
Not applicable

Hi There,

You are updating 3 tables but i dont see any dependency in your code, if there is no dependency (i am predicting to see your code) please try below 

Var tab=['sc_task','sc_request','sc_req_item'];

for (var i = 0; i < tab.length; i++) {

var gr = new GlideRecord(tab[i]);
gr.addQuery('contact_type', '');
gr.query();
gr.setValue('contact_type', 'Self-service');
gr.autoSysFields(false);
gr.setWorkflow(false);
gr.updateMultiple();

}

 

Hope this will help you.