function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
learnSFlearnSF 

ApexPages.addMessages(e) in visual force page

 
I need to handle exception and show my person message(like record is deleted) when user try to fetch data which is already deleted.
 
I wrote code like this in extention controller.
 
Code:
  public MyControllerExtension(ApexPages.StandardController stdController) {
     
      try {               
     
     
      this.acct  = [select id, name, site,Account_Manager__c,RecordTypeId,Dealer_ID__c,OwnerId from Account where id =
                       :System.currentPageReference().getParameters().get('id')];
      this.profile=[Select p.Name From Profile p where p.Id=:UserInfo.getProfileId()];
       } catch (System.QueryException e) {       
        
             ApexPages.addMessages(e);           
        }       
     
      init();
    }

 And page is like this.
Code:
<apex:page standardController="Account" extensions="MyControllerExtension" action="{!doRedirect}">
<apex:pageMessages />

       <apex:detail relatedList="true" relatedListHover="true"subject="{!account.id}"/>

   </apex:page>

Now this page shows message which is not user readable.Istead of standard message,I need to diaply like"record is deleted and it will be in recyclebin for 30 days"
 
How I can show this meesage instead of standard message in visual force page?
 
Any help will be great.
Ron HessRon Hess
you may be able to do this (note: i haven't tried it yet)

ApexPage.addMessage('record is deleted and it will be in recycle bin for 30 days');


what message do you get today?
learnSFlearnSF

Thanks Ron for reply.

I tried to write message in addMessage method,but it gives me error that ,method signature(String) does not exist.

Right now I me getting below error with catch statement.

Attempt to de-reference a null object

An unexpected error has occurred. Your development organization has been notified.

Without writing catch statement error was like shown below.

List has no rows for assignment to SObject

An unexpected error has occurred. Your development organization has been notified.

 

learnSFlearnSF

Hi Ron,

I wroe like this.

} catch (System.QueryException e) {      
       
             ApexPages.addMessage('record deleted');           
}   

But it does not allowed to arite string in addMessage method.It says method does not exist or addMessage(String)is not correct argument.

Could you please tell me how I can write personal message in place of e?

jwetzlerjwetzler
If you're having problems with something not compiling, especially in Apex, the first place you should look is the doc.

Here's a thread where we discussed something similar and I provided several links to the doc.
learnSFlearnSF

Hi,

I read document but I am not able to find example where I can write my own custom messages instead of standard.Each exmaple shows standard messages passes to pages.

When I am trying to write custom messages,addMessage methos does not allowed to save me class nad giving compile error.If you see my above post you will see what type of message I want.

I tried this way also.

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'my error msg');

public MyControllerExtension(ApexPages.StandardController stdController) {

try {

this.acct = [select id, name, site,Account_Manager__c,RecordTypeId,Dealer_ID__c,OwnerId from Account where id =

:System.currentPageReference().getParameters().get('id')];

this.profile=[Select p.Name From Profile p where p.Id=:UserInfo.getProfileId()];

} catch (System.QueryException myMsg) {

ApexPages.addMessage(myMsg);

}

init();

}

 
doesn't help.
jwetzlerjwetzler
The compile error given for that is:
Error: Compile Error: Method does not exist or incorrect signature: ApexPages.addMessage(System.QueryException)

That tells you that you're trying to pass a QueryException into the addMessage method, which is incorrect because you want to be passing in your own message.  You named your query exception myMsg, which is the same name as your ApexPages.Message.  It's not doing what you think it's doing because your message is outside of the scope.

Code:
} catch (System.QueryException ex) {

 
If you rename your QueryException it will work fine.

I would also suggest bring your ApexPages.Message myMsg declaration inside the catch, since there's no reason to construct it unless you actually need to add the message.
learnSFlearnSF

Hi,

Yes I am getting same error.

But still I didnot understand after catching query exception,how I can diaply personal message to page?As It dipsly standard message which is define for query Exception,which is not user readable.

As addMessage only accept QueryException or any other Exception only.How I can pass String Message to method or page?

aballardaballard

ApexPages.addMessages()  (plural) accepts only an exception.  ApexPages.addMessage() (singular) accepts a Message object. 

Your attempt in which you created a Message object containing the error text is almost correct.    Jill explained above where it went wrong.... take another look, I think you must have not quite made the changes she suggested .

 

SirishaMSirishaM
Hi ,

I encountered a similar error and this is how u can add the error message to ur page..

In the apex class u add
Code:
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Due date passed');
ApexPages.addmessage(myMsg);

 Then in the Visual force page add


Code:
<Apex:pageMessages/>


This will help displaying your messages  on the page


Cheers,
Sirisha




learnSFlearnSF

I got it.

Now I wrote code like this.

Code:
 public MyControllerExtension(ApexPages.StandardController stdController) {
     
      try {        
          this.acct  = [select id, name, site,Account_Manager__c,RecordTypeId,Dealer_ID__c,OwnerId from Account where id =
                       :System.currentPageReference().getParameters().get('id')];
          this.profile=[Select p.Name From Profile p where p.Id=:UserInfo.getProfileId()];
         } catch (System.QueryException e) {       
             ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Record is deleted');
             ApexPages.addMessage(myMsg);           
        }       
     
      init();
    }

 

And page is like this.

Code:
<apex:page standardController="Account" extensions="MyControllerExtension" action="{!doRedirect}">
<apex:pageMessages />

       <apex:detail relatedList="true" relatedListHover="true"subject="{!account.id}"/>

   </apex:page>


 
Even though I add personal message to addMessage method,it still shows same error message.

Attempt to de-reference a null object

An unexpected error has occurred. Your development organization has been notified. 

It is not showing my perosnal message.

Any suggestion?
 



Message Edited by learnSF on 06-24-2008 12:30 PM
jwetzlerjwetzler
You're catching a QueryException but it looks like what is getting thrown is a NullPointer.
learnSFlearnSF

One more thing.

Developer edition shows <apex:pageMessage/>  in refrence component while this tag I am trying to writing in my sandbox page,but it givis error.

It suggest only one tag that is <apex:pageMessages/>

Any idea?

learnSFlearnSF
If I am catching null pointer exception,it shows below error.
 
List has no rows for assignment to SObject

An unexpected error has occurred. Your development organization has been notified.
 
If catching Query exception is show

Attempt to de-reference a null object

An unexpected error has occurred. Your development organization has been notified.
  error.

But anytime it didn't show my personal message.

jwetzlerjwetzler
It sounds like there's something wrong with your controller.  The first message means that your query didn't return anything.  You expect that to happen?  The second message means you are trying to use a null object to perform some operation.  I don't think you want to actually catch a NullPointer, you want to instead make your code handle null checks where necessary.

Are you in development mode?  Based on the message it doesn't look like it, you should be getting an Apex code stack trace.


learnSFlearnSF

Basilcally this record is deleted by user. and if they copy and paste same url,query is trying to fatch data based on id on url and data is not present.

So it give error for null ponter.

But even though exception catch I am not sure why it is not showing personal message?

jwetzlerjwetzler
I think you should be expecting a QueryException.  What I'm pretty sure is happening:
The NullPointer is actually happening somewhere later, like maybe in your init method, which is why you need to enable development mode so you can see your stack and find out where it's happening.  Or maybe you are trying to reference {!account.name} on your page when the account has been deleted.  In which case you'll need to conditionally render the blocks that are referencing account fields.

If you're catching a NullPointer around your query that's not going to work.  It's not throwing a NullPointer, it's throwing a QueryException, which is why your QueryException is bubbling out to the page.

If you're catching a QueryException, do you need to call return; after setting the message, instead of continuing on to call init()?  You're probably catching it and adding the exception correctly, but an NPE is happening somewhere else in your page, and so the page isn't going to load, it's going to take you to the error page instead.

Please try that out (if you're not in development mode, you should definitely enable it!) and make sure your page and controller are bulletproofed against having a null account, since you expect that the account can be null from time to time.
learnSFlearnSF
Thanks Jill.
 
You are right.
I was getting null pointer exception in init () method where I wrote another query based on accout object.
 
Now I make sure every where that account is not null.
finaly I got this type of message but not my personal message.
 
Data Not Available
The data you were trying to access could not be found. It may be due to another user deleting the data or a system error. If you know the data is not deleted but cannot access it, please look at our support page.

 

One more thing,Do I have to enable development mode in sandbox through my personal information?

How this mode will help me? Sorry but I am not familier with stack trace.



Message Edited by learnSF on 06-25-2008 08:06 AM

Message Edited by learnSF on 06-25-2008 08:07 AM
jwetzlerjwetzler
Yeah if you're using the standardController I'm not sure that you're going to be able to even get to the page with a deleted ID. The standardController does a lot of things to get your record under the covers.

If you're not using development mode you should be!!!! Please look at the documentation, it is the first thing you are asked to do, and it's where I would have expected you to start.

Development mode gives you access to the inline editor as well as all of the quickfixes and things that make developing in Visualforce much much easier. It also changes some of the error messaging behavior -- you might see much more descriptive error messages in dev mode, including the Apex code stack trace that will help you pinpoint the line that's throwing an exception. Had you been in dev mode I suspect you would have seen your NPE coming from your init method and fixed it right away.

You enable it through your personal information, and this info is on the first page of the quickstart in the dev guide! Please read it, if you're going to be developing in Visualforce you should have this perm turned on.

Message Edited by jwetzler on 06-25-2008 11:35 AM