0

I am facing problem to display error using .. please see following code

Controller

public with sharing class TestingPage {

    public TestingPage()
    {
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'This is testing message'));
        // this code is working if on visual force page showheader="true"
    }

    @RemoteAction
    public static string message()
    {   
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'This is testing message'));
        return 'dilip';
        //this method has called using javascript but message not working..
    }
}

VisualForce page

<apex:page docType="html-5.0" sidebar="false" standardStylesheets="false" showheader="false" controller="TestingPage" cache="true">
 <apex:pageMessages />
  <apex:includeScript value="{!URLFOR($Resource.libraryjs, 'js/jquery-2.0.3.min.js')}" />
  <script>
  function test()
  {
      debugger;
      Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.TestingPage.message}',
        function(result, event) {
            if (event.status) { 

               alert(result);

            } else if (event.type === 'exception') {
                 ErrorAlert('Problem loading page');            
            } else 
            {
                 ErrorAlert('Problem loading page');    
            }

        }, {
            escape: true
        }

    );

}
 $(document).ready(function() {

  test();

  </script>
</apex:page>
2
  • you may need to rerender your pagemessage
    – Ratan Paul
    Jan 7 '16 at 5:53
  • is this correct.. <apex:pageMessages rendered="true" />... it is not working for @RemoteAction method
    – Dilip
    Jan 7 '16 at 5:57
2

Wrapper inside apex:form and pageBlock then it will work

What I believe you may need to use any child component inside apex:form tag

In my test I found. If showheader = false apex:pagemessage should inside the apex:pageblock or any visualforce standard component should be added in that page (ex action function)

(Feel free to edit my post If I am wrong)

<apex:page  sidebar="false" standardStylesheets="true" showheader="false" controller="TestingPage" cache="true">
   <apex:form >
      <apex:pageBlock>
         <apex:pageMessages />
         <script>
            function test()
            {
                debugger;
                Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.TestingPage.message}',
                  function(result, event) {
                      if (event.status) { 

                         alert(result);

                      } else if (event.type === 'exception') {
                           ErrorAlert('Problem loading page');            
                      } else 
                      {
                           ErrorAlert('Problem loading page');    
                      }

                  }, {
                      escape: true
                  }

              );

            }
            test();
         </script>
      </apex:pageBlock>
   </apex:form>
</apex:page>

Class

public with sharing class TestingPage {

    public TestingPage()
    {
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'This is testing message'));
    }

    @RemoteAction
    public static string message()
    {   
        // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'testing remote action '));
        return 'dilip';
    //this method has called using javascript but message not working..
    }

}

I think you can't add page message from JS remoting

0
0

As Ratan already mentioned <apex:pageMessages /> will not work for JS remoting, you can try this alternate method to show error/success messages.

What I have done here is created a wrapper class for actual result, together with error/success messages. So your controller will look like as below. You can get the wrapper object as JSON in your javascript. Parse the JSON and get the required message and your result as below. Hope this helps. VF Page

<apex:page docType="html-5.0" sidebar="false" standardStylesheets="false" showheader="false" controller="VFRemoteTestingPage" cache="true">
 <apex:pageMessages />
  <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" />
  <script>
        function test() {
            //debugger;
            Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.VFRemoteTestingPage.message}',
                function(result, event) {
                    if (event.status) {
                        console.log(result.replace(/(&quot\;)/g,"\""));
                        // your message is : resultObject.successMessage resultObject.errorMessage
                        var resultObject = JSON.parse(result.replace(/(&quot\;)/g,"\""));
                        alert("Success Message : "+resultObject.successMessage);
                        alert("Failure Message : "+resultObject.errorMessage);                       
                    } else if (event.type === 'exception') {
                        ErrorAlert('Problem loading page');
                    } else {
                        ErrorAlert('Problem loading page');
                    }
                }, {
                    escape: true
                }
            );
        }
        $(document).ready(function() {
            test();
        });
  </script>
</apex:page>

Controller

public class VFRemoteTestingPage {
    public class CustomResult{
        public List <Account> accounts; // This is just for an example on how to use 
        public String successMessage;
        public String errorMessage;

        public CustomResult(){
            this.successMessage = '';
            this.errorMessage = '';
        }        
    }    
    public VFRemoteTestingPage(){
       //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'This is testing message'));
       // this code is working if on visual force page showheader="true"
    }

    @RemoteAction
    public static string message(){   
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'This is testing message'));
        CustomResult customResult = new CustomResult ();
        //customResult.contacts = [SELECT Id FROM Contact WHERE Name = 'Dilip']; // just an example
        customResult.successMessage = 'Here is the account info you are looking for.';
        customResult.errorMessage = 'There are no Account info available.';
        return JSON.serialize(customResult);
    }
}

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.