[SalesForce] Javascript alert on button click

I want to prevent users from being redirected when they click a button and picklist value is false.

I have a custom button on Opportunity related list on account. If the value of Account type is 'OLD', users should get an error message and not redirect the page. If it's anything else, they can be redirected opportunity.

What is the best way of going about this?

Thanks,
-Kenn

Best Answer

In the custom button, do something like this:

if("{!JSENCODE(Entity.Field)}"==="OLD") {
    alert("You cannot use this button on this account.");
} else {
    window.top.location.href = "(url to go to)";
}

For a picklist, you can use ISPICKVAL:

if({!ISPICKVAL(Entity.Field,'OLD')}) {
    alert("Access denied.")
}

Make sure the type is set to "Execute JavaScript".

Related Topic