You need to sign in to do that
Don't have an account?
Rajashri
List has no rows for assignment to SObject
Hi ,
My requirement is if the size of list is 0 then display error instead of the error message "List Has No rows.." i want to display use friendly error..Can anyone guide me?
List<Account> accs=[select id, Name from Account ]
if(accs.size()==0)
{
"display error";
}
Good.
try the below code
List<Overview__c> projects = [select Id,Name, Project_Name__c FROM
SEC_Overview__c WHERE Project_Name__c = :dd.Project_Name__c];
if(projects.isEmpty())
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'List is empty')) ;
}
else
{
//do youe code
}
otherwise write in try and catch.
if you have any questions please free to post
All Answers
In VF use <apex:pageMessages />
In Class use
List<Account> accs=[select id, Name from Account ]
if(accs.size()==0)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: List is empty.');
}
Regards,
Magulan D
Salesforce.com certified Force.com Developer.
SFDC Blog
If this post is your solution, kindly mark this as the solution.
Hi Rajashri,
Try this code,
List<Account> accs=[select id, Name from Account ]
if(accs.size()==0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'List has no rows')) ;
}
This code adds a Page Message in the visualforce page.
Please Mark this post as solved, if it helps you
Regards,
Bharathi
Salesforce For All
Hi
List<Account> accs=[select id, Name from Account ]
if(accs.size()==0)
{
}
else
your logic
add
<apex:pagemessages/> in your visualforce page
Thanks for the reply.
My list is empty and that's the reason it is giving me an error message.
List has no rows...I need a solution on that and not how to display message.
Hello Rajashri
Write this line in your apex class
List<Account> accs=[select id, Name from Account ]
if(accs.size()==0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Your message'));
}
Write this line in your visualforce page
<apex:pagemessages/>
then you will see the error message in your viusalforce page what you have written in single quotes(red color)
My List is empty so still it is giving me same error
"List has no rows for assignment to SObject."
List<Account> accs;
try{
accs =[select id, Name from Account ];
}
catch(Exception e)
{
//do whatever you want as error handling - like accs = new List<account>();
//or VF error msg
}
Can you paste your total code. I can help you out.
please paste your error with line no
Hello
you can use
try
{
}catch(Exception e){
------your error message--
}
how is that different jd123?
Thanks Mahi!!
Below is my code
List< Overview__c> overviewList =
[select Id, Project_Name__c FROM
Overview__c ];
if(overviewList.size() == 0){
String msg = 'Please check if Project is ‘
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, msg));
}
try{
List< Overview__c> overviewList =
[select Id, Project_Name__c FROM
Overview__c ];
}
catch(Exception e)
{
String msg = 'Please check if Project is ‘
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, msg));
}
Thanks but it's not working... :(
Rajashri
I think you are getting that error in some other line.
at which line no you are getting error.
Hi,
Yes u r right!!
I am getting an error on some other line where i am trying to execute the below statement.
For below line i am getting an error..
Overview__c projects = [select Id,Name, Project_Name__c FROM
SEC_Overview__c WHERE Project_Name__c = :dd.Project_Name__c];
if(projects.isEmpty())
{
//display error message;
}
else
{
// do rest work;
}
Can you please guide?
Good.
try the below code
List<Overview__c> projects = [select Id,Name, Project_Name__c FROM
SEC_Overview__c WHERE Project_Name__c = :dd.Project_Name__c];
if(projects.isEmpty())
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'List is empty')) ;
}
else
{
//do youe code
}
otherwise write in try and catch.
if you have any questions please free to post
I am checking your solution.
Between I have one more issue . I want to ignore null values from list below is my code
How can i do that?
public static List<Appr__c> getAppr(String pId) {
List<Appr__c> appr = [SELECT ID, NAME, Role__c,Detail__C
FROM Appr__c
WHERE Detail__c = :pId
ORDER by NAME ASC NULLS LAST];
return appr;
}
Rajashri i think You can write like this
List<Appr__c> appr = [SELECT ID, NAME, Role__c,Detail__C
FROM Appr__c
WHERE Detail__c = :pId AND Name!=null AND Role!=null];
Thanks a Lot Mahi..!It works!!
Good work Rajashri. You can ask any no of questions we are ready to help you.
Thanks!!I have posted one more message on workflow approval can you please help me on that?