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
Eric_SantiagoEric_Santiago 

Frustration with apex:messages

http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=2452

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_validation.htm

http://community.salesforce.com/sforce/board/message?board.id=Visualforce&thread.id=2298

http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_message.htm

OK. I've read all of the above thoroughly and I'm still having trouble kicking back a custom error message to a visualforce page using apex:messages. Any suggestions would be appreciated greatly.


Code:
public class dashboardController {

    public Employee__c getEmployee() {
    return [select id, kiosk__r.name, kiosk__c from Employee__c where user_name__c = :UserInfo.getUserId() Limit 1];
    }
    
    public Rep_Quota__c getRepQuota() {
      try {
Rep_Quota__c repquota = [select id, Perf_Day__c, Perf_Week__c, Perf_Month__c from Rep_Quota__c Where Quota_Start_Date__c = THIS_MONTH and Employee__c = :getEmployee().id Limit 1];
return repquota;
} catch (QueryException e) {
ApexPages.Message msg= new ApexPages.Message(ApexPages.Severity.ERROR, 'No Rep Quota record found for current month!', 'No Rep Quota record found for current month!');
ApexPages.addmessage(msg);
return null; } } } <apex:page controller="dashboardController"> <apex:sectionHeader title="Rep Dashboard"></apex:sectionHeader> <apex:pageMessages/> <apex:pageBlock title="{!$User.FirstName} {!$User.LastName}" mode="view"> <apex:image id="user_graph1" value="http://chart.apis.google.com/chart—chs=300x200&cht=gom&chd=t:{!RepQuota.Perf_Day__c}&chl={!RepQuota.Perf_Day__c}&chds=0,200&chf=bg,s,EFEFEF&chtt=Yesterday"/> <apex:image id="user_graph2" value="http://chart.apis.google.com/chart–chs=300x200&cht=gom&chd=t:{!RepQuota.Perf_Week__c}&chl={!RepQuota.Perf_Week__c}&chds=0,200&chf=bg,s,EFEFEF&chtt=This Week"/> <apex:image id="user_graph3" value="http://chart.apis.google.com/chart˜chs=300x200&cht=gom&chd=t:{!RepQuota.Perf_Month__c}&chl={!RepQuota.Perf_Month__c}&chds=0,200&chf=bg,s,EFEFEF&chtt=This Month"/> <hr width="90%"/> <div align="center" style = "font-size:12pt;"><b>Quota Performance</b></div> </apex:pageBlock> </apex:page>

I'm testing a stituation where I know I get a QueryException error (without the catch I get "System.QueryException: List has no rows for assignment to SObject"). I've tried variations with catch(Exception e) and Apex.Messages(e) and ApexPages.addmessage(e.getMessage()); but nothing works. Any suggestions?


 



Message Edited by Eric_Santiago on 07-09-2008 11:59 PM

Message Edited by Eric_Santiago on 07-10-2008 12:15 AM
Eric_SantiagoEric_Santiago
I figured it out. There needed to be an action on the page to call the function. I thought the {!RepQuota.Perf_Day__c} would call the function and throw the expection message, but that wasn't the case. All the other example had a <apex:commandButton action="x" ...> which helped me realize what I was missing.

So to my controller I added

Code:
public PageReference init() {
        getRepQuota();
        return null;
    }

and to my page I added an action to the page tag;

Code:
<apex:page controller="dashboardController" action="{!init}">

 Now it shows the error at the top of the page when it loads. Perfect.

 

jwetzlerjwetzler
Thanks for the update.
 

I figured it out. There needed to be an action on the page to call the function. I thought the {!RepQuota.Perf_Day__c} would call the function and throw the expection message, but that wasn't the case.



Just want to correct this statement a little.  {!RepQuota.Perf_Day__c} does call the function and throw the exception message.  However by the time you've done this, your messages component has already been rendered, so you're not going to actually see the error even though it's there.  You need to actually rerender either your whole page or that part of the page to get the message to show up, which is why you need some sort of action to be performed.

Your solution to use the action on the page component looks like a pretty good one for what you are trying to do.  Your action returns null so the page refreshes, and there's your error.

One suggestion however.  You have a lot of references to getRepQuota() in your page (and now one in your controller).  This means every time you load your page, you're going to be calling getRepQuota 7 times, meaning you're making the same query 7 times.  You should store off the value so you only have to do it once.

Code:
public class dashboardController {

    Rep_Quota__c repQuota;
    
    public Rep_Quota__c getRepQuota() {
      if (repQuota == null) {
        try {
        repQuota =  [select id, Perf_Day__c, Perf_Week__c, Perf_Month__c from Rep_Quota__c Where Quota_Start_Date__c = THIS_MONTH and Employee__c = :getEmployee().id Limit 1];
        return repquota;
        } catch (QueryException e) { 
          ApexPages.Message msg= new ApexPages.Message(ApexPages.Severity.ERROR, 'No Rep Quota record found for current month!', 'No Rep Quota record found for current month!');
          ApexPages.addmessage(msg);
          return null;
        }
      }
      return repQuota; 
    
    }

}

This is generally a good idea for all of the queries you're making in your getters.  No sense in running them more than once if your criteria is not going to change.