You need to sign in to do that
Don't have an account?
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 ??
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.
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 ??
Is there any reason why you can't move the insert outside the for loop?