4

I would like to know if is possible to set and show a apex:pageMessages block from javascript

enter image description here

I've been trying to do something like this:

VF page:

...
<apex:pageMessages id="jserror"/>
...

<script>
 ...
 var msgEl = document.getElementById("{!$Component.jserror}");
 msgEL.style="display:block;"; //sample of showing
 // here I keep doing more cool stuff

However, I have two problems:

1 - The pageMessages block is not build on page If i don't call ApexPages.addmessage(new ApexPages.message(..... So I can't find the element and show it. Obviously the message couldn't be visible by default.

2 - It's not easy to manage the message setting since the html structure change for different devices rendering(Table for desktop, Div's for mobile)

Finally, There is some workarround to manage the standar SF message from javascript? something like: sforce.visualforce.showMessage(WARNING,'This would be really cool indied!');

8

Instead of trying to style and alter the pageMessages markup, I would just create an action function. You could do something like:

VF

<apex:actionFunction name="showMessage" action="{!showMessage}" rerender="jserror">
  <apex:param name="errorLevel" assignTo="{!errorLevel}" value="" />
  <apex:param name="messageName" assignTo="{!messageName}" value="" />
</apex:actionFunction>
...
<script>
  showMessage('WARNING', 'This would be really cool indeed!');
<script>

Apex

...
public String errorLevel {get; set;}
public String messageName {get; set;}
...
public void showMessage() {
  if(errorLevel == 'WARNING') {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning, messageName));
  }
}

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.