- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 05-21-2021 08:52 AM
As an admin, sometimes you might need to clear a field that is set as readonly in the UI. And for fields like Date Date/Time you can't just go to the list view and clear them like text fields. I wanted an alternative to having to making a GlideRecord script just to clear the field. So I decided to add a context menu option to do this (for admins only).
The field context menu is all in an OOTB UI Macro (element_context) that I didn't want to customize and risk missing future updates. But I am leveraging GwtContextMenu() that is instantiated in that UI Macro. This is not future proof because it listens for a DOM element with a certain class to appear which happens when you right click on a label. It then tacks on the additional element for the Clear Field option.
This is a Global UI Script but is limited to admins and only runs on forms. There is room for improvements so feel free to contribute. Enjoy!
jQuery(document).ready(function() {
//only do this for admins
if (g_user.hasRole('admin')) {
//only do this on forms
if (typeof g_form != 'undefined') {
//listen for field context menu and add option to clear field
onElementInserted('body', '.context-menu-minwidth', function(element) {
var menu = jQuery('.context-menu-minwidth');
//only do this if the context menu is for a field
if(menu.next().text()=='Configure Label'){
//there is probably a better way to find the field name
var field = menu.siblings().last().text().replace('Watch - \'','').replace('\'','');
gcm.addLine();
//gcm.addHref([label],[javascript function])
gcm.addHref("Clear Field", "g_form.setValue('"+field+"', '');");
}
});
}
}
});
function onElementInserted(containerSelector, elementSelector, callback) {
var onMutationsObserved = function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) {
var elements = $j(mutation.addedNodes).find(elementSelector);
for (var i = 0, len = elements.length; i < len; i++) {
callback(elements[i]);
}
}
});
};
var target = $j(containerSelector)[0];
var config = {
childList: true,
subtree: true
};
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(onMutationsObserved);
observer.observe(target, config);
}