You need to sign in to do that
Don't have an account?
Need help with Apex error message
I have created a VF page to display Funds records related to current portal user. In the controller I have written a query to return records, which is working fine but I am not able to return error message if no records exist for that user.Here's the code for VF and apex.
=======VF Page======
<apex:page controller="CTRL_FundTab" tabStyle="ENT_Fund__c" title="ENT_Fund__c" >
<apex:form id="form" >
<apex:sectionHeader title="Fund" />
<apex:pageBlock title="Recent Funds">
<apex:pagemessages />
<apex:pageBlockTable value="{!funds}" var="row">
<apex:column >
<apex:facet name="header">Fund Name</apex:facet>
<apex:outputLink title="" value="/{!row.id}" >{!row.Name}</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
=======Controller====
public class CTRL_FundTab
{
public CTRL_FundTab()
{
}
public List<ENT_Fund__c> getFunds()
{
ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;
ID AccID = [Select AccountID from Contact where id =: contactid].AccountId;
list<ENT_Closing_Phase_Entity__c> cpeLst = [select Id,Name, Account__r.Name,Fund__c,Fund__r.Name from ENT_Closing_Phase_Entity__c where Account__c =:AccID];
String ff = cpeLst[0].Fund__r.Name;
list<ENT_Fund__c> funLst = [select Id,Name from ENT_Fund__c where Name =: ff];
system.debug('$$$$$$$'+funLst.size());
system.debug('$$$$$$$'+funLst[0].name);
return funLst;
}
}
When I try to return Apex.Message object in getFunds() it shows me an error saying return type does not match
Thanks in advance!!
Hi Just add this line in your getFunds method -
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'***Type your message here******.');
add this line as well-
ApexPages.addMessage(myMsg);
Hi,
you have to include <apex:pageMessages /> tag in your VF page.
and add below code in your controller.
if(funds.size() == 0){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No Records exist for this User.');
ApexPages.addMessage(myMsg);
}
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/
HI
Try in this way..
<apex:pageMessages/>
<apex:pageBlockTable value="{!FunLst}" var="row">
Hi ,
Thanks for quick reply.
I tried what u ppl gave but didnt work . It still shows following error
Visualforce Error
QueryException: List has no rows for assignment to SObject Class.CTRL_FundTab.getFunds: line 21, column 1
Post your line 21 :
i guess there is no account related to Contact
You can put your query in try catch block
try{
//Query
}
catch(Exception e)
{
Post the ApexPages.addMessage code here
}
This will work fine
remove below line from your controller
system.debug('$$$$$$$'+funLst[0].name);
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/
Hi ,
I got this error..after trying wat u suggested
Visualforce Error
System.QueryException: List has no rows for assignment to SObject
Class.CTRL_FundTab.<init>: line 8, column 1
You have to update your class code as below.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/
Hi
I am not getting any error now but my doesnt show me my custom error message. Do I have to add any attribute in <apex:pagemessages /> tag for this. Because I want to show my error msg on the Vf page as soon as it loads
Yes You need to add <apex:pagemessages />.
One thing I must say Its not a good pratice to write query inside getfunction();Anyways the error is solved its fine.
see my previous post.
Hi Asish,
I want my error message per say 'No records found for user ' to be displayed on VF page. Currently it shows me just a blank page block with column header. What should I do to display my own message?
Try in this way and let me know
<apex:pageMessages/>
<apex:pageBlockTable value="{!FunLst}" var="row">
Hi Neha,
In try catch block I wont be able to return my FundsList else the loop will get over there. Correct me If I am wrong
Hi
i tried this included <apex:pageMessages/> too in VF page and still Vf error .I am not getting what the problem is?
hey write <apex:pagemessages /> just below <apex:form>, see message is comming or not.
<apex:form>
<apex:pagemessages />
.....................rest of your code