Retrieving NPE in Nanoflow

0
Hi Team,   I have a beautiful DS Nanoflow that creates an NPE.   Nano flows cant find the CurrentSession, so I cant bind the NPE to the current session. (which is what we usually do, so we can FindCreate NPEs without Memory getting out of control).   how does everyone get around this limitation? I assume i can change the DS to a MF, but then why would you ever use a NF with NPE?   https://community.mendix.com/link/space/app-development/questions/96108      
asked
1 answers
1

You can use mx.session for storing your NPE. Created an example using buddy chatgpt: See https://chatgpt.com/c/cd885b74-898b-4aab-a8f8-4ba7e80212be

 

Yes, you can use mx.session in Mendix to keep your non-persistent entity alive. By storing the non-persistent entity in the client-side session storage, you can ensure it persists for the duration of the session. Here’s how you can achieve this:

Using mx.session to Keep Non-Persistent Entity Alive

  1. Create a Non-Persistent Entity

    • Create a non-persistent entity in your domain model, e.g., SessionContainer,
    • This entity can have the necessary attributes and associations to other entities.
  2. Create a Nanoflow to Initialize the Non-Persistent Entity

    • Create a nanoflow that will create and initialize the non-persistent entity and store it in the session using mx.session .
  3. Retrieve the Non-Persistent Entity from the Session

    • Create a nanoflow to retrieve the non-persistent entity from the session when needed.

Step-by-Step Implementation

Step 1: Create Non-Persistent Entity

  • Create a non-persistent entity Session.Container with necessary attributes and associations.

Step 2: Create Nanoflow to Initialize and Store Entity

  1. Create Object

    • Use the "Create Object" activity to create a new instance of Session.Container.
    • Store the created object in a variable, e.g., Session.Container .
  2. JavaScript Action to Store Entity in Session

    • Use a JavaScript action to store the entity in the client-side session storage.
    • The JavaScript action can look like this:
      // JavaScript Action to store entity in session
      var entityJson = JSON.stringify(mx.data.create({
        entity: 'MyModule.SessionContainer',
        guid: entity.getGuid(),
        attributes: entity.getAttributes()
      }));
      
      sessionStorage.setItem('sessionContainer', entityJson);
      
    • Add JavaScript Action to Nanoflow

      • Call this JavaScript action in your nanoflow after creating the Session.Container

Step 3: Create Nanoflow to Retrieve Entity

  • JavaScript Action to Retrieve Entity from Session

    • Create a JavaScript action to retrieve the entity from the session storage.
    • The JavaScript action can look like this:
      // JavaScript Action to retrieve entity from session
      var entityJson = sessionStorage.getItem('sessionContainer');
      if (entityJson) {
        var entityObj = JSON.parse(entityJson);
        var entity = mx.data.create({
          entity: 'MyModule.SessionContainer',
          guid: entityObj.guid,
          attributes: entityObj.attributes
        });
        return entity;
      } else {
        return null;
      }
      

       

    • Add JavaScript Action to Nanoflow

    • Call this JavaScript action in your nanoflow to retrieve the Session.Container

 

Actually this could well get added to the NanoflowCommons module by Mendix.

answered