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

Shashank_Jain
Kilo Sage

@chanikya ,

 

How two update set on  a particular script has two current version?

 

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

First person work on script  on 1st-Aug
there were many versions on script , so First person deleted that some specific unnecessary  versions ,there were ran on script for that . While deleting they picked wrong condition it deleted all version .

2nd Person worked on same script on 2nd-Aug. 

So we rollbacked from delete recovery successfully on 2nd-Aug .
now all versions came back successfully with Zero Errors.  but two versions showing current(First person, 2nd person currently working ) .

 

@chanikya ,

 

There is one more way, you can delete these both versions and then create one new update set and capture this script in that version only with updated script.

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

Hi @Shashank_Jain ,

even though to delete those two we need to design Back-groundscript code on table.

sys_update_version

 

How about rather than deleting two records, If we simply set that single old record state value to "previous"  ?