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
SFDC_coderSFDC_coder 

I got this compile error yesterday but not today: variable result is used before it is declared

Code snippet: 
        static testMethod void testOptyTrigger2() {
        
            List<Id> newWorkItemIds;
            User u = [select id, Firstname, Alias, Email, Username, LastName, CommunityNickname, TimeZoneSidKey, 
                      LocaleSidKey, EmailEncodingKey, LanguageLocaleKey from User where Username='User1@marvell.com'];
            User u2 = [select id, Firstname, Alias, Email, Username, LastName, CommunityNickname, TimeZoneSidKey, 
                      LocaleSidKey, EmailEncodingKey, LanguageLocaleKey from User where Username='User2@marvell.com'];
            
            System.runAs(u){
            Opportunity testOpty = [select id from Opportunity where name ='TestOpty'];
            }

got this error when i am in System.runAs line. Please let me know the exact way to solve this error.. 

Thanks in advance.
NagendraNagendra (Salesforce Developers) 
Hi Duraisamy,

Apex is trying to use the variable you are declaring instead of referencing the class. To Apex, result and Result are the same things, and at the moment result is closest in scope. Try renaming it to r or resultA or whatever makes the most sense for your code.

Note: Apex is case insensitive Hope this helps.

Thanks,
Nagendra.

 
v varaprasadv varaprasad
Hi Duraisamy,

To query org data in testclass use SeeAllData = true in test class.
else insert new user like below.
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];

User u1 = new User(Alias = 'standt1',Country='United Kingdom',Email='demo1@randomdemodomain.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='demo1@andomdemodomain.com');

insert u1;

User u2 = new User(Alias = 'standt2',Country='United Kingdom',Email='demo2@randomdemodomain.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',LocaleSidKey='en_US', ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='demo2@andomdemodomain.com');

insert u2;

Thanks
Varaprasad
v varaprasadv varaprasad
Then use
system.runas(u1){
   -==========
 ======
}