Execute JavaScript on Salesforce Standard Page?

Question:
Is there a way to execute JavaScript on a Salesforce standard detail page? Specifically, I need to remove the “New Note” button from the Notes & Attachments related list while retaining the ability to add Attachments. Additionally, I want to rename the related list to “Attachments” for better clarity. Since standard declarative features like Page Layouts do not allow removing the button, I believe this can be achieved using JavaScript to manipulate the DOM. How can this be done effectively?
Answer:
You can execute JavaScript on a Salesforce standard detail page by using a custom button or link with embedded JavaScript.
Master Salesforce with expert-led Salesforce training in Mumbai—join our demo session in Mumbai, India!
Here’s a solution:
First, define the JavaScript code you want to run. For example, if you need to adjust elements on the page or remove buttons, you can modify the DOM like this:
(function() {
var notesButton = document.querySelector('input[value="New Note"]');
if (notesButton) {
notesButton.style.display = 'none'; // Hides the "New Note" button
}
var relatedListHeader = document.querySelector('h3:contains("Notes & Attachments")');
if (relatedListHeader) {
relatedListHeader.textContent = "Attachments"; // Renames the related list
}
})();
Explanation:
The script modifies a Salesforce standard detail page by hiding the “New Note” button and renaming the “Notes & Attachments” related list to “Attachments.” It uses JavaScript to locate these elements in the DOM and applies changes dynamically if the elements are present.
To embed this code into the Salesforce page, you can Base64-encode the script and add it to a custom button or link. Salesforce allows JavaScript execution through custom buttons, and the code will run when the button is clicked.
Here’s how you can add cleanup
Use a setTimeout
function to ensure your changes take effect after the page has fully loaded:
setTimeout(function() {
var relatedList = document.getElementsByClassName('customLinks');
if (!relatedList.length) {
return;
}
var anchorTags = relatedList[0].getElementsByTagName('a');
for (var i = 0; i < anchorTags.length; i++) {
if (anchorTags[i].innerText === "System Functions") {
anchorTags[i].parentNode.removeChild(anchorTags[i]); // Removes the custom button or link
}
}
}, 5000);
Explanation:
This JavaScript code waits for 5 seconds (using setTimeout
) and then searches for a link with the text “System Functions” in the document. If found, it removes the link from the page by targeting its parent node and removing the anchor element.
While this solution works for older Salesforce pages, consider that Salesforce Lightning and modern security practices discourage direct DOM manipulation. It’s advisable to use declarative tools or Lightning Web Components (LWCs) wherever possible.
Accelerate Your Career with Salesforce Training in Mumbai
Are you ready to elevate your career in Mumbai’s thriving Salesforce ecosystem? At CRS Info Solutions, we offer Salesforce online training that equips you with the skills to stand out. Our comprehensive program, led by experienced industry professionals, covers essential areas like Salesforce Administration, Development, and cutting-edge AI modules. With a focus on hands-on learning and real-time projects, you’ll gain practical expertise to tackle real-world challenges confidently.
Whether you’re embarking on your Salesforce journey or aiming to enhance your skill set, our tailored training programs are designed to meet your unique needs.  Salesforce training in Mumbai From mastering certification requirements to acing job interviews, we provide personalized guidance, in-depth course materials, and expert strategies for success.
Join our free demo session today and take the first step toward a prosperous Salesforce career in Mumbai!!!