Session storage

Inbuilt method to store data to the server session and then later reuse it in the same session. Data is held in the session until it is active, logging out will remove the data.

This can be used as an alternative to shared data between pages or applications. Data stored here is available for all applications.

Store data

Values will always be stored as strings. If we want to store other types then we can use one of the helper functions to cast it into different types.

{{ .Storage.Set "key" "value" }}

Store data using input

Simple set command

Starting from version 1.193.0, GoErp provides option to save data through form input parameters, using reserved name Storage.Set. The value of input should have specific syntax, which allows to pass key and value in one string. The syntax is {key}->{value}, where {key} is a key for entry and {value} is actual value In the next sample we are setting entry storage value to the storage under key unique_key.

<form method="post">
    <input type="text" name="Storage.Set" value="unique_key->storage value">
    <button type="submit">Submit </button>
</form>

Using instructions to save data from response payload in dynamics

There is also option to save data from response of specific request to the storage using instructions. Instructions for dynamics using same searching patterns like in chaining:

<form method="post">
  <input type="hidden" name="CRMApi.Api.Post.createCustomer" value="v1/customers/individuals">
  <input type="text" name="CRMApi.Api.Json.createCustomer.string.firstName" value="cool name">

  <!-- Save newly created customer ID to the storage -->
  <input type="hidden" name="Storage.SetInstruction" 
         value="new_customer_id->createCustomer.Response.id">

  <button type="submit">Submit</button>
</form>

<!-- Retrieve new customer ID on next page or even in the same page. -->
<br/>New customer ID: {{ .Storage.Get "new_customer_id" }}

Saving specific parameters from models to storage

Models also supports saving some data based on the path, but options are very restricted in comparison with dynamics. While using save instructions with models, it is possible to save primitive types only (like strings, integers, etc.). Although, there is option to access arrays by using the index.

Some rules that need to take into account when using storage:

  • In the same context .Set and .SetInstruction always called first and .Get call performed last.

Sample for public pages (referring to the customer object which is not available in private):

<h1>Create sales doc</h1>
<form method="post">
    <input type="hidden" name="postActionEntity" value="SalesDocumentInput"/>
    <!-- take customer ID from the session, which will not expose it on the page -->
    <input type="hidden" name="ErplyApi.SalesDocumentInput.CustomerID<-" value="Session.customer.ID">
    <!-- Send instruction for the storage to save the newly created sales document ID to the storage 
         under name new-invoice-id. Choose any name as you want. -->
    <input type="hidden" name="Storage.SetInstruction" value="new-invoice-id->ErplyApi.SalesDocument.ID">
  
    <!-- here goes all other inputs related to the sales document -->

    <button type="submit">Create</button>
</form>

<!-- get newly created document ID from the storage -->
New sales doc ID: {{ .Storage.Get "new-invoice-id" }}

<h1>Get sales documents using models and linking</h1>
<input name="Preset.ErplyApi.SalesDocumentQuery.ClientID<-" value="Session.customer.ID">

<ul>
    {{ range $row := .Data.ErplyApi.SalesDocumentList }}
    <!-- Use any of the available fields here -->
    <li>{{ $row.ID }} | {{ $row.Number }} | {{ $row.ClientID }}</li>
    {{ end }}
</ul>

Reading data

{{ .Storage.Get "key" }}