You need to sign in to do that
Don't have an account?
Aidel 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);
}
}
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);
}
}
I know it's an Account object, but if you could show the code from where this is getting called?
{
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;
}
If you query an account from database and then pass it to the method, you will get the value of the ID.
How can I do what I want to do with an account I created myself?
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:
Please let me know if you face any issues.
Thanks