3

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?

2 Answers 2

4

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.

1
  • link doesn't work anymore. fyi.
    – SamuelDev
    Feb 8 at 8:30
3

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.

2
  • 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? Jul 9, 2014 at 14:33
  • 1
    @PhilHawthorn Of course, you're right. I edited the answer to be more appropriate for this case.
    – sfdcfox
    Jul 9, 2014 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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