You are currently viewing ServiceNow UI Action To Add Record In Update Set

ServiceNow UI Action To Add Record In Update Set

Most of us know what an “Update Set” does in ServiceNow. An update set is a group of configuration changes that can be moved from one instance to another. This feature allows administrators to group a series of changes into a named set and then move them as a unit to other systems for testing or deployment.

However, out-of-the-box settings do not allow all types of data to be captured, such as a data record (like an incident record, a problem record, etc.), a schedule job, and many more. In this article, I have provided the code for a UI action which you can use to add any record to the current update set

Follow the below steps to accomplish it-

  • Create a UI Action (a button or link) as per your choice and displayed it for admin only
  • Put the below code in the script section as it is (just change the function name if you want)
// UI Action Name - Add this in Update Set
// Table - Global (to display it on all tabless)
// Condition - gs.hasRole('admin')
Script -

addInUpdateSet();
function addInUpdateSet(){
    var url = GlideSession.get().getStack().bottom();
    current.setForceUpdate(true);
    current.update();
    var updateManager = new GlideUpdateManager2();
    var currTable = current.getTableName();
    if( currTable.startsWith('sys_ui_') || currTable == 'sys_choice' ||  current.getED().getAttribute('update_synch') == 'true' || current.getED().getAttribute('update_synch_custom') == 'true' || currTable.startsWith('wf_') || currTable == 'sys_attachment'){
        return;
    }
    else{
        updateManager.saveRecord(current);
    }
    if (current.hasAttachments()){
        addAttachments(current, currTable);
    }
   
    var currentSet = new GlideRecord('sys_update_set');
    currentSet.get(gs.getPreference('sys_update_set'));
    gs.flushMessages(); 
    gs.addInfoMessage('Record added in update set ' + currentSet.name );
    action.setRedirectURL(url);
}
function addAttachments(record, table){
    var attach = new GlideRecord("sys_attachment");
    attach.addQuery("table_name", table);
    attach.addQuery("table_sys_id", record.sys_id.toString());
    attach.query();
    while (attach.next()) {
        var updateManager = new GlideUpdateManager2();
        updateManager.saveRecord(attach);
        var attach_doc = new GlideRecord("sys_attachment_doc");
        attach_doc.addQuery("sys_attachment", attach.sys_id.toString());
        attach_doc.orderBy("position");
        attach_doc.query();
        while (attach_doc.next()) {
            updateManager.saveRecord(attach_doc);
        }
    }
}
Sample Incident Form With Related Link
Update Set

This comprehensive solution helped me in capturing anything I want and move it to the QA instance. Please make full use of this amazing code and save your time & energy.

end footer

This Post Has 2 Comments

  1. Aakanksha

    Hello,
    I appreciate your content and knowledge in ServiceNow. I tried this UI Action and it worked flawlessly. Just a request, will you please explain this code in any of the videos?

    1. Spectacular

      Hello Aakanksha,

      I will make a video to discuss the code soon. Thank you for your kind words.

Leave a Reply