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
JesseAJesseA 

Insert a static global variable from @future method

I have a class, called from a trigger which fills a private static List variable declared at the class level. At the end of the class a @future method is called who'se only line is to insert the List. ApexJobs lists it as completed but in the debug logs i can't actually see the insert call and of course the records are not being inserted.

 

So is it even possible to call insert on a class level static List of records be inserted by a @future methods? I know an @future method can insert a List, but usually the list is created from within the @future method itself.

 

Thanks, 

Jesse

Shashikant SharmaShashikant Sharma

Could you please share your code would be more helpful to understand the problem and tracing it.

JesseAJesseA

Here is the framework of what i'm trying to do:

 

public with sharing class ProgramDeviceMethods {
  private static List<DeviceApplicationAssoc__c> devAppsToInsert;

  public static void addProgramAppsToProgramsNewDevice(List<Program_Device__c> newList){
    devAppsToInsert = new List<DeviceApplicationAssoc__c>();
    ...CODE TO FILL LIST WITH NEW DeviceApplicationAssoc__c records
    futureInsertNewDevApps();
  }
	
  @future
  private static void futureInsertNewDevApps(){
    insert devAppsToInsert;
  }

 

JesseAJesseA

Okay so it looks like the class level variable isn't being maintained between methods. At the end of addProgramAppsToProgramsNewDevice the list contains records but when the @future method is called the list is empty.

 

Is there a way to maintain this class level List?

Muzammil BajariaMuzammil Bajaria
Hi JesseA,

I am facing the same issue. Can you please share your solution ?
Ghislain BergeronGhislain Bergeron
I am facing the same issue.
EI-TechnologiesEI-Technologies

Same here, 2 years after... 

Gaurav SadhwaniGaurav Sadhwani

Future method executes in a separate new context. It cant access any variables of class unless been passed.
In above scenario, you can have workaround by serializing your list devAppsToInsert and passing as String. In Future, deserialize it to List.

Check below link for code details:
https://www.mstsolutions.com/technical/passing-sobject-to-future-methods-in-salesforce/

Hope it helps!