Legacy Knowledge Base
Published Jun. 30, 2025

Getting session ID from groovy script embedded in a workflow definition

Written By

Sorin Pop

How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!

While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.

Legacy Article

You are viewing an article from our legacy "FastTrack" publication program, made available for informational purposes. Articles in this program were published without a requirement for independent editing or verification and are provided"as is" without guarantee.

Before using any information from this article, independently verify its suitability for your situation and project.

Issue

  • I have an SEO requirement to track some information for my website.
    To that end I have inserted a groovy script in a workflow, which gets the user session, but it returns a null session ID.
  • Steps to reproduce:
    1. Create a workflow from the attached source in workflow_for_testing.txt.
    2. Create a new form in Contents&Data and assign it to that worklflow.
    3. Put it on a page.
    4. Go to that page and submit the form once. Watch the logs.
    Result: you see something like this logged

    Session is NOT NULL!HttpSessionAdaptor[com.liferay.util.servlet.NullSession#nctk, equinox.http.dynamic-data-mapping-form-web]

    5. Submit something in the form once again.
    Result: you see now a "normal" session ID logged.

    Session is NOT NULL!HttpSessionAdaptor[D46098C43B9A32517696BB5B4D7D7733, equinox.http.dynamic-data-mapping-form-web]

    All subsequent submits seems to be logging a real session ID. (or in some cases also null)

Environment

  • 7.4

Resolution

"The HttpSession object itself can be used in multiple threads (but is not thread-safe and therefore must be synchronized)…. extract session attributes you need in @Async method and pass them directly."

  • Maybe if you could create a DDMFormInstanceRecord model listener you could use the PortalSessionThreadLocal.getHttpSession() to extract these attributes:
     
    public class DDMFormInstanceRecordModelListener 
    extends BaseModelListener<DDMFormInstanceRecord> { 

    @Override 
    public void onBeforeCreate(DDMFormInstanceRecord ddmFormInstanceRecord) throws ModelListenerException { 
    HttpSession httpSession = PortalSessionThreadLocal.getHttpSession(); 

    }
     
    Note: The PortalSessionThreadLocal.getHttpSession() will return null in the groovy script as it is in a different thread.

 

Did this article resolve your issue ?

Legacy Knowledge Base