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
RajashriRajashri 

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";

}

 

Best Answer chosen by Admin (Salesforce Developers) 
jd123jd123

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

MagulanDuraipandianMagulanDuraipandian

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.

BharathimohanBharathimohan

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

 

jd123jd123

Hi

 

List<Account> accs=[select id, Name from Account ]

if(accs.size()==0)

{

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Your message'));

}

else

 

your logic 

 

add

<apex:pagemessages/> in your visualforce page

RajashriRajashri

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.

 

 

jd123jd123

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)

RajashriRajashri

My List is empty so still it is giving me same error

 

"List has no rows for assignment to SObject."

Noam.dganiNoam.dgani

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

}

jd123jd123

Can you paste your total code. I can help you out.

 

please paste your error with line no 

jd123jd123

Hello

 

you can use

try

{

 

 

}catch(Exception e){

 

------your error  message--

Noam.dganiNoam.dgani

how is that different jd123?

RajashriRajashri

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));
               
          }

Noam.dganiNoam.dgani

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));

}
                            

RajashriRajashri

Thanks but it's not working... :(

jd123jd123

Rajashri 

 

I think you are getting that error in some other line.
at which line no you are getting error.

RajashriRajashri

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?

 

jd123jd123

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

 

This was selected as the best answer
RajashriRajashri

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;
    }

jd123jd123

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];

 

 

 

RajashriRajashri

Thanks a Lot Mahi..!It works!!

jd123jd123

Good work Rajashri. You can ask any no of  questions we are ready to help you.

RajashriRajashri

Thanks!!I have posted one more message on workflow approval can you please help me on that?