How to fix update set

chanikya
Kilo Sage

Hi All,

Request. : Please let me know, can we fix this by running script ? is there any consensus if we fix them by script in servicenow.

chanikya_0-1755401812982.png

two update version records has current state, so to fix this I have used below script. it fixed successfully .

var ga = new GlideAggregate("sys_update_version");
ga.addEncodedQuery("sys_created_onONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()^state=current"); // Last 7 days and State = Current //
ga.groupBy('name');
ga.addAggregate('COUNT');
ga.query();
while (ga.next()) {
    var count = parseInt(ga.getAggregate('COUNT'));
    if (count > 1) {
        var name = ga.getValue('name');
        var gr = new GlideRecord("sys_update_version");
        gr.addQuery('state', 'current');
        gr.addQuery('name', name);
        gr.orderBy('sys_created_on');
        gr.setLimit(1);
        gr.query();
        if (gr.next()) {
            var sys_id = gr.getValue('sys_id');
            var recordname = gr.getValue('record_name');
            var updateType = gr.getDisplayValue('type');
            var currentState = gr.getDisplayValue('state');
            // ✅ Confirm sys_id matches a real record before updating
            var target = new GlideRecord("sys_update_version");
            if (target.get(sys_id)) {
                target.autoSysFields(false);
                target.setValue('state', 'previous'); // or your desired state
                target.update();
                gs.info(target.getDisplayValue('sys_created_on'));
                gs.info(
                    "✅ Updated Record — Sys ID: " + sys_id +
                    " | Name: " + name +
                    " | Record Name: " + recordname +
                    " | Type: " + updateType +
					" | Created on  : " + target.getDisplayValue('sys_created_on') +
                    " | Previous State: " + currentState +
                    " → New State: "+target.getValue('state')
                );
            } else {
                gs.warn("⚠️ Could not find record with sys_id: " + sys_id);
            }
        }
    }
}

 



Please let me know, can we fix this by running script ? is there any consensus if we fix them by script in servicenow.

10 REPLIES 10

@chanikya ,

 

Yes that approach would be good changing the state value.

 

I guess there will be no consequences doing soo as we just changing the state.

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain

Hi @Shashank_Jain ,

Yes, I'm just changing State value, not touching other area .

@chanikya , That's sounds good.

If you find the solution helpful , please mark it as accepted and close the thread.

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain

@chanikya , 

 

If the solution was helpful, could you please mark it as accepted and close the thread. That will be much appreciated.

 

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain

Chaitanya ILCR
Mega Patron

Hi @chanikya ,

simply run this is in the background script 

take the sysid of the sys update version record which you want to set to previous to run the script

ChaitanyaILCR_0-1755452845658.png

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya