SAP B1 Forms Session

Forms Session

It’s hard dealing with the stateless condition of objects between events fired on the UI.

Static variables help with the problem. But when you wish to have more than one instance of the form then this approach does not work.

To work around this I came up with the class below. It uses the FormUID (the unique identifier of the form) to identify the object that belongs to it.

Meaning you create your object and store it by using the FormUID as the key. When an event gets fired you ask the FormsSession object to return you the object stored for the FormUID.

This takes care of both problems, the “stateless” condition and the single class object.

One thing not to forget is to remove the object from the Session when the form closes. Or like I intend to do, improve this class to auto-register to the close event and remove the stored object.

public class FormsSession<T> where T : class, new()
{
    private static Dictionary<string, T> _container = new Dictionary<string, T>();

    public void Add(string FormUID, T FormObject)
    {
      if(_container.ContainsKey(FormUID))
        _container[FormUID] = FormObject;
      else
       _container.Add(FormUID, FormObject);
    }

    public T Get(string FormUID)
    {
      if(!_container.ContainsKey(FormUID))
        _container.Add(FormUID, newT());
     
      return_container[FormUID];
    }

    public void Remove(string FormUID)
    {
      if(_container.ContainsKey(FormUID))
        _container.Remove(FormUID);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *