1

I'm new to Salesforce, so if this is trivial, accept my apologies in advance.

I have a button that points to an internal website. This button grabs looks at a custom field and uses that custom field as a passable parameter. The button is working as it should, but I'm needing more functionality.

What I'm looking to do is hide the button or display a popup message if my custom field is blank.

EDIT: So it looks like Javascript is what I'm looking for since we cannot hide the button.

This is what I have right now when I click a button. It opens up an internal report and passes the parameter that is in the KEY4__c custom field:

http://linktoreportserver/Parameter={!Lead.KEY4__c}

If the KEY4__c field is empty or null, I want it to send a popup to the user. If there is data, I want it to go to the URL above.

2 Answers 2

2

For your JavaScript, you can perform both steps to hide the button and/or show an alert.

First, the alert:

if({!ISBLANK(Object.Field)}) {
    alert("You must populate the Field before using this button");
} else {
    window.open("https://myserver/?param={!JSENCODE(Object.Field)}","_blank");
}

Now, we can add the following code to a Static Resource:

(function() {
    function disableButtons() {
         var btns = document.querySelectorAll('input[value="Button Label"]'), i = 0;
         if(window.$shouldDisable) {
             while(i<btns.length) btns[i++].disabled="disabled";
         }
    }
    window.addEventListener("load", disableButtons, true);
}

Finally, add the resource to your button:

{!RequireScript(UrlFor($Resource.disableButton, null, null)}
window.$shouldDisable = {!ISBLANK(Object.Field)};
if(window.$shouldDisable) {
    alert("You must populate the Field before using this button");
} else {
    window.open("https://myserver/?param={!JSENCODE(Object.Field)}","_blank");
}

Using RequireScript causes whatever is included in the script to run on page load.

1
  • The first item is exactly what I'm looking for. Thanks for clearing up my confusion with JS. I'm a DBA by trade and haven't touched any web development since college.
    – jdids
    Jan 2, 2015 at 21:57
1

Your custom button's properties must have Behavior set to Execute JavaScript, and Content Source to OnClick JavaScript.

Then write the JavaScript code that will display a popup based on your field's value. Here's a rough example:

window.alert("{!record.your_field__c}");

Unfortunately, you can't show/hide a custom button based on a given field's value without using record types and page layouts.

1
  • Thanks for your input. I edited my main question and added some additional information
    – jdids
    Jan 2, 2015 at 21:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .