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
Yoshinori MoriYoshinori Mori 

System.QueryException: List has no rows for assignment for SObject.

When I tried Run-test on Eclipse, The above subject error message shows up.

This is a bit too vaqgue for me.

Could it be possible to teach me what this means more clearly for me.

Regards

 

Mori 

kiranmutturukiranmutturu

you are querying an object and getting the result in to a list.. u are not cheking the list is filled or empty before using that..... thats y u r getting the error.

Sonali BhardwajSonali Bhardwaj

You are firing a SOQL and returning its result in a single SObject Like:

Account a = [Select id from Account];

 

But Your SObject does not have any records, that's the reason of error.

Imran MohammedImran Mohammed

Yes, you are querying an object that returns a list and you kight be storing in a Sobject.

Its better you have a list at the receiving end, something like this.

Account[] accList = [select name from Account];

 

In this case, even if no rows are returned the accList variable size will be zero and will not throw the above error message.

Rahul S.ax961Rahul S.ax961

use list instead of object like,

 

-This will throw same error (System.QueryException: List has no rows for assignment for SObject.).

 

Account objAccount = [Select Name, Id from Account where name = 'TestAccount']; // There is no Account with such Name.

 

-now use List Instead Object (won't throw error):

 

 

List<Account> lstAccount = [Select Name, Id from Account where name = 'TestAccount']; // There is no Account with such Name.
if(!lstAccount.isEmpty()) // check if list is not empty
{
  // Access ur first element in List by - lstAccount[0];
}
else
{
 system.debbug('List is Empty');
}

 

 

Yoshinori MoriYoshinori Mori

Thank you for the information.

However, the data to be stored into object by SOQL from object is not going to be List Type.

So, the object that calls the method and retrieve is not List type.

 

The method that returns the value need to be something like .......return retVal[0];?

only the retVal needs to be retVal[0]? It means array?

 

Thanks

Mori 

 

Rahul S.ax961Rahul S.ax961

Can you paste your code for furthur assistance

Yoshinori MoriYoshinori Mori

Thank you for the help.

 

Pasted below is the part that has failures.

There has been no error with Eclipse and Salesforce.

The data output from Salesforce is correct

 

<<Class Name: WXS_CL_programs>>

(Getter&Setter definition)

public WXS_T_sample__c kisyo { get; set; }

(Default Constructor)
 public WXS_CL_programs(){

(
  public void setObjects()
    {
       ##Call Method   
        storedata = WXS_CL_DBAccess.getKishouXmlStoreById(kid);

 

<<Class Name: WXS_CL_DBAccess>>

//Method to be called
public static WXS_T_sample__c getKishouXmlStoreById(string sid)
    {
        WXS_T_sample__c Data = new WXS_T_KishouXmlStore__c();
        Data = [select   GISReceipt__c, POP_UPReceipt__c, AreaMailReceipt__c,
                            IsseiMailReceipt__c, Status__c, ID__c, EventID__c,
                            InfoType__c, Serial__c, Title__c, SecodaryReceipt__c,
                            CreatedDate, PublishingOffice__c , ReportDateTime__c ,
                            EditorialOffice__c, RenkeiString__c ,Name,
                            Target__c,DateTime__c,ShubetsuNo__c,
                            KisyouKbn__c,Keyword__c,
                            tsuuchi_title__c, tsuuchi_message__c,
                            (Select ParentId From Attachments),
                            (Select AttachmentURL1__c, AttachmentURL2__c, AttachmentURL3__c,
                             AttachmentURL4__c, AttachmentURL5__c From KishouXmlStore_id__r)
                    from WXS_T_sample__c
                  where id = :sid];
        if(Data !=null)
            return Data;
        else
            return null;
    }

 

Thanks

Mori

Rahul S.ax961Rahul S.ax961

Hi Yoshinori,

 

Just Check two things here,

 

- is Id of any record of WXS_T_sample__c is passed from below function?

     storedata = WXS_CL_DBAccess.getKishouXmlStoreById(kid);    //   Make sure you are passing id here.

 

-  Do something like this in Second class :

 

 

<<Class Name: WXS_CL_DBAccess>>

//Method to be called
public static WXS_T_sample__c getKishouXmlStoreById(string sid)
    {
        List<WXS_T_sample__c> lstData = new List<WXS_T_KishouXmlStore__c>();
        lstData = [select   GISReceipt__c, POP_UPReceipt__c, AreaMailReceipt__c,
                   IsseiMailReceipt__c, Status__c, ID__c, EventID__c,
                   InfoType__c, Serial__c, Title__c, SecodaryReceipt__c,
                   CreatedDate, PublishingOffice__c , ReportDateTime__c ,
                   EditorialOffice__c, RenkeiString__c ,Name,
                   Target__c,DateTime__c,ShubetsuNo__c,
                   KisyouKbn__c,Keyword__c,
                   tsuuchi_title__c, tsuuchi_message__c,
                   (Select ParentId From Attachments),
                   (Select AttachmentURL1__c, AttachmentURL2__c, AttachmentURL3__c,
                   AttachmentURL4__c, AttachmentURL5__c From KishouXmlStore_id__r)
                   from WXS_T_sample__c
                   where id = :sid];
        if(!lstData.isEmpty())
	{
		system.debug('========Check this is its not null=========='+lstData[0]);
		return lstData[0];
	}	
        else
	{
	    system.debug('========There is no record with such Id==========');
            return null;
	}
    }

 

 

 

 

Yoshinori MoriYoshinori Mori

Thank you for your advice.

You mean all variable of "kisyo" used in the first class needs to be changed to List Type also?

Is it correct?

The only thing why I couldn't understand is there was an error only when I do Run-testing.

 

Mori

 

 

Yoshinori MoriYoshinori Mori

The receiving variable, which is storedata in the first class, needs to be changed to List Type.

Is this correct?

For instance, the variable is to be used as below.

Please forgive me if I'm wrong, but the variable does not  have to be necessarily list in this case.

To extract data and store data, the variable needs to be List Type?

 

I'm not sure about the definition of  when to use List Type and when to use Non-List type.

If you don't mind could it be possible to clarify the definition for me? 

 

 

public void setObjects() {

 

 ##Call Method

   

        storedata = WXS_CL_DBAccess.getKishouXmlStoreById(kid);

 

        storedata.ReportDateTime__c = storedata.ReportDateTime__c.addhours(9);

 

         if(storedata.Target__c!=null) storedata.Target__c = storedata.Target__c.replace('"','');