logo

NJP

Do you need a git Blame! feature for your sys_update_version?

Import · Oct 23, 2020 · article

I some of your changes are not scoped or you need to find creator of some old old you will step on the problem: Created By field is actually showing the user who applied the update set, not one who modified the script.

My suggestion is no add a calculated Blame! field to your sys_update_version and sys_update_xml tables.

The code it pretty simple:

(function calculatedFieldValue(current) {

    return findLastUpdater(current);

})(current);

//current - sys_update_version or remote update
function findLastUpdater (current){

var xmlString = current.getValue('payload');

var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
var userName = xmlDoc.getNodeText('//sys_updated_by');

if(!userName) return ""; //reasonable on upgrade and some  edge cases

//modify if you would like to use String, email, etc instead of reference
var gr = new GlideRecord('sys_user');
gr.get('user_name', userName );

return gr.getUniqueValue();
};

See result at screenshot - script was always changed by same person, even Created by show 3 different users

View original source

https://www.servicenow.com/community/developer-articles/do-you-need-a-git-blame-feature-for-your-sys-update-version/ta-p/2320562