+ Start a Discussion
Ben MalesBen Males 

How do I alert selected picklist value

Hi All,

I'm trying to alert the User when a particualr value is selected within a picklist. 

In Object Manager I have the Standard Object, Case
Case Standard Object

And in the Case Standard Object I have the Case Reason with the field label and field name Case Reason and Reason respectively with the type Data Type picklist.

Within the picklist I have the Case Reason Picklist Values such as Accounts, ALL - Bank Holiday Routing etc.

On the Case Page Layout I have added a Visualforce Page and what I'm trying to do is to alert / popup a warning if the User selects a particular value - example Apex below
<apex:page standardController="Case">
    <apex:outputText id="myScript">
        <script>
            if(!Case.Reason == "ALL - Bank Holiday Routing") {
                window.alert("Alert");
                }
        </script>
    </apex:outputText>
    <apex:form >
        <apex:inputField value="{!Case.Reason}">
            <apex:actionSupport event="onselect" reRender="myScript"/>
        </apex:inputField>
    </apex:form>    
</apex:page>
However, currently nothing is displayed to the User so how can I display an alert when the User selects a value?

Any help greatly appreciated.
 
GulshanRajGulshanRaj
Hi Ben,

You do not need to call the server instead use the script (javascript) to handle at the client (browser) end.  Try this and it will help for sure:
<apex:page standardController="Case">
    <apex:outputText id="myScript">
        <script>
         function changeHandler(data){
                if(data.value == "Other") {
                    alert("Alert");
                    }
             }
        </script>
    </apex:outputText>
    <apex:form >
        <apex:inputField value="{!Case.Reason}" onchange="changeHandler(this);">
        </apex:inputField>
    </apex:form>    
</apex:page>


Thanks
Gulshan Raj (https://forceblazer.com/)

 
Ben MalesBen Males
Hi Gulshan,

Thank you for the Reply.

Unfortunately when selecting Other there is still no alert to the User. Is there an alternative suggestion to try?

Kind regards
GulshanRajGulshanRaj
Which browser you are using?
GulshanRajGulshanRaj
It seems your browser doesn't support javascript. Can you please check if you could show alert anyway?
Ben MalesBen Males
Hi Gulshan,

I'm using the latest version of Chrome (Version 97.0.4692.99 (Official Build) (64-bit)) which does support JavaScript as if I have a simple JS alert in the vf page it works.

Salesforce JavaScript Alert

Below is the Apex for this alert
 
<apex:page >
  <script type="text/javascript">
      {
          window.alert("Alert");
      }
  </script>
</apex:page>
This alert fires onLoad, but I want to alert onChange when the user selects a value like "Other" in your example code but the alert doesn't display using that example code.

Kind regards
GulshanRajGulshanRaj
Hi Ben,

I think you are missing something. When I use the same code it works without any issue in the same browser.
User-added image

Nevermind your browser supports javascript that is what I want to know.

Can you please try this code. Make sure the line I hightlight should be there.
 
<apex:page standardController="Case">
    <apex:outputText id="myScript">
        <script>
         function changeHandler(data){
             console.log(data.value);
                if(data.value == "Other") {
                    alert("Alert");
                    }
             }
        </script>
    </apex:outputText>
    <apex:form >
        <apex:inputField value="{!Case.Reason}" onchange="changeHandler(this);">
        </apex:inputField>
    </apex:form>    
</apex:page>

I want to check two things:
  • The change event is getting fire or not
  • What data value we are getting.

Thanks
Gulshan Raj (https://forceblazer.com/)

 
Ben MalesBen Males
Hi Gulshan,

Thank you for your continued feedback.

As you suspected the event is not getting fired as there is nothing in the console when changing the value(s).

Salesforce Chrome Developer Console View

Kind regards