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
Scott0987Scott0987 

List has no rows for assignment to SObject

May be my brain is just not working this morning, but I am not sure how to do what I am looking at doing.  

for(Account_To_Import__c ai : [select id, name from Account_To_Import__c where Census_Import__c=:ApexPages.currentPage().getParameters().get('id')]){
        
            account a = [select id, name from account where ssn__c = :ai.ssn__c];
            if(a.id != ''){

 What I am trying to do is take one piece of information Account_to_import__c and compare it to the actual account. The first line " for(Account_To_Import__c ai :...." gets the original data.  Then for each object in that list I want to see if an account exists with the same SSN.  if it does I run one bit of code if it does not I run another bit of code.  The issue I am having is that when I run it if the account does not exist I get the error that the list has no rows for assignment.   How should I have this built?  Thanks for the help.

Best Answer chosen by Admin (Salesforce Developers) 
SLockardSLockard

You may need to change the map to <String, Account_To_Import> depending on what type ssn__c is, but try this:

 

List<Account_To_Import__c> a2i = [select id, name from Account_To_Import__c where Census_Import__c=:ApexPages.currentPage().getParameters().get('id')];
Map<Id, Account_To_Import> a2iMap = new Map<Id, Account_To_Import>();
for(Account_To_Import__c ai : a2i)
{
	a2iMap.put(ai.ssn__c, ai);
}

List<Account> accs = [select id, name from account where ssn__c IN :a2iMap.keySet()];
for (Account a : accs)
{
	// logic here .. a.Field_Name__c = a2iMap.get(a.ssn__c).Field_Name__c;
}

 

All Answers

SLockardSLockard

You should assign the Account a to List<Account> accs, because assigning a query result to just one object causes the error when the result is empty. Also, you shouldn't put any SOQL queries inside any for loops ..

Scott0987Scott0987

I can move the initail query out of the for loop easy enough, but how would I move the second one out?

    public list<Account_To_Import__c> a2i = [select id, name from Account_To_Import__c where Census_Import__c=:ApexPages.currentPage().getParameters().get('id')];
    for(Account_To_Import__c ai : a2i){
            account a = [select id, name from account where ssn__c = :ai.ssn__c];
            if(a.id != ''){

 How do I do the account soql outside of the for loop and how would I add it to a list?  everytime I try a list instead of an individual account I get the error that the  Initial term of field expression must be a concrete SObject:

SLockardSLockard

You may need to change the map to <String, Account_To_Import> depending on what type ssn__c is, but try this:

 

List<Account_To_Import__c> a2i = [select id, name from Account_To_Import__c where Census_Import__c=:ApexPages.currentPage().getParameters().get('id')];
Map<Id, Account_To_Import> a2iMap = new Map<Id, Account_To_Import>();
for(Account_To_Import__c ai : a2i)
{
	a2iMap.put(ai.ssn__c, ai);
}

List<Account> accs = [select id, name from account where ssn__c IN :a2iMap.keySet()];
for (Account a : accs)
{
	// logic here .. a.Field_Name__c = a2iMap.get(a.ssn__c).Field_Name__c;
}

 

This was selected as the best answer