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
WPCMSWPCMS 

The "List has no rows for assignment SObject" error and "Must be a concrete SObject" errors!

I am an administrator that is working on someone elses code. I know the basics about Java programing and even less on apex.  My goal is the get the test coverage above 75%.

 

Most of my errors are

 

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

 

From what I am reading on other boards I have a basic idea of what I need to change. But once I change it, something else down the code gives me an errors.

Here is the original code

 public static testMethod void testManageVendorBillDetails(){  
        Invoice_and_Billing_Detail__c invoiceDetails = [Select Id,Adjustment_amount__c,Amount__c, Vendor_Bill__c
        from Invoice_and_Billing_Detail__c  limit 1 for Update];  
      

 Then I changed it to a List<>

 public static testMethod void testManageVendorBillDetails(){  
        List <Invoice_and_Billing_Detail__c> invoiceDetails = [Select Id,Adjustment_amount__c,Amount__c, Vendor_Bill__c
        from Invoice_and_Billing_Detail__c  limit 1 for Update];  
          

 But now I get an error further down the method. Here is the whole method and I will note what line (towards the bottom) that is giving me the error.

 public static testMethod void testManageVendorBillDetails(){  
        List <Invoice_and_Billing_Detail__c> invoiceDetails = [Select Id,Adjustment_amount__c,Amount__c, Vendor_Bill__c
        from Invoice_and_Billing_Detail__c  limit 1 for Update];  
          
        Vendor_Bills__c vendorBillRecord = new Vendor_Bills__c();
        vendorBillRecord.Bill_Date__c = System.today();
        vendorBillRecord.Due_Date_On_Bill__c = System.today().addDays(10);
        vendorBillRecord.Vendor_Invoice_Number__c = 'Test Bill Record';

        Account vend = new Account(Name = 'Test Vendor', 
                       Phone = '(508) 555-1212',
                       Fax = '(508) 555-1213',
                       Type = 'Vendor',
                       Address1__c = 'asfasd',
                       City__c = 'myTstVen',
                       State__c = 'MA',
                       Zip_Code__c = '2090',
                       Company_Name__c = 'Test Vendor',
                       QB_Account__c = 'QB Account Name',
                       Account_QB_Account_Name__c = 'QB Account Name Ven',
                       Account_Code__c = 'testven1');
        insert vend;
        vend = [select ID from Account where Name Like '%myTstVen%' Limit 1];
        
        User userNAM = [select Id from User limit 1];
        
        Account acct = new Account(Name = 'Test Account Name', 
                   Phone = '(508) 555-1212',
                   Fax = '(508) 555-1213',
                   Type = 'Client',
                   Address1__c = '2234234',
                   City__c = 'ShortPayTstClnt',
                   State__c = 'MA',
                   Zip_Code__c = '2090',
                   QB_Account__c = 'QB Account Name',
                   Account_QB_Account_Name__c = 'QB Account Name',
                   Account_Code__c = 'aewrew',
                   NAM_User__c = userNAM.Id,
                   Generate_Tree_Report_Data__c = false,
                   Generate_Tree_Report_As_Of__c = date.today());
        
    	insert acct;
    	
        Contact tstContact = new Contact(LastName = 'test',
						AccountId = acct.Id,
						Contact_Type__c = 'Client',
						Phone = '(555) 555-5555',
						Contact_Region__c = 'test');
    	insert tstContact;
    
        Location__c loc = new Location__c(Name = 'myTst Location', 
                                    Account__c = acct.Id, 
                                    Address__c = '12543',
                                    City__c = '233',
                                    State__c = 'ID',
                                    Zip_Code__c = '2090',
                                    Location_County__c = 'asfsd',
                                    Active_Date__c = date.today(),  //.parse('1/1/2009'),
                                    //Location_Billing__c = 'TestLoc2',
                                    Non_Active_Date__c = date.today(),
                        			Non_Active_Reason_Code__c='asfsd',
                       			    Close_Date__c =date.today(),
                                    Billing_Address_1__c = '23q234',
                                    Billing_City__c = 'Westwood',
                                    Billing_Zip__c = '2090',
                                    Billing_State__c = 'MA',
                                    Quickbooks_Account_Name__c = 'QB Account Name',
                                    Market_Type__c='Open Market',
                                    Contact2__c = tstContact.Id);
                                   
        insert loc; 
            
        loc =  [select ID, Name, Account__c, Account__r.Name from Location__c where Name = 'myTst Location' Limit 1];
        Vendor_Location__c vendLoc = new Vendor_Location__c(Name = 'mytestven1 (sgfsdf)',
                                        RecordTypeId = '01280000000BTSPAA4',
                                        Locationnew__c = loc.ID,
                                        Vendor__c = vend.ID,
                                        PO_Number__c = 'sgdfds',
                                        Commencement_Date__c = date.today(),  //.parse('1/1/2009'),
                                        Vendor_Account_Number__c = 'sgfsdf',
                                        Quickbooks_Account_Name__c = 'QB Account Name',
                          				           
                                        Billing_Vendor__c = vend.ID);
        insert vendLoc; 
        vendLoc = [select ID, Locationnew__c from Vendor_Location__c where Locationnew__c = :loc.ID Limit 1];
        System.debug('vendloc = ' + vendLoc + '   Loc.ID = ' + loc.ID);
        vendorBillRecord.Vendor_Location__c = vendLoc.ID;
        
        insert vendorBillRecord;
        
        invoiceDetails.Vendor_Bill__c  = vendorBillRecord.Id;/*Save Error: Initial term of field expression must be a concrete SObject List<Invoice_and_Billing_Detail__c>*/
        update invoiceDetails;
        
        delete invoiceDetails; 
    }

Any help would be great!

 

Thank you in advance.

Best Answer chosen by Admin (Salesforce Developers) 
eric.luiseric.luis

In any scenario, I'd recommend to create a new record in the code, there is a sample:

 

 

Object__c obj = new Object__c(
	Field1 = 'value',
	Field2__c = 'value2'
);
insert obj;

 

Doing this, your test method will always be working.

 

 

Yes, in a full sandbox, the way the class is should work.

Also you can create a record manually.

 

All Answers

eric.luiseric.luis

You need to put the array index, once you are using a list.

Once you put "LIMIT 1" in the SOQL, the index will be 0, so, you can use invoiceDetails[0].VendorBill__c or any other field queried.

 

 

Edit: But the best practices says that you should insert new records instead of querying them.

WPCMSWPCMS

Yes that got rid of the existing error but now I have an out of bounds error

 

invoiceDetails[0].Vendor_Bill__c  = vendorBillRecord.Id;

 

System.ListException: List index out of bounds:0

eric.luiseric.luis

I recommend you to insert a new Invoice_and_Billing_Detail__c record just for using in this test method.

Actually, that is what salesforce recommends.

Instead of searching a existing record, insert a new one (this is only a test method, everything you do there, is not made into the database)

WPCMSWPCMS

I see what you are saying. I do not code and I am not sure how to insert records. I thought that was what the code was doing?

 

But anyhow, is it because I am in a developer sandbox with no records? Should I do this in our full sandbox? Or can I just create records in the developer sandbox and this should take care of all of my List has no rows for assignment SObject? I am just trying to get the test coverage up and had thought most of the errors were the new validations I had entered.

 

I appreciate your advice.

eric.luiseric.luis

In any scenario, I'd recommend to create a new record in the code, there is a sample:

 

 

Object__c obj = new Object__c(
	Field1 = 'value',
	Field2__c = 'value2'
);
insert obj;

 

Doing this, your test method will always be working.

 

 

Yes, in a full sandbox, the way the class is should work.

Also you can create a record manually.

 

This was selected as the best answer
WPCMSWPCMS

Thank you for helping out a newb. Once I created a Force.com project from our sandbox most of my errors went away. I will still work on getting the insert records to replace the queries.

 

THANKS!