How to resolve the apex compile error: variable does not exist?
I'm having issues with creating a pretty simple test Utility Class. I want to create two records: an account and a related child contact. The error I see in the developer console is:
Variable does not exist: a.ID
My code, based on the Salesforce Apex Workbook, is as follows. I've played around with various things, but it's not working. Where am I going wrong? Thanks in advance.
@isTest
public class AccountSaveTestSuite {
public static Account createOneAccount(){
Account testAccount = createAcct('ABC Computing inc.');
Contact testContact = createContact();
return testAccount;
}
// Helper methods //
//
private static Account createAcct(string accountName) {
Account a = new Account(
Name=accountName);
insert a;
return a;
}
private static Contact createContact(){
Contact c = new Contact (
FirstName = 'Jane'
, LastName = 'Smith'
, Account = a.ID); // *** Error is here ***
insert c;
return c;
}
}
To resolve the apex compile error: variable does not exist:
Since a is declared inside a separate method, it isn't visible from other methods. Here's one way to deal with it by passing the Account to the createContact method.
@isTest
public class AccountSaveTestSuite {
public static Account createOneAccount(){
Account testAccount = createAcct('ABC Computing inc.');
Contact testContact = createContact(testAccount);
return testAccount;
}
// Helper methods //
//
private static Account createAcct(string accountName) {
Account a = new Account(
Name=accountName);
insert a;
return a;
}
private static Contact createContact(Account a){
Contact c = new Contact (
FirstName = 'Jane'
, LastName = 'Smith'
, AccountID = a.ID); // *** Error is here ***
insert c;
return c;
}
}