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;
}
}