0

I just used the below code in my application but the page message not show. What i doing wrong

VF Page:

<apex:pageMessages id="msgs"/>
<apex:pageBlock >
<apex:outputpanel id="outid" >
<div id="divId"> 
<apex:pageBlockTable value="{!SomeList}" var="k" >
<apex:column headerValue="COl 1">
 <a href="javascript:if (window.confirm('Are you sure?'))CallMethod('{!k.Id}');" >Del</a>
</apex:column>

etc.,

Apex:

public PageReference  CallMethod()
 {
try{ 
    //my insert code    
   }catch(System.DMLException e) {
apexpages.Message  msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Fake Error');
apexpages.addmessage(msg);
return Page.MyPageName;
} 
return null;
}   

I think some render problem. Because if i am removing the try catch, i got the exception error in the page.

2 Answers 2

1

I have found what was the issue. Simply Define the id for pagemesage and render that well.

0

When you create an ApexPages.Message, you need to return null to refresh the current page if you want the user to see the message on the current page

Hence the return Page.MyPageName in the catch block should be return null;

If you want the message to appear on a redirected page, you need to place the message in a query string param that is read by the VF controller constructor of that redirected page which in turn recreates the ApexPages.Message

UPDATE

I also noticed that you are invoking your controller's action method via a Javascript call:

<a href="javascript:if (window.confirm('Are you sure?'))CallMethod('{!k.Id}');" >Del</a>

This will not work as Javascript can't directly invoke a controller action method. Instead, you use apex:actionFunction to do this.

You can see this done here and here in the SFDC doc (showing how to pass a parameter)

1
  • Have you tried inserting a record which will throw an exception and enter the catch block?
    – Poonam
    Commented Apr 13, 2015 at 5:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .