function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kenn K.Kenn K. 

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, -Ken

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

You can write something like

 

if('{!Opportunity.AccountType}' == 'OLD'){
       alert('ERROR : Cannot be Redirected');
}
else{
window.location.href = '/YOUR_PAGE_URL';
}

 NOTE :Make sure the field api name and value are correct

All Answers

TheDoctorTheDoctor

Do you need JavaScript for this? Could you just use an IF function on the button formula and redirect them to an error page? Something like this:

 

IF( Opportunity.Account.Type = "OLD",

        <INSERT URL FOR ERROR PAGE>,

        <INSERT REDIRECT URL>

)

Kenn K.Kenn K.

Do you have a script example? that would be very helpful..

Avidev9Avidev9

You can write something like

 

if('{!Opportunity.AccountType}' == 'OLD'){
       alert('ERROR : Cannot be Redirected');
}
else{
window.location.href = '/YOUR_PAGE_URL';
}

 NOTE :Make sure the field api name and value are correct

This was selected as the best answer
Kenn K.Kenn K.

Thanks a lot. Might you know the proper syntax in the situation where I have a picklist instead of a text field? Not sure how the ISPICKVAL arguments should be utilized.

 

Kenn K.Kenn K.
Never mind. Worked perfectly.