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
Cody Sanders 33Cody Sanders 33 

Use a record ID created in one method as a variable in another method within an Apex class

I'm currently working on a sandbox record creation class so I can prepopulate a couple records when refreshing sandboxes. However, I'm very inexperienced with Apex code and am hitting query limits, most likely because I'm having to look up the records previously created in the class. Is there a way that I can set the newly created record in the class so that I can then use the record ID as a variable in later methods? Basically all the queries here should be variables.
Here's the class:
public class SandboxClass {
    // Use this method to insert the accounts, the contacts, and the donations
    // you will need one method per object here is the account method
    public Account InsertCouplesAccount(){
        Account Acct1 = new Account();
        Acct1.Name='Couples Household';
        Acct1.Type='Household';
        Acct1.Location__c='City';
        Acct1.BillingCity ='City';
        Acct1.BillingCountry='United States';
        Acct1.BillingStreet='123 Test St.';
        Acct1.BillingState='Illinois';
        Acct1.BillingPostalCode='12345';   
        
        
        insert Acct1;
        return Acct1;
    } 
    public  Account InsertSingleAccount() {
    	Account Acct2 = new Account();
        Acct2.Name='Single Household';
        Acct2.Type='Household';
        Acct2.Location__c='Town';
        Acct2.BillingCity ='Indescript City';
        Acct2.BillingCountry='United States';
        Acct2.BillingStreet='321 Testing St.';
        Acct2.BillingState='Illinois';
        Acct2.BillingPostalCode='54321';
        
        insert Acct2;
        return Acct2;
    } 
    public Contact InsertHusbandContact() {
        Account Acct = [SELECT Id FROM Account WHERE Name = 'Couples Household'];
        Contact HusbCon = new Contact();
        HusbCon.Salutation = 'Mr.';
        HusbCon.FirstName = 'John';
        HusbCon.LastName = 'Couples';
        HusbCon.AccountId = Acct.Id;
        
        insert HusbCon;
        return HusbCon;
    }
    public Contact InsertWifeContact() {
        Account Acct = [SELECT Id FROM Account WHERE Name = 'Couples Household'];
        Contact HusbCon = [SELECT Id FROM Contact WHERE LastName = 'Couples'];
        Contact WifeCon = new Contact();
        WifeCon.Salutation = 'Mrs.';
        WifeCon.FirstName = 'Jane';
        WifeCon.LastName = 'Couples';
        WifeCon.AccountId = Acct.Id;
        WifeCon.Spouse__c = HusbCon.Id;
        
        insert WifeCon;
        return WifeCon;
    }
    public Contact UpdateHusbandContact() {
        Contact HusbCon = [SELECT Id FROM Contact WHERE FirstName = 'John'];
        Contact WifeCon = [SELECT Id FROM Contact WHERE FirstName = 'Jane'];
        HusbCon.Spouse__c = WifeCon.Id;
        
        update HusbCon;
        return HusbCon;
    }
    public Contact InsertSingleContact() {
        Account Acct = [SELECT Id FROM Account WHERE Name = 'Single Household'];
        Contact SingCon = new Contact();
        SingCon.Salutation = 'Mr.';
        SingCon.FirstName = 'Jack';
        SingCon.LastName = 'Single';
        SingCon.AccountId = Acct.Id;
        
        insert SingCon;
        return SingCon;
    }
    public Opportunity InsertCoupleDonation1() {
        Account Acct = [SELECT Id FROM Account WHERE Name = 'Couples Household'];
        Contact Cont = [SELECT Id FROM Contact WHERE FirstName = 'John'];
        Opportunity Don1 = new Opportunity();
        Don1.AccountId = Acct.Id;
        Don1.Contact__c = Cont.Id;
        Don1.StageName = 'Posted';
        Don1.Name = 'Couple Donation 1';
        Don1.Amount = 15;
        Don1.CloseDate = System.today();
        Don1.Location__c = 'City';
        Don1.Method_of_Payment__c = 'Cash';
        Don1.RecordTypeId ='0121N000000uGN8QAM';

        
        insert Don1;
        return Don1;
    }
    public Opportunity InsertCoupleDonation2() {
        Account Acct = [SELECT Id FROM Account WHERE Name = 'Couples Household'];
        Contact Cont = [SELECT Id FROM Contact WHERE FirstName = 'Jane'];
        Opportunity Don2 = new Opportunity();
        Don2.AccountId = Acct.Id;
        Don2.Contact__c = Cont.Id;
        Don2.StageName = 'Posted';
        Don2.Name = 'Couple Donation 2';
        Don2.Amount = 20;
        Don2.CloseDate = System.today();
        Don2.Location__c = 'Town';
        Don2.Method_of_Payment__c = 'Credit Card';
        Don2.RecordTypeId ='0121N000000uGN8QAM';

        
        insert Don2;
        return Don2;
    }
    public Opportunity InsertSingleDonation1() {
        Account Acct = [SELECT Id FROM Account WHERE Name = 'Single Household'];
        Contact Cont = [SELECT Id FROM Contact WHERE FirstName = 'Jack'];
        Opportunity Don3 = new Opportunity();
        Don3.AccountId = Acct.Id;
        Don3.Contact__c = Cont.Id;
        Don3.StageName = 'Posted';
        Don3.Name = 'Single Donation 1';
        Don3.Amount = 10;
        Don3.CloseDate = System.today();
        Don3.Location__c = 'City';
        Don3.Method_of_Payment__c = 'Cash';
        Don3.RecordTypeId ='0121N000000uGN8QAM';

        
        insert Don3;
        return Don3;
    }
    public Opportunity InsertSingleDonation2() {
        Account Acct = [SELECT Id FROM Account WHERE Name = 'Single Household'];
        Contact Cont = [SELECT Id FROM Contact WHERE FirstName = 'Jack'];
        Opportunity Don4 = new Opportunity();
        Don4.AccountId = Acct.Id;
        Don4.Contact__c = Cont.Id;
        Don4.StageName = 'Posted';
        Don4.Name = 'Single Donation 2';
        Don4.Amount = 25;
        Don4.CloseDate = System.today();
        Don4.Location__c = 'Town';
        Don4.Method_of_Payment__c = 'Cash';
        Don4.RecordTypeId ='0121N000000uGN8QAM';

        
        insert Don4;
        return Don4;
    }
    public Opportunity InsertSingleDonation3() {
        Account Acct = [SELECT Id FROM Account WHERE Name = 'Single Household'];
        Contact Cont = [SELECT Id FROM Contact WHERE FirstName = 'Jack'];
        Opportunity Don5 = new Opportunity();
        Don5.AccountId = Acct.Id;
        Don5.Contact__c = Cont.Id;
        Don5.StageName = 'Posted';
        Don5.Name = 'Single Donation 3';
        Don5.Amount = 45;
        Don5.CloseDate = System.today();
        Don5.Location__c = 'Town';
        Don5.Method_of_Payment__c = 'Cash';
        Don5.RecordTypeId ='0121N000000uGN8QAM';

        
        insert Don5;
        return Don5;
    }
}

 
Best Answer chosen by Cody Sanders 33
Cody Sanders 33Cody Sanders 33
I apologize, I posted and immediately after figured out the issue I was having. I guess sometimes that's the way it goes!

All Answers

Abdul KhatriAbdul Khatri
Can you please share the exact error with the line number? I probably be thinking it's not these insert causing any issues but may be some triggers those are getting trigger becuase of these inserts. So you might need optimize triggers.
Cody Sanders 33Cody Sanders 33
I apologize, I posted and immediately after figured out the issue I was having. I guess sometimes that's the way it goes!
This was selected as the best answer
Abdul KhatriAbdul Khatri
then you should have marked it resolved. Please do so if you are so kind. Thanks.