Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a class

public class Sample {
  public static Map<String,List<String>> flowMap;

    @RemoteAction 
       public static List<String> getFlowList(){
        flowMap = new Map<String,List<String>>();
        MetadataService.MetadataPort service = createService();
        MetadataService.ListMetadataQuery query = new MetadataService.ListMetadataQuery();
        query.Type_x = 'Flow';
        MetadataService.FileProperties[] lmr = service.listMetadata(
            new MetadataService.ListMetadataQuery[] {query}, 30);
        for(MetadataService.FileProperties properties:lmr)
         {List<String> ff = flowMap.get(properties.fullName.split('-')[0]);
         if(ff == null)
         ff = new List<String>();
         ff.add(properties.fullName);
         flowMap.put(properties.fullName.split('-')[0],ff);
         }

        flowList = new List<String>(flowMap.keySet());
          return flowList;
       }
}

First in my page, I called getFlowList. After that, I clicked a button in which flowMap is used. In flowMap, value it is returning is null, but in this function, it's assigned flowMap. Why is it returning null?

share|improve this question

2 Answers 2

Static variables are implicitly transient, which means their state is not stored in the view state (meaning each transaction starts all static variables as null). Use an apex:actionFunction if you expect to be able to interact with the view state, and make sure your variable is not static.

share|improve this answer
1  
They won't then be able to access the map since it would be an instance variable, I don't think you can maintain any state on a Remoting class since they are stateless....if you want to persist data, don't you have to use viewstate a la normal VF/Controllers? –  Phil Hawthorn Jul 9 at 14:33
1  
@PhilHawthorn Of course, you're right. I edited the answer to be more appropriate for this case. –  sfdcfox Jul 9 at 14:35

See the following discussion about static variables in controllers. In short, static variables are only static within the scope of a single request, not multiple requests.

http://www.tgerm.com/2010/09/visualforce-apex-static-instance.html

If you want to preserve that state, you'll have to store the result of your remote method somewhere in your VF page. You can then pass it through to the server when the user clicks the button.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.