+ Start a Discussion
Rodolfo CalvoRodolfo Calvo 

Show an JavaScript alert from code

Hello team, I have a simple code such as following: 
public void showQuery()
    {
        try
        {
            //try something
            If(Success)
            {
                //Show JavaScript success message
            }
            else
            {
                //Show JavaScript missing something
            }
        }
        catch(Exception e)
        {
            //Show JavaScript code of error
        }
    }

How can I show a this JavaScript from code?
Thanks a lot in advance
Best Answer chosen by Rodolfo Calvo
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi,

You can use java Script in Visual Force and Use render Attribute for Alert : Please refer below Code 

Add below in  Visual Force Code :
<apex:ouputPanel id="successMessgae"  rendered="{!showSuccessMessage}">
<script>
alert('You Success Message');
</script>
</apex:outputPanel>

<apex:ouputPanel id="errorMessgae"  rendered="{!showErrorMessage}">
<script>
alert('You ErrorMessage');
</script>
</apex:outputPanel>
Controller Code :- 

 
public boolean showSuccessMessage{get;set;}
public boolean showErrorMessage{get;set;}

public void showQuery()
    {
        try
        {
            //try something
            If(Success)
            {
                //Show JavaScript success message
               showSuccessMessage = true;
               showErrorMessage= false;
            }
            else
            {
                //Show JavaScript missing something
              showErrorMessage= true;
               showSuccessMessage = false;

            }
        }
        catch(Exception e)
        {
            //Show JavaScript code of error
        }
    }
 
Please let me know, if you have any doubt.

Hope this helps

--
Thanks,
Swayam