Pages

Wednesday, October 6, 2010

Disable the Links in a Lookup Field

[UPDATE: Thanks to Mad Computerist for his recommendations for script additions to remove hyperlink formatting!]

Sometimes simply making a Lookup field “read only” isn’t enough.  Sometimes, you may want to restrict the ability to even follow the links held inside the field.  Though it may be trivial to work around such a limitation, sometimes a dead-bolt keeps people out of your house by virtue of being a dead-bolt.  That said, here’s a handy function I whipped up to meet such a need:

function DisableLookupLinks(lookupFieldName) {
 var lookupParentNode = document.getElementById(lookupFieldName + "_d");
 var lookupSpanNodes = lookupParentNode.getElementsByTagName("SPAN");

 for (var spanIndex = 0; spanIndex < lookupSpanNodes.length; spanIndex ++) {
  var currentSpan = lookupSpanNodes[spanIndex];

  // Hide the hyperlink formatting
  currentSpan.style.textDecoration = "none";
  currentSpan.style.color = "#000000";

  // Revoke click functionality
  currentSpan.onclick = function() {};
 }
}

To disable a field's Lookup links, pass the field's schema name into the function.  For example, for the "Customer" field on a Case:

DisableLookupLinks("customerid");

The code also conveniently works for multi-record Lookup fields, such as party-lists on activity records.