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
Mario G.Mario G. 

Cannot see modifications on class static variables from an @future method


I have a method that calls a @future method to make a callout.  For me to pass a list of objects to the callout, I created a static map  that holds this list with and associated Id.  Prior to the callout, I place the list of objects to be process in the map, place the callout, and retrivieve the list from within the callout method (I have to do this because I'm working with "in memory" updated objects not commited to the database).

Everything works fine when I do my code coverage tests but in real life, the static map always returns null when I retrieve the list of objects to process.
Am I missing something?
It's like the callout gets processed before the static variables manipulations which I doubt.

Thanks for the help!

This code simplifies what I'm trying to do:

public class MyClass() {
	static map<Id, list<Custom_Object__c>> tempDrawer = new map<Id, list<Custom_Object__c>>();
	static Boolean someBool = false;

	/**
	 * This calls a webservice
	 */
	@future (callout=true)
	static void calloutMethod(Id someId) {
		list<Custom_Object__c> objsToProcess = tempDrawer.remove(someId);

		//someBool is set to false!
		//objsToProcess.get(someId) returns null!
		if(someBool && objsToProcess != null && objsToProcess.size() > 0) {
			//process the objects and make the webservice call
		}
	}

	/**
	 * This method is called from a visualPage button
	 */
	public void someMethod(Id caseId) {
		//some code here...

		Custom_Object__c[] cObjs = new list<Custom_Object__c>();

		for(Integer i=0; i<10; i++) {
			cObjs.add(new Custom_Object__c(Status__c='New'));
		}

		tempDrawer.put(caseId, cObjs);
		someBool = true;
		system.assertEquals(10, tempDrawer.get(caseId).size()) //this is OK!
		system.assertEquals(true, someBool)	//this is OK too!

		//calling the callout method
		calloutMethod(caseId);
	}
}



Tony TannousTony Tannous
Dear Mario,

can you try to replace the remove with get, because the remove is deleting the object and not getting it.
 
list<Custom_Object__c> objsToProcess = tempDrawer.remove(someId);

with 

if(tempDrawer.containskey(someId))
{
list<Custom_Object__c> objsToProcess = tempDrawer.get(someId);
}

Good Luck

Mario G.Mario G.
Tony,

Thanks for your quick response!
I tried both ways and I get the same behavior... From the developer's guide reference, the remove method returns the value just removed.  Also, my boolean is still false when it should be true.

Thanks.
Tony TannousTony Tannous
Mario,

the best solution will be to write the code that fill the map table inside the calloutMethod and this will work 100 %.

Regards,