1

I have created an Apex Class with an AuraEnabled method, which I am calling from a Lightning component.

After the method is called I am receiving the below error.

There was an exception List has no rows for assignment to SObject On the line number 9

I am adding the Apex code below. Can anyone please let me know what I am doing wrong here?

  public class RakitaTwentyFourApex {
        @AuraEnabled
        public static String createAccountContact(String accName, String firstName, String lastName, String phone) {
            System.debug('Account Name aks ' + accName + ' FirstName ' + firstName + ' LastName ' + lastName + ' Phone ' + phone);
            String statusString;
            Account accReturn = new Account();
            try {
                //String accStr = 'SELECT Id, Name FROM Account WHERE Name = accName LIMIT 1';
                accReturn = [SELECT Id, Name FROM Account WHERE Name = :accName LIMIT 1];
                
                if(accReturn != null) {
                    Contact con = new Contact();
                    con.FirstName = firstName;
                    con.LastName  = lastName;
                    con.Phone     = phone;
                    con.AccountId = accReturn.Id;
                    insert con;
                    
                    statusString = 'Account already existed with the ID ' + accReturn.Id + ' Contact was inserted Successfully ' + con.Id;
                } else {
                    Account acc = new Account(Name=accName);
                    insert acc;
                    
                    Contact con = new Contact();
                    con.FirstName = firstName;
                    con.LastName  = lastName;
                    con.Phone     = phone;
                    con.AccountId = acc.Id;
                    insert con;
                    
                    statusString = 'New Account was created the ID is ' + acc.Id + ' Contact was inserted Successfully ' + con.Id;
                }
            } catch(exception e) {
                statusString = 'There was an error';
                System.debug('There was an exception ' + e.getMessage() + ' On the line number ' + e.getLineNumber());
            }
            return statusString;
        }
    }

1 Answer 1

4

This structure is incorrect for protecting against non-existent result:

Account record = [SELECT ... FROM Account WHERE ... LIMIT 1];
if (record != null) { /*logic for existing record*/ }

As you have observed, record will never be null, but you will instead hit an exception. Instead, use the more canonical structure below:

List<Account> potentialRecords = [SELECT ... FROM Account WHERE ... LIMIT 1];
if (potentialRecords.isEmpty()) { /*logic for non-existent record*/ }    
else
{
    Account record = potentialRecords[0];
}
1
  • That worked! So I should have used a list and associated isEmpty() function to check for the non-existent result instead of using a single record variable. Got it. Thank you, Adrian. Commented Aug 9, 2021 at 18:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .