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
Patrick G. BrownPatrick G. Brown 

Need assistance with System.QueryException: List has no rows for assignment to SObject

I have a very simple query that ls looking querying Accounts to find one particular Account, then assign that Account's ID to the Account field on my Contact record.  I continue to get the "System.QueryException: List has no rows for assignment to SObject" error, but when I run the query using Query Editor, it returns the appropriate record.  Can anyone please tell me what I'm doing wrong?
 
trigger assignAccounttoContact on Contact (before insert, before update) {
    
    Account a = [
        SELECT ID, Name 
        FROM Account 
        WHERE Name = 'TestAccount'
    ];
    
    for(Contact c: Trigger.new){
        
        c.AccountID = a.ID;
    }
    
}

 
Patrick G. BrownPatrick G. Brown
I'd also like to add I tried the following code (as other posts mentioned first creating a list) and received a different error (Initial term of field expression must be a concrete SObject: List<Account>):
 
trigger assignAccounttoContact2 on Contact (before insert, before update) {
    
    List<Account> a = [
        SELECT ID, Name 
        FROM Account 
        WHERE Name = 'TestAccount'
    ];
    
    for(Contact c: Trigger.new){
        
        if (a.size() > 0)
        c.AccountID = a.ID;
    }
    
}

 
RohanGoreRohanGore
Try this: 
trigger assignAccounttoContact2 on Contact (before insert, before update)  {
List<Account> a = [ SELECT ID, Name FROM Account WHERE Name = 'TestAccount' ];
for(Contact c: Trigger.new) {
    if (a.size() > 0)
    c.AccountID = a[0].ID;
}
}

You are trying to access the field from a list, though it should be accessed from a record.
Shreyas Shah 9Shreyas Shah 9
Hi,
This will Help you : 
Trigger assignAccounttoContact on Contact (before insert, before update)  {
Account a = null;
a = [ SELECT ID, Name FROM Account WHERE Name = 'TestAccount' ];
if(a != null){
for(Contact c: Trigger.new) {
    c.AccountID = a.ID;
}
}
And don't use if check in for loop every contacts .
 
Patrick G. BrownPatrick G. Brown
@RohanGore - Thanks for your suggestions.  You helped me clear the "Initial term of field expression must be a concrete SObject: List<Account>" issue.  

However, I'm still getting a "System.QueryException: List has no rows for assignment to SObject" error.  I simply can't understand why it's happening.  When I run this query directly, it works fine:

User-added image

When I execute my test, it fails:

User-added image
Amit Chaudhary 8Amit Chaudhary 8
Can you please check your org have and Apex class with Name Account if yes then please delete or rename and try below code that should work
trigger assignAccounttoContact2 on Contact (before insert, before update) 
{
    
    List<Account> a = [ SELECT ID, Name  FROM Account  WHERE Name = 'TestAccount' ];
    for(Contact c: Trigger.new)
	{
        if (a.size() > 0)
		{
			c.AccountID = a[0].ID;
		}
    }
}

Let us know if that will help you