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

Test Methods - Can't retrieve data via SOQL after setting values in Trigger

I have some tests running for a Trigger (before insert), where I set some lookup fields (based on some logic from other fields).


Through debugging, I can see that the fields are set properly within my Trigger (let's call this field LookupField__c).


However, in my test class, if I query the inserted records [select Id, LookupField__c from Sobject where Id = :insertedRecord.Id], the LookupField__c is null (even though in the Trigger, I can see it was set).


The field is not touched anywhere else.  Is there a reason I can't query on this in the Test Class?  I could have sworn I used to be able to do this.


Eugene PozniakEugene Pozniak

Starting with 24 version of API your test methods do not have access to the real database. You should build a test database from your test or if you still want to use the real database set the @isTest(SeeAllData=true) annotation .


Hope, it will be the answer on your question.


Thanks for the reply, Euguene.  I added the annotation @isTest(SeeAllData=true) to the top of my test class, but I'm still not able to query the test data via SOQL in my test method.


I'm not familiar with building a test database - can you point me to where to get started?


Create the data in your actual test class, that you will use in your test class.


Hi Damien,


The problem is the Trigger is what sets the values;  if I create the data in the Test Class, I'm not really testing the Trigger functionality then, unless I'm not understanding.


- Test class creates record, does insert

- Trigger runs on "before insert", sets specific Lookup field value

- Test class then runs SOQL query to check that the values were set properly when running the trigger.




From the sounds of it, it sounded like you were querying for dependent data since the other suggestion was to use the 'SeeAllData'.  I must have misinterpreted what you needed because of this.


The only other suggestion I can make without knowing more is to make sure you use the Test.startTest() and Test.stopTest();


I think you are running that trigger in before case,

in before case records are not comited to database ,so it will return 0 records


place that soql in below block 


if(! isRunningTest){


soql in trigger




simply stop that code when you are running test.

before means before insert