+ Start a Discussion
Mitchell McLaughlin 10Mitchell 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.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Mitchell,

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
  • If a test class is defined with the isTest(SeeAllData=true) annotation, this annotation applies to all its test methods whether the test methods are defined with the @isTest annotation or the testmethod keyword.
  • The isTest(SeeAllData=true) annotation is used to open up data access when applied at the class or method level. However, using isTest(SeeAllData=false) on a method doesn’t restrict organization data access for that method if the containing class has already been defined with the isTest(SeeAllData=true) annotation. In this case, the method will still have access to all the data in the organization.

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
Raj VakatiRaj Vakati
See ALl to true is not the best practices .. you can do one think use system.runs with System admin profile 
 
Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
       System.runAs(uu){
	   YOUR TEST CLASS
	   }


 
Krishna.AvvaKrishna.Avva
Hi Mitchell,
  • 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