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
Benedetto Virzi 17Benedetto Virzi 17 

Contact field Accountid in test

Hi,
I have a liltte problem with accountid in my test class. In my test method I create account and contact in this way:
 
Account a = new Account(name='Mycompany'); 
	 	insert a;
	 	Contact c = new Contact(Accountid=a.id, firstname='Mario', lastname='Rossi',Codice_Cliente__c='001');
	 	insert c;
In my production class I do this:
private static Account idAccount;
	
	idAccount = [select id from Account where name='MyCompany'];

       id idContact=[select id from Contact where Accountid=:idAccount.Id].id;
But second select, fail.
With the system.debug I have seen that the Accountid filed in Contact record, is different from idAccount variable populeted by selecting Account record by name. How is it possible?

Benny
 
Vla ChaVla Cha
Hi Benedetto, 

I see that name for accounts are different: one is called 'Mycompany' and the other one - 'MyCompany', therefore you are querying a different account in your production class.

Cheers!
Benedetto Virzi 17Benedetto Virzi 17
Hi Vla,

thanks for the replay but this is only an example. In my code the string it's ok.
ShotShot
Try to check if you have two accounts with the same name.
Can you also provide code with System.debug code and result, which you are getting.
Tejpal KumawatTejpal Kumawat
Hello Benedetto Virzi,

Test classes records are imaginary records, those never created in the org database. So please won't confuse with those ids.

If you will check those id in your org then you will found error like : Data not avaliable.

In the test class always palace  @isTest(seeAllData = false) on top of class & never check with hard coded ids.

I think you are receiving error like : No row found.

If this answers your question then hit Like and mark it as solution!
Benedetto Virzi 17Benedetto Virzi 17
Hello Tejpal,

i don't use hard coded ids. For this I work with the account name. I create account with fixed name in test class and with the same name i do select in the production class. I'm confused!!

Benny
ShotShot
What do you mean 'test class'? Is it class with @isTest?  If yes, what you create in Test class visible only in this class, data wont be added to real DB.