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
Aidel BruckAidel Bruck 

why does the Account ID equal Null

I am passing a person ID to a function that creates opportunities.
I am completely stumped because the Account ID equals null.
If it makes any difference, the account is a person account
below is the code:

static void CreateServices(Account a)
    {
        system.debug(a.FirstName+'firstname'+a.Id);  //when I do this the names print but not the ID. What is going on?
        List<RecordType> opRecordType = [SELECT id, Name FROM RecordType WHERE SobjectType = 'Opportunity'];
        For (RecordType rt: opRecordType)
        {
               
            Opportunity newOpp= new opportunity();
            newOpp.Name='test' +i;
            newOpp.RecordTypeId=rt.ID;
            i++;
            newOpp.AccountId= a.Id;
            system.debug('account ID'+a.Id);
            ... //code adds more details to the opportunity.
            createdServices.add(newOpp);
        }
        
    }
Vishal Negandhi 16Vishal Negandhi 16
can you also share what are you passing to CreateServices?
I know it's an Account object, but if you could show the code from where this is getting called?
Aidel BruckAidel Bruck
static testMethod void UpdateAccount()
    {

        test.startTest();
               system.debug('starting now');
        Account A1= CreateAccount();
        CreateServices(A1);
        insert A1;
....
}

Static Account CreateAccount()
    {
       
        
        Account newAccount= new Account();
        newAccount.RecordTypeId= [select Id from RecordType where DeveloperName = 'recordTypeName' and SobjectType = 'Account'].Id;
        system.debug(newAccount.RecordTypeId);
        newAccount.FirstName= 'bla';
        newAccount.LastName= 'humpty dumpty';
        system.debug(newAccount.FirstName+ newAccount.FirstName);
        newAccount.Birthdate__c= date.today();
        ...//Code populates other account fields
        
        
        return newAccount;
     
    }
Keshab AcharyaKeshab Acharya
Looks like you are creating the account in the class itself and then passing it to the method CreateServices();

If you query an account from database and then pass it to the method, you will get the value of the ID.
Aidel BruckAidel Bruck
I do not want to use live data.
How can I do what I want to do with an account I created myself?
Jithin Krishnan 2Jithin Krishnan 2
Hi Aidel,
I think that is because you are calling the method before inserting Account object. The id will be created only when the record is inserted. Try changing the order of your testmethod like this:
static testMethod void UpdateAccount()
    {

        test.startTest();
               system.debug('starting now');
        Account A1= CreateAccount();
        insert A1;
        CreateServices(A1);
....
}

Please let me know if you face any issues.
Thanks
Keshab AcharyaKeshab Acharya
You can do something like this.
 
@isTest 
private class AccountId {
    void createData() {
       Account a = new Account(Name='Hello');
       insert a;
    }
    static testmethod void testAccount(){
        createData();
        Account a = [select Name from Account where Name =:'Hello'];
        Test.startTest();
        	CreateServices(a);
        Test.stopTest();
    }
    static void CreateServices(Account a)
    {
        system.debug(a.FirstName+'firstname'+a.Id);  //when I do this the names print but not the ID. What is going on?
        List<RecordType> opRecordType = [SELECT id, Name FROM RecordType WHERE SobjectType = 'Opportunity'];
        For (RecordType rt: opRecordType)
        {
               
            Opportunity newOpp= new opportunity();
            newOpp.Name='test' +i;
            newOpp.RecordTypeId=rt.ID;
            i++;
            newOpp.AccountId= a.Id;
            system.debug('account ID'+a.Id);
            createdServices.add(newOpp);
        }
        
    }
}