Move Deletions to Default Update Set in ServiceNow

Utility Tool: Move Deletions to Default Update Set

Client: We would need these 5 fields on the form. Auto populate them from so and so table.
You: Sure, would do that.
After some time…
Client: Oh you know what, we would not need those fields. Could you please revert that?
You: Sure!

We all would have faced the above scenarios multiple times in our ServiceNow career. How do you handle that?

1. Do you capture the deletions in your current set & then move them to default?
OR
2. Let the deletion remain in the current set?

Well either of the above method requires some sort of efforts either by you or by your instance.
First one, needs you to manually move each customer updates to the default set. Even if you do via list view it would still be a bit of hassle.
Second, well your updates will first be inserted and then deleted while committing.

And not to forget, you also have application scopes to take care of.
Considering all these situations & also the fact that I am a bit lazy, thought of creating a basic utility tool. Which would reduce repetitive steps while keeping the process efficient. Here’s the code & explanation for it.

We start with creating a UI Action with following specifications:

And add the below code in the script section.

 

function moveToDefaultSet() {
    var sysId = g_form.getUniqueValue(); //current update set sysid
    var ajax = new GlideAjax("UpdateSetUtils");//Calling a Script Include 
    ajax.addParam("sysparm_name", "moveToDefault");
    ajax.addParam("sysparm_Idval", sysId);
    ajax.addParam("sysparm_appScope", g_form.getValue('application'));//sysid of the application the current update set is
    ajax.getXML(calBck);
    function calBck(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert(answer);
        location.reload();//refreshes the update set page
    }
}

 

We would also need to create a client callable script include with code below:

 

var UpdateSetUtils = Class.create();
UpdateSetUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    moveToDefault: function() {
        var getDefSet = new GlideRecord('sys_update_set');
        getDefSet.addQuery('application=' + this.getParameter('sysparm_appScope') + '^is_default=true^state=in progress');//at any point in time only 1 inprogress default update set would be present per application
        getDefSet.query();
        if (getDefSet.next()) {
            var defSetSysId = getDefSet.getUniqueValue(); //get default update set of the scope the update set is present
        }
        var getDeletions = new GlideRecord('sys_update_xml');
        getDeletions.addQuery('update_set=' + this.getParameter('sysparm_Idval') + '^action=DELETE'); //get the deleted updates that need to be migrated
        getDeletions.setValue('update_set', defSetSysId); //default update set
        getDeletions.updateMultiple();
        return 'Data Migrated!';
    },
 
    type: 'UpdateSetUtils'
});

 

And that’s it you are all set!

Now let’s see the code in action.

Here we can see that under customer updates we have one record which we have deleted and don’t want to keep it in our current update set. As we are all set for moving this custom update set from our actual update set to default update set. Below screen shows how you can do so. You just need to click related link “Move Deletion to Default Update Set”.

Below screen show that we are done with changes, message also popes up stating data migrated successfully.

After refreshing the page you can verify that now no result are present under Customer updates related list. it means we have successfully moved the deleted changes to default update set.

Now, we will verify the same whether it correctly moved to default update set or not. Below screen shows you that it moved correctly.

 

<strong>Join Runjay Patel</strong>

Thanks for reading this article, i hope you liked it, if that so, do like and subscribe my YouTube channel. You can also join our telegram channel to clear your technical doubt.

Get Latest Update From Runjay Patel

We don’t spam! Read our privacy policy for more info.

LEAVE A REPLY

Please enter your comment!
Please enter your name here