Forum Discussion

PAULEM's avatar
PAULEM
Advisor
6 years ago

env.context.message.uniqueId returns null

Hi

I have a script on my ForumTopicPage that captures the id of the original post.  I have used this:

<#assign msg_id = env.context.message.uniqueId />
<#assign msgQuery = "SELECT * FROM messages WHERE id='${msg_id}'" />
etc

It works fine - or so I thought.  I have found a thread with lots of replies - enough in fact to cause paging to occur - and this code doesn't work on page 2.  I get the big yellow error! Here's the advice provided:

InvalidReferenceException:The following has evaluated to null or missing:
==> env.context.message  [in template "custom.messageMarkup" at line 14, column 23]

----
Tip: It's the step after the last dot that caused this error, not those before it.
----
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----

----
FTL stack trace ("~" means nesting-related😞
	- Failed at: #assign msg_id = env.context.message...  [in template "custom.messageMarkup" at line 14, column 5]
----

 As suggested, I tried an initial IF test:

<#if env.context.message.uniqueId??>
  <#assign msg_id = env.context.message.uniqueId />
  etc
</#if>

But this didn't work!  I still get the same error.

Does anyone know either 

  • How to get the ID of the original post when you're not on the first page of the thread, OR
  • How to intercept the error and stop it from appearing (ie stop the code from running)?

Thanks in advance

Paul (Tasmania)

  • PAULEM,

    There are two ways to get the messages id from context object.

    Your condition would be like below code.

    <#if env.context.message?? >
      <#assign msg_id = env.context.message.uniqueId />
    <#else>
      <#assign msg_id = page.context.message.uniqueId />
    </#if>

     

    You can get the ID of the currently rendered message
    ${page.context.message.id}
    ${env.context.message.id}

    You can get the unique ID of the currently rendered message
    ${page.context.message.uniqueId}
    ${env.context.message.uniqueId}

  • UPDATE:

    I've found Parshant's answer on page-context-user-on-MediaGalleryPage worked for me to!  When you go to page 2 (etc) the URL has '/page/2' appended to the end so I've used Parshant's code to check the URL.

    I would rather run the script, so if there's a way to get the OP's ID on page 2 I'd love to hear about it. In the meantime, this has removed the error from page 2, etc which is good for our community members at least!

    Cheers, Paul.

     

    • Parshant's avatar
      Parshant
      Boss

      PAULEM,

      There are two ways to get the messages id from context object.

      Your condition would be like below code.

      <#if env.context.message?? >
        <#assign msg_id = env.context.message.uniqueId />
      <#else>
        <#assign msg_id = page.context.message.uniqueId />
      </#if>

       

      You can get the ID of the currently rendered message
      ${page.context.message.id}
      ${env.context.message.id}

      You can get the unique ID of the currently rendered message
      ${page.context.message.uniqueId}
      ${env.context.message.uniqueId}

  • This method returns a list of all bundles installed in the OSGi environment at the time ... be thrown with a human readable message where the filter became unparsable.