You need to sign in to do that
Don't have an account?
Mitchell McLaughlin 10
Apex - Run As
Hello,
In Apex Code, I'm running a very simple SOQL query.
List<Network__c> network = [SELECT Id FROM Network__c WHERE Name = 'Network1' AND IsActive__c = true LIMIT 1];
This is coming back with 0 rows in my Apex trigger, but it brings back one value in the Query Editor of the dev console. I'm not doing any magic to run as a specific user in the apex, so what other factors should I consider?
All I'm doing is running an apex test, when I'm logged in as a system administrator.
In Apex Code, I'm running a very simple SOQL query.
List<Network__c> network = [SELECT Id FROM Network__c WHERE Name = 'Network1' AND IsActive__c = true LIMIT 1];
This is coming back with 0 rows in my Apex trigger, but it brings back one value in the Query Editor of the dev console. I'm not doing any magic to run as a specific user in the apex, so what other factors should I consider?
All I'm doing is running an apex test, when I'm logged in as a system administrator.
Greetings to you!
If you are trying to run this in a test, you have to create the data first. Post API version 24, all real-world data is no longer accessible to tests without the seeAllData.
Annotate your test class or test method with IsTest(SeeAllData=true) to open up data access to records in your organization.
Considerations for the IsTest(SeeAllData=true) Annotation
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm?search_text=seealldata
https://www.sfdc-lightning.com/2018/09/testing-in-salesforce-with-seealldata.html
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
- I would agree with Khan that we need to create the data before you can query them in APEX Test class if you are NOT using (seeAllData = True) annotation.
- How to setup test data : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm
Regards,Krishna Avva