- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Included in the Developer Toolbox Update Set available on Share (link to Share in the post).
I really like using a Clone Cleanup Script to reset certain things after a clone. Might as well automate as many things as possible.
Here's the base script I start off with and add to it as needed:
/*
Written by Jim Coyne
https://community.servicenow.com/community?id=community_user_profile&user=8352da29dbd81fc09c9ffb651f9619c7
https://community.servicenow.com/community?id=community_blog&sys_id=23ec41f6db81ab40fece0b55ca961998
Custom script to perform the following:
- deactivate Scheduled Email Reports
- deactivate Discovery Schedules (if configured)
- deactivate any Audit jobs
- deactivate custom Scheduled Jobs with a name starting with "Custom"
- set the repeat interval for the email send and receive jobs to 30 seconds
- clear the email outbox
- enable email sending and receiving
- update the clone target property so the instance can be a clone target (gets overridden during the clone)
- set the "glide.product.description" property based on the instance suffix and current date to display in the banner frame
*/
(function() {
//to enable, remove or comment out the next line
return; //*****update required
//put here to avoid accidentally running the script
//figure out the sub-production suffix by removing the production instance name
//e.g. if the production name is "acme" and the dev instance is "acmedev", the instance variable value will be "DEV"
//used below when setting the banner frame title
//************************* replace "ProductionInstanceName" on the next line with the real value *************************
var instance = gs.getProperty("instance_name").replace("ProductionInstanceName", "").toUpperCase(); //*****update required
//deactivate the Scheduled Email Reports
//no point sending reports from sub-production instances
var gr = new GlideRecord("sysauto_report");
gr.addEncodedQuery("active=true");
gr.query();
while (gr.next()) {
gr.active = false;
gr.update();
}
/* remove the comment block if you are using Discovery and want to disable in sub-production instances
//deactivate Discovery Schedules
gr = new GlideRecord("discovery_schedule");
gr.addEncodedQuery("active=true");
gr.query();
while (gr.next()) {
gr.active = false;
gr.update();
}
*/
//deactivate any Audit jobs
gr = new GlideRecord("cert_audit");
gr.addEncodedQuery("active=true^run_typeNOT INonce,on_demand");
gr.query();
while (gr.next()) {
gr.active = false;
gr.update();
}
//deactivate custom Scheduled Jobs with a name starting with "Custom"
//use the commented out query if you want to leave LDAP imports active
gr = new GlideRecord("sysauto");
//gr.addEncodedQuery("nameSTARTSWITHCustom^nameNOT LIKELDAP^active=true");
gr.addEncodedQuery("nameSTARTSWITHCustom^active=true");
gr.query();
while (gr.next()) {
gr.active = false;
gr.update();
}
//reset the mail send/receive intervals to 30 seconds
//handy for testing purposes, otherwise you are waiting 2 - 3 minutes for delivery/processing
gr = new GlideRecord("sys_trigger");
gr.addEncodedQuery("nameINEmail Reader,POP Reader,SMTP Sender");
gr.query();
while (gr.next()) {
gr.repeat = "00:00:30";
gr.update();
}
//clear the email outbox
//no need to send any pending emails that were in the production instance from the sub-production one
gr = new GlideRecord("sys_email");
gr.addEncodedQuery("mailbox=outbox");
gr.deleteMultiple();
//enable email sending and receiving
//re-enable as cloning will disable it
gr = new GlideRecord("sys_properties");
gr.addEncodedQuery("name=glide.email.read.active^ORname=glide.email.smtp.active");
gr.query();
while (gr.next()) {
gr.value = "true";
gr.update();
}
//was an issue in the past but no longer???
//update the clone target property so the instance can be a clone target (gets overridden during the clone)
gs.setProperty("glide.db.clone.allow_clone_target", true);
//set the "glide.product.description" property based on the instance suffix and current date to display in the banner frame
//get the current date/time then convert to the instance timezone
var timeZone = gs.getProperty("glide.sys.default.tz", "US/Pacific");
var gdt = GlideDateTime();
var tz = Packages.java.util.TimeZone.getTimeZone(timeZone);
gdt.setTZ(tz);
gs.setProperty("glide.product.description", "*** " + instance + " - Cloned on " + gdt.getDisplayValue().replace(" ", " @ ") + " " + timeZone + " ***");
})();
The script will deactivate scheduled reports, Discovery jobs as well as Audit jobs in sub-production instances because you probably don't want to be running them. Same thing for Scheduled Jobs with a name starting with "Custom".
Setting the email jobs to run every 30 seconds helps cut down with wait times while testing. Clearing the outbox is also a good idea in case there were some emails in production that got cloned over to sub-prod instances. And then it re-enables email sending and receiving as a clone will disable it.
It will also ensure the "glide.db.clone.allow_clone_target" System Property is set to "true" so that the sub-prod instances remain valid clone targets. The value used to be set to "false" in the past, so the instance could not be clone over again. This just makes sure it remains set to "true".
The last thing it does is change the "glide.product.description" System Property to contain the sub-production instance suffix and date/time it was last cloned on, so it is shown in the banner:
In order for it to figure out the suffix of the instance, you have to replace the "ProductionInstanceName" string on or about line 27 with the name of your production instance. Search for the string "*****update required" to see which lines need some updating.
NOTE: The "return;" string on or about line 20 has to be removed or commented out. It was put there to avoid accidentally running the script.
I've attached an XML file so you can just import it into your instance. As always, try it out in your company's development instance first, or better yet, your own personal development instance.
NOTE: If you import the XML file, you will have to activate the record as the XML contains "active=false" to avoid the script being run by accident. Make sure you review the script and then activate it when ready. You can always run int in Xplore in a sub-production instance or PDI to test it out first.
Please share any suggestions or things you've included in your own scripts.
- 5,425 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.