How to automate retirement of Change Templates after they got approved

Arjun Reddy Yer
Tera Guru

Can anyone help me @Vasantharajan N @Ankur Bawiskar 

 

As I need to auto retire the Change Templates after 1 year based on the approved date & time if there is modifications happen on the Change Templates.

Can any one help me with Schedule Job script and creation of it

4 REPLIES 4

Vasantharajan N
Giga Sage
Giga Sage

@Arjun Reddy Yer - Do you want to trigger the retire template process after one year of template creation or just set the template retired to true and make it inactive without triggering the workflow which is not recommended at all? 


Thanks & Regards,
Vasanth

trigger the retire template process after one year of template got approved and in between one year if got modified then based on modification approval date & time the retire date & time needs to auto change

@Arjun Reddy Yer - You can try with the below code snippet to initiate the Auto retire process for the change templates that is not retired and active. 

var stdChgTemGr = new GlideRecord("std_change_record_producer");
stdChgTemGr.addEncodedQuery("active=true^retired=false");
stdChgTemGr.setLimit(1);
stdChgTemGr.query();
while (stdChgTemGr.next()) {
    var gr = new GlideRecord("std_change_producer_version");
    gr.addEncodedQuery("std_change_producer=" + stdChgTemGr.getValue('sys_id').toString() + "^std_change_proposal.approval=approved^std_change_proposal.approval_setRELATIVELT@year@ago@1");
    gr.orderByDesc('sys_created_on');
    gr.setLimit(1);
    gr.query();
    while (gr.next()) {
        gs.info("Template " + stdChgTemGr.getValue('name') + ' approved on ' + gr.std_change_proposal.approval_set + ' will be retired using the proposal ' + intiateRetireProcess(stdChgTemGr.getValue('sys_id'), stdChgTemGr.getValue('name')));
    }

}

function intiateRetireProcess(template_sys_id, template_name) {
    var retireGr = new GlideRecord('std_change_proposal');
    retireGr.initialize();
    retireGr.std_change_producer = template_sys_id;
    retireGr.proposal_type = 3;
    retireGr.short_description = "Retiring template " + template_name;
    retireGr.business_justification = "Automated process retiring the template after one year for no change";
    var proposal = retireGr.insert();
    if (retireGr.getLastErrorMessage())
        gs.info("Error Message while initiating the retire proposal " + retireGr.getLastErrorMessage());

    return retireGr.number;
}

Thanks & Regards,
Vasanth

Instead of using 3 tables in the script can we use one table in the script.

For testing purpose if want to make it work for 3 mins auto retire process for the change templates can I know how to change the script and make it work @Vasantharajan N @Ankur Bawiskar