0

I need to display a message on vf page based on onchange of data from picklist. In my controller the method is boolean type....based on data selected from picklist which call my method...if the method return true a alert message "true" will be displayed else if it return false message will display "false". Please help with a sample code.

1

To display error messages in the Visualforce page add below tag where you want to display the error message.

Visualforce page:

<apex:pageMessages />

Apex Controller:

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.');
ApexPages.addMessage(myMsg); 

(Or)

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.'));
  • 1
    When your answer does not provide more specific information than answers already present on the site it's preferable to refer to such answers and duplicate questions. This means we only have to maintain correct information in a single location. – Samuel De Rycke Feb 25 '14 at 10:06
1

Another approach to show alert on visualforce page.

public class
{
 // value referred on visulforce to make decision
 public boolean returnValue {get; set;}

 // selectoption implementation
 public List<SelectOption> itemList {get; set;}
 public String selectedItem {get; set;}

 // Called from action function
 public void controllerMethod()
 {
    // implement any logic using selectedItem 
    returnValue = !returnValue;
    return returnValue;
 }
}

Alert message can be shown by using oncomplete attribute of actionfuntion by referencing boolean value directly in the javascript function ( showAtert(t)) called by this attribute.

Page

    <apex:page>
       <apex:form>
         <apex:selectList value="{!selectedItem}" onchange="callMethod()">
           <apex:selectOption value="{!itemList}"
         </apex:selectList>
       <apex:actionfunction name="callMethod" action="{!controllerMethod}" oncomplete="showAlert({!returnValue })"/>
      <apex:form>
  <script>
    function showAlert(t) {
      if(t) {
        alert('Correct');
      } else {
        alert('No value entered !');
      } 
    }
  </script>
</apex:page>
  • 2
    Adding an explanation rather than code dumping would improve the quality of your answer. – Samuel De Rycke Feb 25 '14 at 10:14
1

Visualforce page

<!-- Using java script in visualforce page -->
<apex:page controller="DynamicVisualforceController">
  <apex:form >
        <apex:pageBlock >
           Input Here  : <apex:inputText id="inptID"/>
                       <apex:commandButton onclick="MyjavaFunction('{!$Component.inptID}')" value=" Submit "/>
        </apex:pageBlock>
  </apex:form>

  <!-- Java script starts Here -->
  <script>
   function MyjavaFunction(ReceiveInputID){

    var inputValue = document.getElementById(ReceiveInputID).value;
         if(inputValue == ''){
            alert('You did not eneter any value in input box');
         } 
         else
            alert(' You entered :: '+inputValue); 
   }
  </script> 

</apex:page>

Here is one code which may be useful for u

Image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.