AddToSession Use Examples
Basic AddToSession Use
In this example we have a button process which first tries to retrieve our TestVariable. If it is not found, it adds it.
The first time, we press the button, it tries to retrieve our session variable, which doesn’t exist yet and since it is not found, it is created in AddToSession.
Here are the inputs for that service and the logs of it running:
Now it is a session variable that exist and the next time we click the button, the RetrieveFromSession service will find the value and output it to the user is a message.
Now if we close the browser and try again, it again will not be found. The same will happen if we let the page sit long enough to reach the session timeout. Usually users see this as leaving the page open for a long time and when they return, they are required to log in again. This is because we keep track of the logged in user by their session variables.
Using AddToSession to Store CartId
In this example, the GetCartId process has been called and the output should return a default cart id for the user.
We query the database to look for a default cart for the user. If the user just placed an order, their cart would be deleted and a default cart would not be found. From there, the process will create a new default cart for the user if one was not found.
At this point, the user has a default cart and the process adds that cartid variable to the session. This way, it can easily be retrieved by a RetrieveFromSession service. After this, the process also outputs the CartId so that wherever the GetCartId process is called, the output returns the cart id.
Here is a closer look at those AddToSession service inputs:
Here is one of the places that uses the session variable we just saved. When the tax amount is needed the GetTax process is called.
- It first checks if tax should be hidden for the site.
- Then it checks if this particular user should have tax hidden.
- After that, it checks if we are finding the tax for the cart or for an already placed order in JDE.
- If the tax needs to be calculated for the cart, it then checks if the CartTotal was passed in through the input. If not, it will need to be calculated.
- If we are going to calculate the CartTotal, we will need the CartId. Retrieve CartId with RetrieveFromSession service.
Here is a closer look at the service’s inputs: