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
Robert Webber 6Robert Webber 6 

global variable from query

I need to reduce SOQL queries. I access the Organization record a lot and would like to do a query one time when my application starts and make the data available so I don't have to requery in other classes. I thought this would be a simple matter of declaring a variable as global in my home page controller, but it isn't visible from other classes. Am I misunderstanding the use of global variables, or is there a way to access Organization data without having to requery?
mukesh guptamukesh gupta
Hi Robert,

Please follow below example:- 

HomePageComponent class:-

Public static List<Account> acc = new List<Account>();

acc = [select id, name from Account ];

Another Class:-

System.debug('ENTER in anoher class ====>>> +'HomePageComponent.acc);

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 




 
Robert Webber 6Robert Webber 6
I put this in HomePageController1:
    public static Organization OrgInfo;
        OrgInfo = [Select FiscalYearStartMonth,NameSpacePrefix,UsesStartDateAsFiscalYearName From Organization Where id=:Userinfo.getOrganizationId()];

Tried to use in another class:
        Organization orgInfo = HomePageController1.OrgInfo;

and it shows OrgInfo as not existing, even though it shows as a field in the record. The class using this is static. Does that  make any difference?



 
mukesh guptamukesh gupta
Hi Robert,

Please follow below URL:-

https://www.sfdc-lightning.com/2018/10/static-variable-in-salesforce.html

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 


 
Robert Webber 6Robert Webber 6
I appreciate the help, but it doesn't seem to do what I want.

I initialize OrgInfo in HomePageController1:

    public static Organization OrgInfo;

in HomePageConstroller1 constructor:

        OrgInfo = [Select FiscalYearStartMonth,NameSpacePrefix,UsesStartDateAsFiscalYearName From Organization];

I added the following to my test setup in one of my unit test classes:

        HomePageController1 homePage = new HomePageController1();
        system.debug(LoggingLevel.Info,'**** '+HomePageController1.OrgInfo);

I added a unit test:

    @isTest static void testStatic() {
        Organization orgInfo = HomePageController1.OrgInfo;
        system.debug(LoggingLevel.Info,'**** '+orgInfo);
    }    

The log shows that the Organization record during the test setup, but when the unit test runs, orgInfo is null;

I'm beginning to think that what I am trying to do is not possible. I just want to make the Organization record available in all my other classes without having to requery when I access its fields.