- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2019 05:35 PM
Can some one help me with fix script ,
I need to close all the active incidents whos group contains "3M Software" whats wrong in this fix script
var inc = new GlideRecord('incident');
inc.addQuery('active',true);
inc.addQuery("assignment_group",'3M Software');
inc.query();
if(inc.next()){
inc.state="7";
inc.active=false;
inc.close_code=6;
inc.close_notes="This incident has been closed" ;
inc.update();
inc.setWorkflow(false);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2019 10:29 PM
Hi Kam,
the query for assignment_group is incorrect; the assignment_group is a reference field and hence it expects a sys_id
you should either give sys_id of "3M Software" group from sys_user_group table or dot walk to query
either use below code
inc.addQuery("assignment_group.name",'3M Software');
//OR
inc.addQuery("assignment_group",'groupSysId');
also the code would run in optimized way if you give sys_id of that group because if you give query with assignment_group.name it has to go one level deep to get the name from the reference field
Few recommendations:
1) Also use try catch block to handle exceptions whenever you run bulk update etc from fix script
2) Always run the script first for few records like 10 or 20; then verify everything looks good then run it for bulk
3) Always wrap your code in function
4) Always give some info message so that in logs you can check fix script started and fix script ended; this will help you debug whether any error occurred or not
updated code as below
updateBulkRecords();
function updateBulkRecords(){
try{
gs.info('Fix Script started for updateBulkRecords');
var inc = new GlideRecord('incident');
inc.addQuery('active',true);
inc.addQuery("assignment_group.name",'3M Software');
//inc.addQuery("assignment_group",'groupSysId');
inc.setLimit(10); // remove this line once you verify for 10 records
inc.query();
if(inc.next()){
inc.state = 7; // check state value; I think it should be integer value
inc.active = false;
inc.close_code = 6;
inc.close_notes = "This incident has been closed" ;
inc.setWorkflow(false); // setWorkflow has to be used before update
inc.update();
}
gs.info('Fix Script Ended for updateBulkRecords');
}
catch(ex){
gs.info('Exception is: ' + ex);
}
}
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2019 05:46 PM
there are 10k incidents , before updating all , is there any way we can see how many records are about to update?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2019 06:04 PM
Try the following:
function getIncidentsAssignedTo3MSoftware() {
var inc = new GlideRecord('incident');
inc.addQuery('active',true);
inc.addQuery("assignment_group.name",'3M Software');
inc.query();
return inc;
}
function getIncidentsNumbersAssignedTo3MSoftware() {
var inc = getIncidentsAssignedTo3MSoftware();
var numbers = [];
while(inc.next() ) {
numbers.push(inc.getValue('number'));
}
return numbers;
}
function closeIncidentsAssignedTo3MSoftware() {
var inc = getIncidentsAssignedTo3MSoftware();
while(inc.next() ) {
closeIncident(inc);
}
}
function closeIncident(inc) {
inc.state="7";
inc.active=false;
inc.close_code=6;
inc.close_notes="This incident has been closed" ;
inc.setWorkflow(false);
inc.update();
}
Example:
var incidentNumbers = getIncidentsNumbersAssignedTo3MSoftware();
gs.info("Number of Incidents: " + incidentNumbers.length);
gs.info("Incident Numbers: " + incidentNumbers.join("\r\n");
// Uncomment when you are happy
// closeIncidentsAssignedTo3MSoftware()
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2019 10:00 PM
Hi KAm,
Please use
inc.getRowCount() to print number of records which are in outcome of your query.
Thanks
Ankush
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2019 10:21 PM
Hello Kam,
You have written a perfect script. you just need to passs sysID of assignment group:
var inc = new GlideRecord('incident');
inc.addQuery('active',true);
inc.addQuery("assignment_group",'sys ID Of3M Software');
//OR inc.addQuery("assignment_group.name",'3M Software');
inc.query();
while(inc.next()){
inc.state="7";
inc.active=false;
inc.close_code=6;
inc.close_notes="This incident has been closed" ;
inc.setWorkflow(false);
inc.update();
}
Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2019 10:29 PM
Hi Kam,
the query for assignment_group is incorrect; the assignment_group is a reference field and hence it expects a sys_id
you should either give sys_id of "3M Software" group from sys_user_group table or dot walk to query
either use below code
inc.addQuery("assignment_group.name",'3M Software');
//OR
inc.addQuery("assignment_group",'groupSysId');
also the code would run in optimized way if you give sys_id of that group because if you give query with assignment_group.name it has to go one level deep to get the name from the reference field
Few recommendations:
1) Also use try catch block to handle exceptions whenever you run bulk update etc from fix script
2) Always run the script first for few records like 10 or 20; then verify everything looks good then run it for bulk
3) Always wrap your code in function
4) Always give some info message so that in logs you can check fix script started and fix script ended; this will help you debug whether any error occurred or not
updated code as below
updateBulkRecords();
function updateBulkRecords(){
try{
gs.info('Fix Script started for updateBulkRecords');
var inc = new GlideRecord('incident');
inc.addQuery('active',true);
inc.addQuery("assignment_group.name",'3M Software');
//inc.addQuery("assignment_group",'groupSysId');
inc.setLimit(10); // remove this line once you verify for 10 records
inc.query();
if(inc.next()){
inc.state = 7; // check state value; I think it should be integer value
inc.active = false;
inc.close_code = 6;
inc.close_notes = "This incident has been closed" ;
inc.setWorkflow(false); // setWorkflow has to be used before update
inc.update();
}
gs.info('Fix Script Ended for updateBulkRecords');
}
catch(ex){
gs.info('Exception is: ' + ex);
}
}
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader