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
sohail najmisohail najmi 

I have testDataUtil class with methods How to use List as golobal within testDataUtil class

Following are the code:



@isTest
public class TestDataUtil {
  // need to set as golobal list, can be access within class
       List<Account> lstAccount = new List<Account>(); 
       List<Product2> lstProducts = new List<Product2>();
       List<Order> lstOrders = new List<Order>();


  public static List<Account> createTestRecordsAccount(Integer numRecords) {
        List<Account> accts = new List<Account>();
        for(Integer i=0;i<numRecords;i++) {
            Account acco = new Account(Name='TestAccount'+i, Phone = '123456789'+i, Fax = '123455677'+i);
            accts.add(acco);
        }
            insert accts;
          return accts;
    }

 public static List<Product2> createTestRecordsProducts(Integer numRecords) {
      List<Product2> lstProducts = new List<Product2>();
         for(Integer i=0;i<numRecords;i++) {
              Product2 prod = new Product2(Name = 'Example Product '+i,
                                        Description = 'This is the Product description.'+i,
                                        ProductCode = 'EX1234'+i,
                                        StockKeepingUnit = 'EX5678'+i,
                                        Family = 'Example Product Family'+i,
                                        QuantityUnitOfMeasure = 'inches'+i,
                                        DisplayUrl = 'https://www.DisplayUrl.com/'+i,
                                        ExternalId = 'ID #1234'+i
                                        );
                   lstProducts.add(prod);}
                    insert lstProducts;    
              return lstProducts;
     }
     
     public static List<Order> createTestOrder(Integer numRecords, Boolean ifInsert) {
          List<Order> lstOrders = new List<Order>();
         List<Account> accts =  TestDataUtil.createTestRecordsAccount(numRecords);
         List<Pricebook2> pBook =  TestDataUtil.createTestPricebook(numRecords, ifInsert);

         for(Integer i=0;i<numRecords;i++) {
          Order ordo = new Order(Name = 'Test Order ',
                                Status = 'Draft',
                                EffectiveDate = system.today(),
                                EndDate = system.today() + 4,
                                AccountId = accts[i].id,
                                Pricebook2Id =  pBook[i].Id,
                                ShippingStreet  = 'shipping Address shipping Street   );
           lstOrders.add(ordo);
                insert lstOrders;
          return lstOrders;
     }



Question:
I have added  List  'lstAccount, lstProducts, lstOrders' as golobal. 

Now adding method and returning the List where needed. 

On 'createTestOrder' method i am calling   'createTestRecordsAccount  & createTestPricebook' again. 

Is there anyway i can save the list in these golobal list 'lstAccount, lstProducts',   MEANS where-ever i need data from these method firstly i'll check the list (lstAccount..size() >0 && lstAccount !=null). 

If lstAccount.size>0, means i have the data, so no need to call 'createTestRecordsAccount ' method again. saving dml iteration. 
    
  like following: 

  public static List<Account> createTestRecordsAccount(Integer numRecords) {
        List<Account> accts = new List<Account>();
        for(Integer i=0;i<numRecords;i++) {
            Account acco = new Account(Name='TestAccount'+i, Phone = '123456789'+i, Fax = '123455677'+i);
            accts.add(acco);
        }
            insert accts;

lstAccount.add(acco);  /// saving in golobal LIST ... (how to save it)?
          return accts;
    }  



​​​​​​​Thanks in advance ...... 
Surya GSurya G
Hi Sohail Nijami,

you can call variables with static as the methods all declared static, and you can call the variable directly inside your method and assign values to it.
         private static List<Account> lstAccount; 
        private static List<Product2> lstProducts;
        private static List<Order> lstOrders;

Thanks
Surya G
sohail najmisohail najmi
Thanks @Surya G for your reply. 

When i saved the list in 'static list' it allow me to save the list, the reason i was doing this because i have to swap list between 2 classes. 
This solutuion only working within class, i can't access the data in list in another class...

In the 2nd class iam access the data through this method: 

public static List<OrderItem> createTestRecordOrderItem(Integer numRecords, Boolean ifInsert,   List<Order> lstOrder, List<Product2> lstProduct) {
   List<Order> lstOrders =  (lstOrder != null && lstOrder.size()>0) ? lstOrder : TestDataUtil.createTestOrder(numRecords, ifInsert, null, null);
                                                 //createTestOrder -> (1 ,true,List<Account>,List<Pricebook2>)
     List<Product2> lstProducts = (lstProduct != null && lstProduct.size()>0) ? lstProduct : TestDataUtil.createTestRecordsProducts(numRecords, true);
}

lstOrder & lstProduct always return null. 


Thanks for the reply.