goswami_sudipta
Mega Expert
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 06-27-2018 04:17 AM
Hello,
I would like to share a simple yet powerful approach of customizing elements’ behavior on a form in ServiceNow.
- Learning objective – Override default onclick behavior in ServiceNow
- Requirement – On clicking of the knowledge article links in the related list of a CI form, would take you to the kb_view page instead of opening the KA record in the edit view
- Challenges – Simple DOM manipulation on onLoad of the form for those KA links in related list would not work if the related list is reloaded through clicking of the breadcrumb and also using the ‘Refresh List’ UI Action which re-sets the HTML elements in its original format (See Community_EventOverridingChallenges_SNOW.png)
- Approach taken – Instead of DOM manipulation, Event Overriding or Dynamic Event Binding is used to overcome the challenges which eventually prevents the DOM being re-set on onclick of breadcrumb or 'Refresh List’ UI Action. See attached code snippet - Community_EventOverridingCode_SNOW.png – which works in Chrome
- Points to remember – This is not a recommended approach by ServiceNow and should only be used in an ultimate need, carefully and sensitively
function onLoad() {
if (document.addEventListener) {
document.addEventListener("click", overrideOnClickEvt, true);
}
function overrideOnClickEvt(event) {
var v_sNewWindow;
var v_sNewTarget = "";
var v_sTarget = event.target.toString();
//alert('hello there ' + event.target);
if (v_sTarget.indexOf("kb_knowledge.do?sys_id=") != -1) {
v_sNewTarget = "kb_view.do?sys_kb_id=" + (v_sTarget.split("="))[1];
v_sNewWindow = window.open(v_sNewTarget);
//alert("I should go elsewhere " + v_sNewTarget);
event.preventDefault();
}
}
}