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
devendra dhakadevendra dhaka 

Static list in class

global class InsertDefaultLeaveBalanceForNewUserClass
   {
    
    Static List<Leave_Balance__c> leaveBalanceList = new List<Leave_Balance__c>();
    
    @future
     Global Static void createLeaveBalance(Id userId,Decimal defaultLeave,ID leaveTypeID)
      {
          Leave_Balance__c lb = new Leave_Balance__c();
                lb.Employee_Name__c=userId;
                lb.LeaveType__c=leaveTypeID;
                lb.Leave_Credit__c=defaultLeave;
                  
                leaveBalanceList.add(lb);
                system.debug(leaveBalanceList.Size());     // it always gives list size to 1
               
      
      }
   
    Global Static void insertLeaveBalance()
       {
         system.debug(leaveBalanceList.size());      // here always the list size is 0
         insert leaveBalanceList;
       
       }
   
   }

 

 

How can I modify this code to insert the records ??

bob_buzzardbob_buzzard

Statics only exist for the life of a transaction in Apex, as opposed to Java etc where they are available all the time.  Thus when your @future method is invoked, it will be in a new transaction and thus back to an empty list.

devendra dhakadevendra dhaka

Then how do I achieve this ??

Because I cannot insert it when a record is created , as that is inside a for loop.

 

Any work around ??

bob_buzzardbob_buzzard

Is there any reason why you can't move the insert outside the for loop?