We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Automating deactivation of unused Standard Change Templates after 6 months

akashyada1
Tera Contributor

Hi Team,

I have a requirement to automate the deactivation of unused Standard Change Templates and would like guidance on the best approach.

Requirement:
If the following conditions are met:

Template is Active = true
The Last Closed Proposal Closed Date is older than 6 months from the current date
The Last Change execution (states like Closed / Implement / Review) is also older than 6 months
Then the system should:

Automatically deactivate the template

5 REPLIES 5

Tanushree Maiti
Tera Patron

Hi @akashyada1 ,

 

I will recommend here to go with Scheduled Script Execution

     Create a New Scheduled Job: Go to System Scheduler > Scheduled Jobs > Scheduled Script Executions.

    Set it to run monthly (e.g., "On 1st of every month") to avoid constant overhead.

    

  Sample Code:

 

var sixMonthsAgo = new GlideDateTime();
sixMonthsAgo.addMonths(-6);

var stdTemplate = new GlideRecord('std_change_record_producer');
stdTemplate.addActiveQuery();
stdTemplate.query();

while (stdTemplate.next()) {
var chgReq = new GlideRecord('change_request');
chgReq.addQuery('std_change_producer', stdTemplate.getUniqueValue());
chgReq.addQuery('type', 'standard');
chgReq.addQuery('state', 'IN', '3,4,7');
chgReq.addQuery('closed_at', '<', sixMonthsAgo);
chgReq.orderByDesc('closed_at');
chgReq.setLimit(1 );
chgReq.query();

if (!chgReq.hasNext()) {
gs.info('Deactivating Required Standard Change Template: ' + stdTemplate.name);
stdTemplate.active = false;
stdTemplate.update();
}
}

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti