+ Start a Discussion
Sapana WSapana W 

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!!

Neha LundNeha Lund

Hi Just add this line in your getFunds method -

 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'***Type your message here******.');

Neha LundNeha Lund

add this line as well-

ApexPages.addMessage(myMsg);

hitesh90hitesh90

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/

asish1989asish1989

HI

Try in this way..

public class CTRL_FundTab
{
   public String errorMsg{get;set;}
    public CTRL_FundTab()
    {
		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);
			
			//error message
			if(funLst == null && funLst.size()>0){
				errorMsg = 'There are currently no ENT_Fund available. Please check back in a few days. Sorry   the  inconvenience.';
                    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR,errorMsg )); 
			}

    }
public List<ENT_Fund__c> getFunLst() { return funLst; } }

 

<apex:pageMessages/>
<apex:pageBlockTable value="{!FunLst}" var="row">

Sapana WSapana W

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

Neha LundNeha Lund

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

hitesh90hitesh90

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/

Sapana WSapana W

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

hitesh90hitesh90

You have to update your class code as below.

 

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];
		if(cpeLst.size() > 0){
			String ff = cpeLst[0].Fund__r.Name;
			list<ENT_Fund__c> funLst = [select Id,Name from ENT_Fund__c where Name =: ff];
			if(funds.size() == 0){
				ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No Records exist for this User.');
				ApexPages.addMessage(myMsg);
			}			
		}
		return funLst;
   }
}

 

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

Sapana WSapana W

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

asish1989asish1989

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.

Sapana WSapana W

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?

Sapana WSapana W
Also my doing just POC in my dev org. And I would follow the standards after doing it on sandbox INT
asish1989asish1989

Try in this way and let me know

 

<apex:pageMessages/>
<apex:pageBlockTable value="{!FunLst}" var="row">

public class CTRL_FundTab
{
   public String errorMsg{get;set;}
    public CTRL_FundTab()
    {
ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;
if(contactid !=null) ID AccID = [Select AccountID from Contact where id =: contactid].AccountId; If(AccID !=null)
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); //error message if(funLst == null && funLst.size()>0){ errorMsg = 'There are currently no ENT_Fund available. Please check back in a few days. Sorry the inconvenience.'; ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR,errorMsg )); } }
public List<ENT_Fund__c> getFunLst() { return funLst; } }
Sapana WSapana W

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

Sapana WSapana W

Hi

 

i tried this included <apex:pageMessages/> too in VF page and still Vf error .I am not getting what the problem is?

public class CTRL_FundTab
{
   public String errorMsg{get;set;}
public list<ENT_Fund__c> funLst{get;set;}
public CTRL_FundTab() {
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; funLst = [select Id,Name from ENT_Fund__c where Name =: ff]; system.debug('$$$$$$$'+funLst.size()); system.debug('$$$$$$$'+funLst[0].name); //error message if(funLst == null && funLst.size()>0){ errorMsg = 'There are currently no ENT_Fund available. Please check back in a few days. Sorry the inconvenience.'; ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR,errorMsg )); } }
public List<ENT_Fund__c> getFunLst() { return funLst; } }
asish1989asish1989

hey write <apex:pagemessages /> just below <apex:form>, see message is comming or not.

<apex:form>

<apex:pagemessages />

.....................rest of your code